Skip to content

Commit

Permalink
Merge branch 'release/v1.6.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
quvox committed Nov 11, 2019
2 parents 4c7eb92 + ac2c769 commit 6fca37d
Show file tree
Hide file tree
Showing 23 changed files with 913 additions and 733 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -4,7 +4,6 @@ go:
- "1.12.x"
before_install:
- go get github.com/mattn/goveralls
- bash prepare.sh
script:
- $GOPATH/bin/goveralls -service=travis-ci
- go test -race -coverprofile=coverage.txt -covermode=atomic
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,10 @@
CHANGELOG
====

## v1.6.0
* change programming interfaces
- BBcTransaction and its child classes provide utility methods to create objects.

## v1.5.1
* remove dependency on libbcsig
- all key manipulation and sign/verify functions become golang native
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -12,9 +12,9 @@ This reposigory is originally from https://github.com/quvox/bbclib-go

### Features
* Support most of the features of py-bbclib in https://github.com/beyond-blockchain/py-bbclib
* BBc-1 version 1.5
* BBc-1 version 1.6
* transaction header version 1 and 2.
* Go v1.10 or later
* Go v1.12 or later (need go mod)

## Usage

Expand Down
14 changes: 14 additions & 0 deletions asset.go
Expand Up @@ -37,6 +37,7 @@ BBcAsset can contain a digest of a file, string, map[string]interface{} object a
type (
BBcAsset struct {
IdLengthConf *BBcIdConfig
Version uint32
digestCalculating bool
AssetID []byte
UserID []byte
Expand Down Expand Up @@ -115,6 +116,19 @@ func (p *BBcAsset) AddFile(fileContent *[]byte) {
p.AssetFileDigest = digest[:]
}

// AddBody sets data in the BBcAsset object
func (p *BBcAsset) AddBody(bodyContent interface{}) {
if body, ok := bodyContent.(string); ok {
p.AssetBody = []byte(body)
p.AssetBodySize = uint16(len(body))
} else if body, ok := bodyContent.([]byte); ok {
copy(p.AssetBody, body)
p.AssetBodySize = uint16(len(body))
} else {
_ = p.AddBodyObject(bodyContent)
}
}

// AddBodyString sets a string data in the BBcAsset object
func (p *BBcAsset) AddBodyString(bodyContent string) {
p.AssetBodyType = 0
Expand Down
1 change: 1 addition & 0 deletions asset_hash.go
Expand Up @@ -32,6 +32,7 @@ The length of "AssetID" is defined by "IDLength".
type (
BBcAssetHash struct {
IdLengthConf *BBcIdConfig
Version uint32
AssetIdNum uint16
AssetIDs [][]byte
}
Expand Down
3 changes: 2 additions & 1 deletion asset_raw.go
Expand Up @@ -33,6 +33,7 @@ The length of "AssetID" is defined by "IDLength".
type (
BBcAssetRaw struct {
IdLengthConf *BBcIdConfig
Version uint32
AssetID []byte
AssetBodySize uint16
AssetBody []byte
Expand All @@ -53,7 +54,7 @@ func (p *BBcAssetRaw) SetIdLengthConf(conf * BBcIdConfig) {
p.IdLengthConf = conf
}

// AddBodyString sets a string data in the BBcAsset object
// AddBody sets a string data in the BBcAsset object
func (p *BBcAssetRaw) AddBody(assetID *[]byte, assetBody interface{}) {
if assetID != nil {
p.AssetID = make([]byte, p.IdLengthConf.AssetIdLength)
Expand Down
181 changes: 11 additions & 170 deletions bbclib.go
Expand Up @@ -38,7 +38,6 @@ import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"time"
)

Expand Down Expand Up @@ -183,189 +182,31 @@ func Deserialize(dat []byte) (*BBcTransaction, error) {
return nil, errors.New("formatType not supported")
}


// MakeTransaction is a utility for making simple BBcTransaction object with BBcEvent, BBcRelation or/and BBcWitness
func MakeTransaction(eventNum, relationNum int, witness bool) *BBcTransaction {
txobj := BBcTransaction{Version: 2}
txobj.SetIdLengthConf(&IdLengthConfig)
txobj.Timestamp = time.Now().UnixNano() / int64(time.Microsecond)

for i := 0; i < eventNum; i++ {
evt := BBcEvent{}
txobj.AddEvent(&evt)
evt := BBcEvent{Version: txobj.Version}
evt.SetIdLengthConf(&txobj.IdLengthConf)
txobj.Events = append(txobj.Events, &evt)
}

for i := 0; i < relationNum; i++ {
rtn := BBcRelation{}
txobj.AddRelation(&rtn)
rtn := BBcRelation{Version: txobj.Version}
rtn.SetIdLengthConf(&txobj.IdLengthConf)
txobj.Relations = append(txobj.Relations, &rtn)
}

if witness {
wit := BBcWitness{}
txobj.AddWitness(&wit)
wit := BBcWitness{Version: txobj.Version}
wit.SetIdLengthConf(&txobj.IdLengthConf)
wit.Transaction = &txobj
txobj.Witness = &wit
}

return &txobj
}

// addInRelation is an internal function to create a BBcAsset and add it to BBcRelation object and then a BBcTransaction object
func addInRelation(transaction *BBcTransaction, relationIdx int, assetGroupID, userID *[]byte) {
ast := BBcAsset{}
transaction.Relations[relationIdx].Add(assetGroupID, &ast)
ast.Add(userID)
}

// AddRelationAssetFile sets a file digest to BBcAsset in BBcRelation and add it to a BBcTransaction object
func AddRelationAssetFile(transaction *BBcTransaction, relationIdx int, assetGroupID, userID, assetFile *[]byte) {
if transaction == nil {
return
}
addInRelation(transaction, relationIdx, assetGroupID, userID)
if assetFile != nil {
transaction.Relations[relationIdx].Asset.AddFile(assetFile)
}
}

// AddRelationAssetBodyString sets a string in BBcAsset in BBcRelation and add it to a BBcTransaction object
func AddRelationAssetBodyString(transaction *BBcTransaction, relationIdx int, assetGroupID, userID *[]byte, body string) {
if transaction == nil {
return
}
addInRelation(transaction, relationIdx, assetGroupID, userID)
if body != "" {
transaction.Relations[relationIdx].Asset.AddBodyString(body)
}
}

// AddRelationAssetBodyObject sets an object (map[string]interface{}) in BBcAsset in BBcRelation, convert the info into msgpack, and add it in a BBcTransaction object
func AddRelationAssetBodyObject(transaction *BBcTransaction, relationIdx int, assetGroupID, userID *[]byte, body interface{}) {
if transaction == nil {
return
}
addInRelation(transaction, relationIdx, assetGroupID, userID)
if body != nil {
if err := transaction.Relations[relationIdx].Asset.AddBodyObject(body); err != nil {
fmt.Println("Fail to exec AddRelationAssetBodyObject")
}
}
}

// AddRelationAssetRawBody sets a data in BBcAssetRaw in BBcRelation and add it to a BBcTransaction object
func AddRelationAssetRaw(transaction *BBcTransaction, relationIdx int, assetGroupID, assetID *[]byte, assetBody interface{}) {
ast := BBcAssetRaw{}
ast.SetIdLengthConf(&transaction.IdLengthConf)
ast.AddBody(assetID, assetBody)
transaction.Relations[relationIdx].AddAssetRaw(assetGroupID, &ast)
if transaction.Version < 2 {
transaction.Version = 2
transaction.Relations[relationIdx].Version = 2
}
}

// AddRelationAssetHash sets assetIDs in BBcAssetHash in BBcRelation and add it to a BBcTransaction object
func AddRelationAssetHash(transaction *BBcTransaction, relationIdx int, assetGroupID *[]byte) {
ast := BBcAssetHash{}
ast.SetIdLengthConf(&transaction.IdLengthConf)
transaction.Relations[relationIdx].AddAssetHash(assetGroupID, &ast)
if transaction.Version < 2 {
transaction.Version = 2
transaction.Relations[relationIdx].Version = 2
}
}

// AddRelationPointer creates and includes a BBcPointer object in BBcRelation and then, add it in a BBcTransaction object
func AddRelationPointer(transaction *BBcTransaction, relationIdx int, refTransactionID, refAssetID *[]byte) {
if transaction == nil {
return
}
ptr := BBcPointer{}
transaction.Relations[relationIdx].AddPointer(&ptr)
ptr.Add(refTransactionID, refAssetID)
}

// AddPointerInRelation creates and includes a BBcPointer object in BBcRelation
func AddPointerInRelation(relation *BBcRelation, refTransaction *BBcTransaction, refAssetID *[]byte) {
ptr := BBcPointer{}
relation.AddPointer(&ptr)
ptr.Add(&refTransaction.TransactionID, refAssetID)
}

// AddReference creates and includes a BBcReference object in a BBcTransaction object
func AddReference(transaction *BBcTransaction, assetGroupID *[]byte, refTransaction *BBcTransaction, eventIdx int) {
if transaction == nil || refTransaction == nil {
return
}
if refTransaction.TransactionID == nil {
refTransaction.Digest()
}
ref := BBcReference{}
transaction.AddReference(&ref)
ref.Add(assetGroupID, refTransaction, eventIdx)
}

// addInEvent is an internal function to add a BBcEvent object in a BBcTransaction object
func addInEvent(transaction *BBcTransaction, eventIdx int, assetGroupID, userID *[]byte) {
ast := BBcAsset{}
transaction.Events[eventIdx].Add(assetGroupID, &ast)
ast.Add(userID)
}

// AddEventAssetFile sets a file digest to a BBcAsset object in a BBcEvent object and then, add it in a BBcTransaction object
func AddEventAssetFile(transaction *BBcTransaction, eventIdx int, assetGroupID, userID *[]byte, assetFile *[]byte) {
if transaction == nil {
return
}
addInEvent(transaction, eventIdx, assetGroupID, userID)
if assetFile != nil {
transaction.Events[eventIdx].Asset.AddFile(assetFile)
}
}

// AddEventAssetBodyString sets a string to a BBcAsset object in a BBcEvent object and then, add it in a BBcTransaction object
func AddEventAssetBodyString(transaction *BBcTransaction, eventIdx int, assetGroupID, userID *[]byte, body string) {
if transaction == nil {
return
}
addInEvent(transaction, eventIdx, assetGroupID, userID)
if body != "" {
transaction.Events[eventIdx].Asset.AddBodyString(body)
}
}

// AddEventAssetBodyObject sets an object (map[string]interface{}) to a BBcAsset object in a BBcEvent object and then, add it in a BBcTransaction object
func AddEventAssetBodyObject(transaction *BBcTransaction, eventIdx int, assetGroupID, userID *[]byte, body interface{}) {
if transaction == nil {
return
}
addInEvent(transaction, eventIdx, assetGroupID, userID)
if body != "" {
_ = transaction.Events[eventIdx].Asset.AddBodyObject(body)
}
}

// MakeRelationWithAsset is a utility for making simple BBcTransaction object with BBcRelation with BBcAsset
func MakeRelationWithAsset(assetGroupID, userID *[]byte, assetBodyString string, assetBodyObject interface{}, assetFile *[]byte) *BBcRelation {
rtn := BBcRelation{}
rtn.SetIdLengthConf(&IdLengthConfig)
ast := BBcAsset{}
ast.SetIdLengthConf(&IdLengthConfig)
ast.Add(userID)
rtn.Add(assetGroupID, &ast)
if assetFile != nil {
ast.AddFile(assetFile)
}
if assetBodyString != "" {
ast.AddBodyString(assetBodyString)
} else if assetBodyObject != nil {
ast.AddBodyObject(assetBodyObject)
}
return &rtn
}

// SignToTransaction signs the transaction and append the BBcSignature object to it
func SignToTransaction(transaction *BBcTransaction, userID *[]byte, keypair *KeyPair) {
sig := BBcSignature{}
sig.SetPublicKeyByKeypair(keypair)
signature, _ := transaction.Sign(keypair)
sig.SetSignature(&signature)
transaction.AddSignature(userID, &sig)
}

0 comments on commit 6fca37d

Please sign in to comment.