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"
16 stdjson "encoding/json"
17 bchain "github.com/bytom/blockchain"
26 RootXPubs []chainkd.XPub `json:"root_xpubs"`
29 Tags map[string]interface{}
30 Definition map[string]interface{} `json:"omitempty"`
31 ClientToken string `json:"client_token"`
34 func NewInstance(alias, typ string) (Ins, chainkd.XPrv) {
35 xprv, _ := chainkd.NewXPrv(nil)
37 fmt.Printf("type:%s,xprv:%v\n", typ, xprv)
38 fmt.Printf("type:%s,xpub:%v\n", typ, xpub)
41 ins.RootXPubs = []chainkd.XPub{xpub}
44 ins.Tags = map[string]interface{}{"test_tag": "v0"}
46 ins.Definition = map[string]interface{}{"test_definition": "v0"}
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)
58 client.Call(context.Background(), "/create-account", &ins, &accounts)
59 fmt.Printf("account:%v\n", accounts)
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)
71 func IssueTest(client *rpc.Client, args []string) {
73 fmt.Printf("To create Account:\n")
74 aliceIns, _ := NewInstance("alice", Account)
75 bobIns, _ := NewInstance("bob", Account)
76 accounts, _ := NewAnnotate(client, Account, aliceIns, bobIns)
79 fmt.Printf("To create Asset:\n")
80 goldIns, xprvGold := NewInstance("gold", Asset)
81 _, assets := NewAnnotate(client, Asset, goldIns)
84 fmt.Printf("To build transaction:\n")
88 "type":"spend_account_unspent_output",
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"}
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)
105 tpl := make([]txbuilder.Template, 1)
106 client.Call(context.Background(), "/build-transaction", []*bchain.BuildRequest{&buildReq}, &tpl)
107 fmt.Printf("tpl:%v\n", tpl)
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
115 fmt.Printf("sign-transaction error. err:%v\n", err)
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])
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===============")
131 //alice <btm:8888888888,gold:100>