OSDN Git Service

Genesis (#127)
[bytom/bytom.git] / cmd / bytomcli / example / issue.go
1 package example
2
3 import (
4         "context"
5         "fmt"
6         "os"
7         "time"
8
9         "github.com/bytom/blockchain/query"
10         "github.com/bytom/blockchain/rpc"
11         "github.com/bytom/blockchain/txbuilder"
12         "github.com/bytom/crypto/ed25519/chainkd"
13         "github.com/bytom/encoding/json"
14         "github.com/bytom/config"
15
16         stdjson "encoding/json"
17         bchain "github.com/bytom/blockchain"
18 )
19
20 const (
21         Account = "account"
22         Asset   = "asset"
23 )
24
25 type Ins struct {
26         RootXPubs   []chainkd.XPub `json:"root_xpubs"`
27         Quorum      int
28         Alias       string
29         Tags        map[string]interface{}
30         Definition  map[string]interface{} `json:"omitempty"`
31         ClientToken string                 `json:"client_token"`
32 }
33
34 func NewInstance(alias, typ string) (Ins, chainkd.XPrv) {
35         xprv, _ := chainkd.NewXPrv(nil)
36         xpub := xprv.XPub()
37         fmt.Printf("type:%s,xprv:%v\n", typ, xprv)
38         fmt.Printf("type:%s,xpub:%v\n", typ, xpub)
39
40         var ins Ins
41         ins.RootXPubs = []chainkd.XPub{xpub}
42         ins.Quorum = 1
43         ins.Alias = alias
44         ins.Tags = map[string]interface{}{"test_tag": "v0"}
45         if typ == Asset {
46                 ins.Definition = map[string]interface{}{"test_definition": "v0"}
47         }
48         ins.ClientToken = typ
49
50         return ins, xprv
51 }
52
53 func NewAnnotate(client *rpc.Client, typ string, ins ...Ins) ([]query.AnnotatedAccount, []query.AnnotatedAsset) {
54         accounts := make([]query.AnnotatedAccount, 1)
55         assets := make([]query.AnnotatedAsset, 1)
56
57         if typ == Account {
58                 client.Call(context.Background(), "/create-account", &ins, &accounts)
59                 fmt.Printf("account:%v\n", accounts)
60                 return accounts, nil
61         } else if typ == Asset {
62                 client.Call(context.Background(), "/create-asset", &ins, &assets)
63                 fmt.Printf("assetid=%s\n", assets[0].ID.String())
64                 fmt.Printf("asset:%v\n", assets)
65                 return nil, assets
66         }
67
68         return nil, nil
69 }
70
71 func IssueTest(client *rpc.Client, args []string) {
72         // Create Account.
73         fmt.Printf("To create Account:\n")
74         aliceIns, _ := NewInstance("alice", Account)
75         bobIns, _ := NewInstance("bob", Account)
76         accounts, _ := NewAnnotate(client, Account, aliceIns, bobIns)
77
78         // Create Asset.
79         fmt.Printf("To create Asset:\n")
80         goldIns, xprvGold := NewInstance("gold", Asset)
81         _, assets := NewAnnotate(client, Asset, goldIns)
82
83         // Build Transaction.
84         fmt.Printf("To build transaction:\n")
85         buildReqFmt := `
86                 {"actions": [
87                         {
88                                 "type":"spend_account_unspent_output",
89                                 "receiver":null,
90                                 "output_id":"%v",
91                                 "reference_data":{}
92                         },
93                         {"type": "issue", "asset_id": "%s", "amount": 100},
94                         {"type": "control_account", "asset_id": "%s", "amount": 100, "account_id": "%s"},
95                         {"type": "control_account", "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "amount": 8888888888, "account_id": "%s"}
96                 ]}`
97         fmt.Printf("spend_account_unspent_output:%v\n", config.GenerateGenesisTx().ResultIds[0])
98         buildReqStr := fmt.Sprintf(buildReqFmt, config.GenerateGenesisTx().ResultIds[0], assets[0].ID.String(), assets[0].ID.String(), accounts[0].ID, accounts[0].ID)
99         var buildReq bchain.BuildRequest
100         err := stdjson.Unmarshal([]byte(buildReqStr), &buildReq)
101         if err != nil {
102                 fmt.Println(err)
103         }
104
105         tpl := make([]txbuilder.Template, 1)
106         client.Call(context.Background(), "/build-transaction", []*bchain.BuildRequest{&buildReq}, &tpl)
107         fmt.Printf("tpl:%v\n", tpl)
108
109         // sign-transaction
110         err = txbuilder.Sign(context.Background(), &tpl[0], []chainkd.XPub{xprvGold.XPub()}, "", func(_ context.Context, _ chainkd.XPub, path [][]byte, data [32]byte, _ string) ([]byte, error) {
111                 derived := xprvGold.Derive(path)
112                 return derived.Sign(data[:]), nil
113         })
114         if err != nil {
115                 fmt.Printf("sign-transaction error. err:%v\n", err)
116                 os.Exit(0)
117         }
118
119         fmt.Printf("sign tpl:%v\n", tpl[0])
120         fmt.Printf("sign tpl's SigningInstructions:%v\n", tpl[0].SigningInstructions[0])
121         fmt.Printf("SigningInstructions's SignatureWitnesses:%v\n", tpl[0].SigningInstructions[0].SignatureWitnesses[0])
122
123         // submit-transaction
124         var submitResponse interface{}
125         submitArg := bchain.SubmitArg{Transactions: tpl, Wait: json.Duration{Duration: time.Duration(1000000)}, WaitUntil: "none"}
126         client.Call(context.Background(), "/submit-transaction", submitArg, &submitResponse)
127         fmt.Printf("submit transaction:%v\n", submitResponse)
128         fmt.Println("==============test end===============")
129
130         //Issue result:
131         //alice <btm:8888888888,gold:100>
132 }