Skip to content

Commit

Permalink
add sample code
Browse files Browse the repository at this point in the history
  • Loading branch information
quvox committed Dec 3, 2018
1 parent 37d6873 commit c462981
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 9 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ language: go
sudo: false
go:
- "1.10.x"
- tip
before_install:
- go get github.com/mattn/goveralls
- bash prepare.sh
Expand Down
44 changes: 36 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,48 @@ bbclib-go
[![Coverage Status](https://coveralls.io/repos/github/quvox/bbclib-go/badge.svg?branch=develop)](https://coveralls.io/github/quvox/bbclib-go?branch=develop)
[![Maintainability](https://api.codeclimate.com/v1/badges/0c523f5a3d71b77aad46/maintainability)](https://codeclimate.com/github/quvox/bbclib-go/maintainability)

Golang implementation of bbc1.core.bbclib and bbc1.core.libs modules in beyond-blockchain/bbc1
Golang implementation of bbc1.core.bbclib and bbc1.core.libs modules in https://github.com/beyond-blockchain/bbc1

### Features
* Support serializing/deserializing BBc-1 transaction object
* transaction version 1.2 or later
* Support sign/verify transaction
* Utility methods for creating transaction are not implemented
* Need to set information to struct BBcTransaction and its members directly
* Support most of features of bbclib in https://github.com/beyond-blockchain/bbc1
* BBc-1 version 1.2
* transaction header version 1 only
* Go v1.10 or later

### dependencies
* https://github.com/beyond-blockchain/libbbcsig

## Usage

## Install
```bash
go get -u github.com/quvox/bbclib-go
```

Building an external library is also required.
When "go get" is done, you will find github.com/quvox/bbclib-go/ directory in ${GOPATH}/src.
Then, execute the following commands:
```
cd ${GOPATH}/src/github.com/quvox/bbclib-go
bash prepare.sh
```

If you want to use this module in an AWS environment (EC2 or Lambda), do as follows:
```
cd ${GOPATH}/github.com/quvox/bbclib-go
bash prepare.sh aws
```
The preparation script (prepare.sh) produces libbbcsig.a, which is a static link library for signing/verifying a transaction.
Building libbbcsig.a takes long time, so be patient.

After finishing the compilation, you are ready for "go install".

```
go install github.com/quvox/bbclib-go
```

NOTE: [example/](./example) directory includes a sample code for this module. There are a document and a preparation script.

## Prepare for development (module itself)

For linux/mac
```
Expand All @@ -31,5 +59,5 @@ For Amazon Lambda, you need docker and do the following:
sh prepare.sh aws
```

After finishing prepare.sh script, you will find libbbcsig.dylib or libbbcsig.so in bbclib/.
After finishing prepare.sh script, you will find libbbcsig.a and libbbcsig.h, which are used by keypair.go for signing/verifying.

51 changes: 51 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
Sample application using bbclib-go
======

## install

### in the case of using "go get"

```
go get -u -d github.com/quvox/bbclib-go
pushd ${GOPATH}/src/github.com/quvox/bbclib-go
bash prepare.sh
popd
go install github.com/quvox/bbclib-go
```

The important point is adding "-d" in the first go get. This means downloads repository only. Before installing, libbbcsig.a needs to be built.

### in the case of vendoring through "dep"

```
dep init
pushd vendor/github.com/quvox/bbclib-go
bash prepare.sh
popd
go install github.com/quvox/bbclib-go
```

The difference between "go get" and "dep" is just a directory including libraries.

### Utility script

In the src/main directory, there is "prepare_libbbcsig.h" for building libbbcsig.a in the case of both "go get" and "dep".


## Build app

In src/main directory,

```
go run main.go
```

or

```
go build main.go
./main
```

then you will see the string output of a sample transaction.

54 changes: 54 additions & 0 deletions example/src/main/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"bytes"
"fmt"
"github.com/quvox/bbclib-go"
"io/ioutil"
)

func main() {
assetGroupID := bbclib.GetIdentifierWithTimestamp("assetGroupID", 32)
u1 := bbclib.GetIdentifierWithTimestamp("user1", 32)
u2 := bbclib.GetIdentifierWithTimestamp("user2", 32)
keypair1 := bbclib.GenerateKeypair(bbclib.KeyTypeEcdsaP256v1, 4)
keypair2 := bbclib.GenerateKeypair(bbclib.KeyTypeEcdsaSECP256k1, 4)

txobj := bbclib.MakeTransaction(3, 0, true, 32)
bbclib.AddEventAssetBodyString(txobj, 0, &assetGroupID, &u1, "teststring!!!!!")
txobj.Events[0].AddMandatoryApprover(&u1)
filedat, _ := ioutil.ReadFile("./asset_test.go")
bbclib.AddEventAssetFile(txobj, 1, &assetGroupID, &u2, &filedat)
txobj.Events[1].AddMandatoryApprover(&u2)
datobj := map[string]string{"param1": "aaa", "param2": "bbb", "param3": "ccc"}
bbclib.AddEventAssetBodyObject(txobj, 2, &assetGroupID, &u1, &datobj)
txobj.Events[2].AddMandatoryApprover(&u1)

txobj.Witness.AddWitness(&u1)
txobj.Witness.AddWitness(&u2)

bbclib.SignToTransaction(txobj, &u1, &keypair1)
bbclib.SignToTransaction(txobj, &u2, &keypair2)

fmt.Println("-------------transaction--------------")
fmt.Printf("%v", txobj.Stringer())
fmt.Println("--------------------------------------")

dat, err := txobj.Pack()
if err != nil {
fmt.Printf("failed to serialize transaction object (%v)", err)
}
fmt.Printf("Packed data: %x", dat)

obj2 := bbclib.BBcTransaction{}
obj2.Unpack(&dat)
obj2.Digest()
if result := obj2.Signatures[0].Verify(obj2.TransactionID); !result {
fmt.Println("Verification failed..")
}

if bytes.Compare(txobj.Events[0].Asset.AssetID, obj2.Events[0].Asset.AssetID) != 0 ||
bytes.Compare(txobj.TransactionID, obj2.TransactionID) != 0 {
fmt.Println("Not recovered correctly...")
}
}
24 changes: 24 additions & 0 deletions example/src/main/prepare_libbbcsig.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

REPO=github.com/quvox/bbclib-go

#go get -u -d ${REPO}

if [ -d ./vendor/${REPO} ]; then
WORKINGDIR=./vendor/${REPO}
elif [ -d ${GOPATH}/src/${REPO} ]; then
WORKINGDIR=${GOPATH}/src/${REPO}
else
echo "No bbclib-go repository found..."
exit 1
fi

cd ${WORKINGDIR}

if [ $# -eq 1 ] && [ $1 = "aws" ]; then
bash prepare.sh aws
else
bash prepare.sh
fi

# go install ${REPO}

0 comments on commit c462981

Please sign in to comment.