forked from akash-network/node
/
builder.go
103 lines (87 loc) · 2.67 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package txutil
import (
"fmt"
"github.com/gogo/protobuf/proto"
"github.com/ovrclk/akash/types"
crypto "github.com/tendermint/tendermint/crypto"
camino "github.com/tendermint/tendermint/crypto/encoding/amino"
)
type TxBuilder interface {
SignableTx
Signature() []byte
TxBytes() ([]byte, error)
}
func BuildTx(signer Signer, nonce uint64, payload interface{}) ([]byte, error) {
txb, err := NewTxBuilder(nonce, payload)
if err != nil {
return nil, err
}
if err := signer.Sign(txb); err != nil {
return nil, err
}
return txb.TxBytes()
}
func NewTxBuilder(nonce uint64, payload interface{}) (TxBuilder, error) {
tx := &types.Tx{}
switch payload := payload.(type) {
case *types.TxSend:
tx.Payload.Payload = &types.TxPayload_TxSend{TxSend: payload}
case *types.TxCreateDeployment:
tx.Payload.Payload = &types.TxPayload_TxCreateDeployment{TxCreateDeployment: payload}
case *types.TxUpdateDeployment:
tx.Payload.Payload = &types.TxPayload_TxUpdateDeployment{TxUpdateDeployment: payload}
case *types.TxCreateProvider:
tx.Payload.Payload = &types.TxPayload_TxCreateProvider{TxCreateProvider: payload}
case *types.TxCreateOrder:
tx.Payload.Payload = &types.TxPayload_TxCreateOrder{TxCreateOrder: payload}
case *types.TxCreateFulfillment:
tx.Payload.Payload = &types.TxPayload_TxCreateFulfillment{TxCreateFulfillment: payload}
case *types.TxCreateLease:
tx.Payload.Payload = &types.TxPayload_TxCreateLease{TxCreateLease: payload}
case *types.TxCloseDeployment:
tx.Payload.Payload = &types.TxPayload_TxCloseDeployment{TxCloseDeployment: payload}
case *types.TxCloseFulfillment:
tx.Payload.Payload = &types.TxPayload_TxCloseFulfillment{TxCloseFulfillment: payload}
case *types.TxCloseLease:
tx.Payload.Payload = &types.TxPayload_TxCloseLease{TxCloseLease: payload}
default:
return nil, fmt.Errorf("unknown payload type: %T", payload)
}
tx.Payload.Nonce = nonce
pbytes, err := proto.Marshal(&tx.Payload)
if err != nil {
return nil, err
}
return &txBuilder{tx, pbytes}, nil
}
type txBuilder struct {
tx *types.Tx
pbytes []byte
}
func (b *txBuilder) SignBytes() []byte {
return b.pbytes
}
func (b *txBuilder) Sign(key crypto.PubKey, sig []byte) error {
if b.tx.Key != nil || b.tx.Signature != nil {
return fmt.Errorf("already signed")
}
b.tx.Key = key.Bytes()
b.tx.Signature = sig
return nil
}
func (b *txBuilder) Signers() ([]crypto.PubKey, error) {
if b.tx.Key == nil {
return nil, nil
}
key, err := camino.PubKeyFromBytes(b.tx.Key)
if err != nil {
return nil, err
}
return []crypto.PubKey{key}, nil
}
func (b *txBuilder) Signature() []byte {
return b.tx.Signature
}
func (b *txBuilder) TxBytes() ([]byte, error) {
return proto.Marshal(b.tx)
}