-
Notifications
You must be signed in to change notification settings - Fork 217
/
example_trx_pack_test.go
90 lines (68 loc) · 2.67 KB
/
example_trx_pack_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package eos_test
import (
"bytes"
"context"
"encoding/hex"
"fmt"
eos "github.com/eoscanada/eos-go"
)
func ExamplePackedTransaction_Pack() {
api := eos.New(getAPIURL())
// Fills in the transaction header information like ref block, delays and expiration is set to 30s
txOpts := &eos.TxOptions{}
err := txOpts.FillFromChain(context.Background(), api)
// The actual account here instead of `eosio` must be the the account that has the public key
// associated with the private key below configure for 'active' level.
from := "eosio"
// This is of course now a burnt key, never ever use it, it's for demo purpose and protecting your key is your responsibility
fromPrivateKey := "PVT_K1_2i6s2S8cxJw33zFuF3keAfUJjKSUJ53qVH7ac4veCuPVCpUSp"
transferPermissionLevel, err := eos.NewPermissionLevel(from + "@active")
NoError(err, "parse permission level")
transferQuantity, err := eos.NewAssetFromString("10.0000 EOS")
NoError(err, "parse asset")
trx := eos.NewTransaction([]*eos.Action{
{
Account: "eosio.token",
Name: "transfer",
Authorization: []eos.PermissionLevel{transferPermissionLevel},
ActionData: eos.NewActionData(&eos.Transfer{
From: eos.AccountName(from),
To: "eosio.token",
Quantity: transferQuantity,
Memo: "Example action",
}),
},
}, txOpts)
keyBag := eos.NewKeyBag()
err = keyBag.Add(fromPrivateKey)
NoError(err, "add key to bag")
keyBagKeys, err := keyBag.AvailableKeys(context.Background())
NoError(err, "key bag available keys")
Ensure(len(keyBagKeys) == 1, "expected a single available key")
mainnetChainID := decodeChainID("aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906")
signerKey := keyBagKeys[0]
signedTrx, err := keyBag.Sign(context.Background(), eos.NewSignedTransaction(trx), mainnetChainID, signerKey)
NoError(err, "sign transaction")
packedTrx, err := signedTrx.Pack(eos.CompressionNone)
NoError(err, "pack transaction")
trxID, err := packedTrx.ID()
NoError(err, "transaction id")
fmt.Printf("Encode transaction: %s\n", encode(trx))
fmt.Println()
fmt.Printf("Encode signed transaction: %s\n", encode(signedTrx))
fmt.Println()
fmt.Printf("Encode packed and signed transaction ID %s: %s\n", trxID, encode(packedTrx))
fmt.Println()
}
func encode(v interface{}) string {
buffer := bytes.NewBuffer(nil)
err := eos.NewEncoder(buffer).Encode(v)
NoError(err, "encode %T", v)
return hex.EncodeToString(buffer.Bytes())
}
func decodeChainID(in string) eos.Checksum256 {
data, err := hex.DecodeString(in)
NoError(err, "chain ID %q is not a valid checksum256 value", in)
Ensure(len(data) == 32, "invalid checksum256, must have 32 bytes got %d", len(data))
return data
}