-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
147 lines (121 loc) · 3.73 KB
/
main_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package stellargo
import (
"bytes"
"encoding/base64"
"fmt"
"log"
"strings"
b "github.com/laxmicoinofficial/go/build"
"github.com/laxmicoinofficial/go/hash"
"github.com/laxmicoinofficial/go/keypair"
"github.com/laxmicoinofficial/go/xdr"
)
// ExampleDecodeTransaction shows the lowest-level process to decode a base64
// envelope encoded in base64.
func ExampleDecodeTransaction() {
data := "AAAAAGL8HQvQkbK2HA3WVjRrKmjX00fG8sLI7m0ERwJW/AX3AAAACgAAAAAAAAABAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAArqN6LeOagjxMaUP96Bzfs9e0corNZXzBWJkFoK7kvkwAAAAAO5rKAAAAAAAAAAABVvwF9wAAAEAKZ7IPj/46PuWU6ZOtyMosctNAkXRNX9WCAI5RnfRk+AyxDLoDZP/9l3NvsxQtWj9juQOuoBlFLnWu8intgxQA"
rawr := strings.NewReader(data)
b64r := base64.NewDecoder(base64.StdEncoding, rawr)
var tx xdr.TransactionEnvelope
bytesRead, err := xdr.Unmarshal(b64r, &tx)
fmt.Printf("read %d bytes\n", bytesRead)
if err != nil {
log.Fatal(err)
}
fmt.Printf("This tx has %d operations\n", len(tx.Tx.Operations))
// Output: read 192 bytes
// This tx has 1 operations
}
// ExampleBuildTransaction creates and signs a simple transaction using the
// build package. The build package is designed to make it easier and more
// intuitive to configure and sign a transaction.
func ExampleBuildTransaction() {
source := "SA26PHIKZM6CXDGR472SSGUQQRYXM6S437ZNHZGRM6QA4FOPLLLFRGDX"
tx, err := b.Transaction(
b.SourceAccount{source},
b.Sequence{1},
b.Payment(
b.Destination{"SBQHO2IMYKXAYJFCWGXC7YKLJD2EGDPSK3IUDHVJ6OOTTKLSCK6Z6POM"},
b.NativeAmount{"50.0"},
),
)
if err != nil {
panic(err)
}
txe, err := tx.Sign(source)
if err != nil {
panic(err)
}
txeB64, err := txe.Base64()
if err != nil {
panic(err)
}
fmt.Printf("tx base64: %s", txeB64)
}
// ExampleLowLevelTransaction creates and signs a simple transaction, and then
// encodes it into a hex string capable of being submitted to rover-core.
//
// It uses the low-level xdr facilities to create the transaction.
func ExampleLowLevelTransaction() {
skp := keypair.MustParse("SA26PHIKZM6CXDGR472SSGUQQRYXM6S437ZNHZGRM6QA4FOPLLLFRGDX")
dkp := keypair.MustParse("SBQHO2IMYKXAYJFCWGXC7YKLJD2EGDPSK3IUDHVJ6OOTTKLSCK6Z6POM")
asset, err := xdr.NewAsset(xdr.AssetTypeAssetTypeNative, nil)
if err != nil {
panic(err)
}
var destination xdr.AccountId
err = destination.SetAddress(dkp.Address())
if err != nil {
panic(err)
}
op := xdr.PaymentOp{
Destination: destination,
Asset: asset,
Amount: 50 * 10000000,
}
memo, err := xdr.NewMemo(xdr.MemoTypeMemoNone, nil)
var source xdr.AccountId
err = source.SetAddress(skp.Address())
if err != nil {
panic(err)
}
body, err := xdr.NewOperationBody(xdr.OperationTypePayment, op)
if err != nil {
panic(err)
}
tx := xdr.Transaction{
SourceAccount: source,
Fee: 10,
SeqNum: xdr.SequenceNumber(1),
Memo: memo,
Operations: []xdr.Operation{
{Body: body},
},
}
var txBytes bytes.Buffer
_, err = xdr.Marshal(&txBytes, tx)
if err != nil {
panic(err)
}
txHash := hash.Hash(txBytes.Bytes())
signature, err := skp.Sign(txHash[:])
if err != nil {
panic(err)
}
ds := xdr.DecoratedSignature{
Hint: skp.Hint(),
Signature: xdr.Signature(signature[:]),
}
txe := xdr.TransactionEnvelope{
Tx: tx,
Signatures: []xdr.DecoratedSignature{ds},
}
var txeBytes bytes.Buffer
_, err = xdr.Marshal(&txeBytes, txe)
if err != nil {
panic(err)
}
txeB64 := base64.StdEncoding.EncodeToString(txeBytes.Bytes())
fmt.Printf("tx base64: %s", txeB64)
// Output: tx base64: AAAAAAU08yUQ8sHqhY8j9mXWwERfHC/3cKFSe/spAr0rGtO2AAAACgAAAAAAAAABAAAAAAAAAAAAAAABAAAAAAAAAAEAAAAA+fnTe7/v4whpBUx96oj92jfZPz7S00l3O2xeyeqWIA0AAAAAAAAAAB3NZQAAAAAAAAAAASsa07YAAABAieruUIGcQH6RlQ+prYflPFU3nED2NvWhtaC+tgnKsqgiKURK4xo/W7EgH0+I6aQok52awbE+ksOxEQ5MLJ9eAw==
}