-
Notifications
You must be signed in to change notification settings - Fork 211
/
tx.go
78 lines (64 loc) · 2.31 KB
/
tx.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
package wallet
import (
"bytes"
"github.com/oasisprotocol/curve25519-voi/primitives/ed25519"
"github.com/spacemeshos/go-scale"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/genvm/core"
"github.com/spacemeshos/go-spacemesh/genvm/sdk"
"github.com/spacemeshos/go-spacemesh/genvm/templates/wallet"
"github.com/spacemeshos/go-spacemesh/signing"
)
func encode(fields ...scale.Encodable) []byte {
buf := bytes.NewBuffer(nil)
encoder := scale.NewEncoder(buf)
for _, field := range fields {
_, err := field.EncodeScale(encoder)
if err != nil {
panic(err)
}
}
return buf.Bytes()
}
// SelfSpawn creates a self-spawn transaction.
func SelfSpawn(pk signing.PrivateKey, nonce core.Nonce, opts ...sdk.Opt) []byte {
args := wallet.SpawnArguments{}
copy(args.PublicKey[:], signing.Public(pk))
return Spawn(pk, wallet.TemplateAddress, &args, nonce, opts...)
}
// Spawn creates a spawn transaction.
func Spawn(pk signing.PrivateKey, template core.Address, args scale.Encodable, nonce core.Nonce, opts ...sdk.Opt) []byte {
options := sdk.Defaults()
for _, opt := range opts {
opt(options)
}
payload := core.Payload{}
payload.Nonce = nonce
payload.GasPrice = options.GasPrice
public := &core.PublicKey{}
copy(public[:], signing.Public(pk))
// note that principal is computed from pk
principal := core.ComputePrincipal(wallet.TemplateAddress, public)
tx := encode(&sdk.TxVersion, &principal, &sdk.MethodSpawn, &template, &payload, args)
sig := ed25519.Sign(ed25519.PrivateKey(pk), core.SigningBody(options.GenesisID[:], tx))
return append(tx, sig...)
}
// Spend creates spend transaction.
func Spend(pk signing.PrivateKey, to types.Address, amount uint64, nonce types.Nonce, opts ...sdk.Opt) []byte {
options := sdk.Defaults()
for _, opt := range opts {
opt(options)
}
spawnargs := wallet.SpawnArguments{}
copy(spawnargs.PublicKey[:], signing.Public(pk))
principal := core.ComputePrincipal(wallet.TemplateAddress, &spawnargs)
payload := core.Payload{}
payload.GasPrice = options.GasPrice
payload.Nonce = nonce
args := wallet.SpendArguments{}
args.Destination = to
args.Amount = amount
tx := encode(&sdk.TxVersion, &principal, &sdk.MethodSpend, &payload, &args)
sig := ed25519.Sign(ed25519.PrivateKey(pk), core.SigningBody(options.GenesisID[:], tx))
return append(tx, sig...)
}