-
Notifications
You must be signed in to change notification settings - Fork 0
/
tezos.go
437 lines (366 loc) · 10.4 KB
/
tezos.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package tezos
import (
"context"
"errors"
"fmt"
"math/big"
"time"
ed25519hd "github.com/bitmark-inc/go-ed25519-hd"
"blockwatch.cc/tzgo/codec"
"blockwatch.cc/tzgo/contract"
"blockwatch.cc/tzgo/micheline"
"blockwatch.cc/tzgo/rpc"
"blockwatch.cc/tzgo/signer"
"blockwatch.cc/tzgo/tezos"
)
const (
DefaultAccountIndex = 0
DefaultSignPrefix = "Tezos Signed Message:"
)
var (
ErrWrongChainID = errors.New("Connected node serve different chain from setting")
ErrInvalidRpcNode = errors.New("Invalid rpc node")
ErrSignFailed = errors.New("Failed to sign with provided data")
ErrInvalidAddress = errors.New("Invalid address provided")
ErrInvalidTimestamp = errors.New("Invalid timestamp provided")
ErrInvalidPublicKey = errors.New("Invalid public key provided")
ErrInvalidSignature = errors.New("Invalid signature provided")
ErrInvalidTokenID = errors.New("Invalid tokenID provided")
ErrTransferAmountLowerThanSetFee = errors.New("Transfer amount lower than set fee")
ErrExceedSettingFee = errors.New("Actual cost is more than setting")
)
func buildDerivePath(index uint) string {
return fmt.Sprintf("m/44'/1729'/%d'/0'", index)
}
type Wallet struct {
chainID tezos.ChainIdHash
masterKey ed25519hd.PrivateKey
privateKey tezos.PrivateKey
accountIndex uint
rpcClient *rpc.Client
}
type TransferXTZParam struct {
To string
Amount int64
}
// NewWallet creates a tezos wallet from a given seed
func NewWallet(seed []byte, network string, rpcURL string) (*Wallet, error) {
pk, err := ed25519hd.GetMasterKeyFromSeed(seed)
if err != nil {
return nil, err
}
dpk, _ := pk.DeriveChildPrivateKey(buildDerivePath(DefaultAccountIndex))
key := toTzgoPrivateKey(*dpk)
c, err := rpc.NewClient(rpcURL, nil)
if err != nil {
return nil, err
}
// Set default signer to wallet private key
c.Signer = signer.NewFromKey(key)
if err := c.Init(context.Background()); err != nil {
return nil, ErrInvalidRpcNode
}
// chainID := GHOSTNETChainID
if network == "livenet" {
if !c.ChainId.Equal(tezos.DefaultParams.ChainId) {
return nil, ErrWrongChainID
}
}
return &Wallet{
chainID: c.ChainId,
masterKey: *pk,
privateKey: key,
accountIndex: DefaultAccountIndex,
rpcClient: c,
}, nil
}
// DeriveAccount derive the specific index account from the master key
func (w *Wallet) DeriveAccount(index uint) (*Wallet, error) {
dpk, err := w.masterKey.DeriveChildPrivateKey(buildDerivePath(index))
if err != nil {
return nil, err
}
key := toTzgoPrivateKey(*dpk)
rpc := w.rpcClient
rpc.Signer = signer.NewFromKey(key)
return &Wallet{
chainID: w.chainID,
masterKey: w.masterKey,
privateKey: key,
accountIndex: index,
rpcClient: rpc,
}, nil
}
// signMessage sign a specific message from privateKey
func (w *Wallet) signMessage(message []byte) (string, error) {
// force add prefix to message to prevent possible attack
m := append([]byte(DefaultSignPrefix), message...)
// pack the message to tezos bytes
mp := micheline.Prim{
Type: micheline.PrimBytes,
Bytes: m,
}
dm := tezos.Digest(mp.Pack())
sig, err := w.privateKey.Sign(dm[:])
if err != nil {
return "", ErrSignFailed
}
return sig.Generic(), nil
}
// SignAuthTransferMessage sign the authorized transfer message from privateKey
func (w *Wallet) SignAuthTransferMessage(to, contractAddress, tokenID string, expiry time.Time) (string, error) {
// timestamp
ts := big.NewInt(expiry.Unix())
// address
ad, err := tezos.ParseAddress(to)
if err != nil {
return "", ErrInvalidAddress
}
contractAddr, err := tezos.ParseAddress(contractAddress)
if err != nil {
return "", ErrInvalidAddress
}
// token
tk, ok := new(big.Int).SetString(tokenID, 10)
if !ok {
return "", ErrInvalidTokenID
}
tsp := micheline.Prim{
Type: micheline.PrimInt,
Int: ts,
}
ctp := micheline.Prim{
Type: micheline.PrimBytes,
Bytes: contractAddr.EncodePadded(),
}
adp := micheline.Prim{
Type: micheline.PrimBytes,
Bytes: ad.EncodePadded(),
}
tkp := micheline.Prim{
Type: micheline.PrimInt,
Int: tk,
}
m := append(append(append(tsp.Pack(), ctp.Pack()...), adp.Pack()...), tkp.Pack()...)
return w.signMessage(m)
}
// Send will send a op to tezos blockchain and return hash
func (w *Wallet) Send(args contract.CallArguments) (*string, error) {
opts := &rpc.CallOptions{
TTL: tezos.DefaultParams.MaxOperationsTTL - 2,
MaxFee: 10_000_000,
}
op := codec.NewOp().WithTTL(opts.TTL)
op.WithContents(args.Encode())
if w.chainID.Equal(tezos.GhostnetParams.ChainId) {
op.WithParams(tezos.GhostnetParams)
} else {
op.WithParams(tezos.DefaultParams)
}
return w.send(op, opts)
}
// SendOperations will send list of operations to tezos blockchain and return hash
func (w *Wallet) SendOperations(ops []codec.Operation) (*string, error) {
opts := &rpc.CallOptions{
TTL: tezos.DefaultParams.MaxOperationsTTL - 2,
MaxFee: 10_000_000,
}
op := codec.NewOp().WithTTL(opts.TTL)
for _, o := range ops {
op.WithContents(o)
}
if w.chainID.Equal(tezos.GhostnetParams.ChainId) {
op.WithParams(tezos.GhostnetParams)
} else {
op.WithParams(tezos.DefaultParams)
}
return w.send(op, opts)
}
func (w *Wallet) SimulateXTZTransferFee(txs []TransferXTZParam) (*int64, error) {
opts := &rpc.CallOptions{
TTL: tezos.DefaultParams.MaxOperationsTTL - 2,
}
op := codec.NewOp()
for _, tx := range txs {
ad, err := tezos.ParseAddress(tx.To)
if err != nil {
return nil, ErrInvalidAddress
}
// construct a transfer operation
op.WithTransfer(ad, tx.Amount)
}
ctx := context.Background()
signer := w.rpcClient.Signer
// identify the sender address for signing the message
addr := opts.Sender
if !addr.IsValid() {
addrs, err := signer.ListAddresses(ctx)
if err != nil {
return nil, err
}
addr = addrs[0]
}
key, err := signer.GetKey(ctx, addr)
if err != nil {
return nil, err
}
// set source on all ops
op.WithSource(key.Address())
// auto-complete op with branch/ttl, source counter, reveal
err = w.rpcClient.Complete(ctx, op, key)
if err != nil {
return nil, err
}
bufferFee := int64(0)
// simulate to estimate cost
sim, err := w.rpcClient.Simulate(ctx, op, opts)
if err != nil {
// FIXME: hack around way to get the estimate fee using the lowest transfer amount
// have to find out a better way like include the tzStats SDK to get the current balance of account
// we don't have a better way to get the est. fee for tezos in tzgo now if the transfer amount is almost(same) as the entire account balance
// New Op
op = codec.NewOp()
// construct a transfer operation with 0.000001 xtz to do brief fee estimation
for _, tx := range txs {
ad, err := tezos.ParseAddress(tx.To)
if err != nil {
return nil, ErrInvalidAddress
}
op.WithTransfer(ad, 1)
}
// set source on all ops
op.WithSource(key.Address())
// auto-complete op with branch/ttl, source counter, reveal
err = w.rpcClient.Complete(ctx, op, key)
if err != nil {
return nil, err
}
// simulate to estimate cost
sim, err = w.rpcClient.Simulate(ctx, op, opts)
if err != nil {
return nil, err
}
bufferFee = int64(1000)
}
op.WithLimits(sim.MinLimits(), rpc.GasSafetyMargin)
c := sim.TotalCosts()
tc := c.Burn + op.Limits().Fee + bufferFee
return &tc, nil
}
// send is a convenience wrapper for sending operations. It auto-completes gas and storage limit,
// ensures minimum fees are set, protects against fee overpayment, signs and broadcasts the final
// operation.
func (w *Wallet) send(op *codec.Op, opts *rpc.CallOptions) (*string, error) {
ctx := context.Background()
if opts == nil {
opts = &rpc.DefaultOptions
}
signer := w.rpcClient.Signer
if opts.Signer != nil {
signer = opts.Signer
}
// identify the sender address for signing the message
addr := opts.Sender
if !addr.IsValid() {
addrs, err := signer.ListAddresses(ctx)
if err != nil {
return nil, err
}
addr = addrs[0]
}
key, err := signer.GetKey(ctx, addr)
if err != nil {
return nil, err
}
// set source on all ops
op.WithSource(key.Address())
// auto-complete op with branch/ttl, source counter, reveal
err = w.rpcClient.Complete(ctx, op, key)
if err != nil {
return nil, err
}
// simulate to check tx validity and estimate cost
sim, err := w.rpcClient.Simulate(ctx, op, opts)
if err != nil {
return nil, err
}
// fail with Tezos error when simulation failed
if !sim.IsSuccess() {
return nil, sim.Error()
}
// apply simulated cost as limits to tx list
if !opts.IgnoreLimits {
op.WithLimits(sim.MinLimits(), rpc.GasSafetyMargin)
}
// check minFee calc against maxFee if set
if opts.MaxFee > 0 {
if l := op.Limits(); l.Fee > opts.MaxFee {
return nil, fmt.Errorf("estimated cost %d > max %d", l.Fee, opts.MaxFee)
}
}
// sign digest
sig, err := signer.SignOperation(ctx, addr, op)
if err != nil {
return nil, err
}
op.WithSignature(sig)
// broadcast
hash, err := w.rpcClient.Broadcast(ctx, op)
if err != nil {
return nil, err
}
h := hash.String()
return &h, nil
}
// RPCClient returns the Tezos RPC client which is bound to the wallet
func (w *Wallet) RPCClient() *rpc.Client {
return w.rpcClient
}
// Account returns the tezos account address string
func (w *Wallet) Account() string {
return w.privateKey.Address().String()
}
// ChainID returns the tezos wallet ChainID
func (w *Wallet) ChainID() string {
return w.chainID.String()
}
// Account returns the private key
func (w *Wallet) PrivateKey() tezos.PrivateKey {
return w.privateKey
}
// TransferXTZ transfer the xtz to destination
func (w *Wallet) TransferXTZ(to string, amount int64) (*string, error) {
return w.BatchTransferXTZ(
[]TransferXTZParam{
{
To: to,
Amount: amount,
},
},
)
}
// BatchTransferXTZ transfer the xtz to destinations
func (w *Wallet) BatchTransferXTZ(txs []TransferXTZParam) (*string, error) {
opts := &rpc.CallOptions{
TTL: tezos.DefaultParams.MaxOperationsTTL - 2,
MaxFee: 1_000_000,
}
op := codec.NewOp()
for _, tx := range txs {
ad, err := tezos.ParseAddress(tx.To)
if err != nil {
return nil, ErrInvalidAddress
}
// construct a transfer operation
op.WithTransfer(ad, tx.Amount)
}
return w.send(op, opts)
}
// convert an ed25519 hd private key to tzgo private key
func toTzgoPrivateKey(edk ed25519hd.PrivateKey) tezos.PrivateKey {
key := tezos.PrivateKey{
Type: tezos.KeyTypeEd25519,
}
key.Data = append(edk.Key, edk.GetPublicKey()...)
return key
}