forked from gagliardetto/solana-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.go
204 lines (174 loc) · 5.56 KB
/
transaction.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
package solana
import (
"bytes"
"fmt"
"sort"
bin "github.com/dfuse-io/binary"
"go.uber.org/zap"
)
type Instruction interface {
Accounts() []*AccountMeta // returns the list of accounts the instructions requires
ProgramID() PublicKey // the programID the instruction acts on
Data() ([]byte, error) // the binary encoded instructions
}
type TransactionOption interface {
apply(opts *transactionOptions)
}
type transactionOptions struct {
payer PublicKey
}
type transactionOptionFunc func(opts *transactionOptions)
func (f transactionOptionFunc) apply(opts *transactionOptions) {
f(opts)
}
func TransactionPayer(payer PublicKey) TransactionOption {
return transactionOptionFunc(func(opts *transactionOptions) { opts.payer = payer })
}
func NewTransaction(instructions []Instruction, blockHash Hash, opts ...TransactionOption) (*Transaction, error) {
if len(instructions) == 0 {
return nil, fmt.Errorf("requires at-least one instruction to create a transaction")
}
options := transactionOptions{}
for _, opt := range opts {
opt.apply(&options)
}
feePayer := options.payer
if feePayer.IsZero() {
found := false
for _, act := range instructions[0].Accounts() {
if act.IsSigner {
feePayer = act.PublicKey
found = true
break
}
}
if !found {
return nil, fmt.Errorf("cannot determine fee payer. You can ether pass the fee payer vai the 'TransactionWithInstructions' option parameter or it fallback to the first instruction's first signer")
}
}
programIDs := map[PublicKey]bool{}
accounts := []*AccountMeta{}
for _, instruction := range instructions {
for _, key := range instruction.Accounts() {
accounts = append(accounts, key)
}
programIDs[instruction.ProgramID()] = true
}
// Add programID to the account list
for programID := range programIDs {
accounts = append(accounts, &AccountMeta{
PublicKey: programID,
IsSigner: false,
IsWritable: false,
})
}
// Sort. Prioritizing first by signer, then by writable
sort.Slice(accounts, func(i, j int) bool {
return accounts[i].less(accounts[j])
})
uniqAccountsMap := map[PublicKey]uint64{}
uniqAccounts := []*AccountMeta{}
for _, acc := range accounts {
if index, found := uniqAccountsMap[acc.PublicKey]; found {
uniqAccounts[index].IsWritable = uniqAccounts[index].IsWritable || acc.IsWritable
continue
}
uniqAccounts = append(uniqAccounts, acc)
uniqAccountsMap[acc.PublicKey] = uint64(len(uniqAccounts) - 1)
}
zlog.Debug("unique account sorted", zap.Int("account_count", len(uniqAccounts)))
// Move fee payer to the front
feePayerIndex := -1
for idx, acc := range uniqAccounts {
if acc.PublicKey.Equals(feePayer) {
feePayerIndex = idx
}
}
zlog.Debug("current fee payer index", zap.Int("fee_payer_index", feePayerIndex))
accountCount := len(uniqAccounts)
if feePayerIndex < 0 {
// fee payer is not part of accounts we want to add it
accountCount++
}
finalAccounts := make([]*AccountMeta, accountCount)
itr := 1
for idx, uniqAccount := range uniqAccounts {
if idx == feePayerIndex {
uniqAccount.IsSigner = true
uniqAccount.IsWritable = true
finalAccounts[0] = uniqAccount
continue
}
finalAccounts[itr] = uniqAccount
itr++
}
message := Message{
RecentBlockhash: blockHash,
}
accountKeyIndex := map[string]uint8{}
for idx, acc := range finalAccounts {
zlog.Debug("transaction account",
zap.Int("account_index", idx),
zap.Stringer("account_pub_key", acc.PublicKey),
)
message.AccountKeys = append(message.AccountKeys, acc.PublicKey)
accountKeyIndex[acc.PublicKey.String()] = uint8(idx)
if acc.IsSigner {
message.Header.NumRequiredSignatures++
if !acc.IsWritable {
message.Header.NumReadonlySignedAccounts++
}
continue
}
if !acc.IsWritable {
message.Header.NumReadonlyUnsignedAccounts++
}
}
zlog.Debug("message header compiled",
zap.Uint8("num_required_signatures", message.Header.NumRequiredSignatures),
zap.Uint8("num_readonly_signed_accounts", message.Header.NumReadonlySignedAccounts),
zap.Uint8("num_readonly_unsigned_accounts", message.Header.NumReadonlyUnsignedAccounts),
)
for trxIdx, instruction := range instructions {
accounts = instruction.Accounts()
accountIndex := make([]uint8, len(accounts))
for idx, acc := range accounts {
accountIndex[idx] = accountKeyIndex[acc.PublicKey.String()]
}
data, err := instruction.Data()
if err != nil {
return nil, fmt.Errorf("unable to encode instructions [%d]: %w", trxIdx, err)
}
message.Instructions = append(message.Instructions, CompiledInstruction{
ProgramIDIndex: accountKeyIndex[instruction.ProgramID().String()],
AccountCount: bin.Varuint16(uint16(len(accountIndex))),
Accounts: accountIndex,
DataLength: bin.Varuint16(uint16(len(data))),
Data: data,
})
}
return &Transaction{
Message: message,
}, nil
}
type privateKeyGetter func(key PublicKey) *PrivateKey
func (t *Transaction) Sign(getter privateKeyGetter) (out []Signature, err error) {
buf := new(bytes.Buffer)
if err = bin.NewEncoder(buf).Encode(t.Message); err != nil {
return nil, fmt.Errorf("unable to encode message for signing: %w", err)
}
messageCnt := buf.Bytes()
signerKeys := t.Message.signerKeys()
for _, key := range signerKeys {
privateKey := getter(key)
if privateKey == nil {
return nil, fmt.Errorf("signer key %q not found. Ensure all the signer keys are in the vault", key.String())
}
s, err := privateKey.Sign(messageCnt)
if err != nil {
return nil, fmt.Errorf("failed to signed with key %q: %w", key.String(), err)
}
t.Signatures = append(t.Signatures, s)
}
return t.Signatures, nil
}