forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
85 lines (71 loc) · 1.77 KB
/
builder.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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package spchainvm
import (
"errors"
"github.com/ava-labs/gecko/ids"
"github.com/ava-labs/gecko/utils/crypto"
"github.com/ava-labs/gecko/utils/hashing"
)
var (
errNilChainID = errors.New("nil chain id")
)
// Builder defines the functionality for building payment objects.
type Builder struct {
NetworkID uint32
ChainID ids.ID
}
// NewAccount creates a new Account
func (b Builder) NewAccount(id ids.ShortID, nonce, balance uint64) Account {
return Account{
id: id,
nonce: nonce,
balance: balance,
}
}
// NewBlock creates a new block
func (b Builder) NewBlock(parentID ids.ID, txs []*Tx) (*Block, error) {
block := &Block{
parentID: parentID,
txs: txs,
}
codec := Codec{}
bytes, err := codec.MarshalBlock(block)
if err != nil {
return nil, err
}
block.bytes = bytes
block.id = ids.NewID(hashing.ComputeHash256Array(block.bytes))
return block, nil
}
// NewTx creates a new transaction from [key|nonce] for [amount] to [destination]
func (b Builder) NewTx(key *crypto.PrivateKeySECP256K1R, nonce, amount uint64, destination ids.ShortID) (*Tx, error) {
if b.ChainID.IsZero() {
return nil, errNilChainID
}
tx := &Tx{
networkID: b.NetworkID,
chainID: b.ChainID,
nonce: nonce,
amount: amount,
to: destination,
verification: make(chan error, 1),
}
codec := Codec{}
unsignedBytes, err := codec.MarshalUnsignedTx(tx)
if err != nil {
return nil, err
}
sig, err := key.Sign(unsignedBytes)
if err != nil {
return nil, err
}
tx.sig = sig
bytes, err := codec.MarshalTx(tx)
if err != nil {
return nil, err
}
tx.bytes = bytes
tx.id = ids.NewID(hashing.ComputeHash256Array(tx.bytes))
return tx, nil
}