-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolana_test.go
More file actions
240 lines (208 loc) · 6.77 KB
/
Copy pathsolana_test.go
File metadata and controls
240 lines (208 loc) · 6.77 KB
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
package outscript_test
import (
"bytes"
"crypto/ed25519"
"encoding/hex"
"testing"
"github.com/KarpelesLab/outscript"
)
func TestSolanaAddress(t *testing.T) {
key := ed25519.NewKeyFromSeed(must(hex.DecodeString("20a1c9d559159085c82ae54e35f332a2d54aab952dd5832c42d06fb0548d5f88")))
s := outscript.New(key.Public())
sout, err := s.Out("solana")
if err != nil {
t.Fatalf("failed to generate solana out: %s", err)
}
addr, err := sout.Address("solana")
if err != nil {
t.Fatalf("failed to generate solana address: %s", err)
}
// Round-trip: parse the address and verify it matches.
parsed, err := outscript.ParseSolanaAddress(addr)
if err != nil {
t.Fatalf("failed to parse solana address %s: %s", addr, err)
}
if parsed.Script != sout.Script {
t.Errorf("round-trip failed: scripts differ: %s != %s", parsed.Script, sout.Script)
}
// Verify Hash() returns the raw bytes.
h := sout.Hash()
if h == nil {
t.Error("Hash() returned nil for solana out")
}
if len(h) != 32 {
t.Errorf("Hash() returned %d bytes, expected 32", len(h))
}
}
func TestSolanaAddressParse(t *testing.T) {
// A well-known Solana address (System Program)
addr := "11111111111111111111111111111111"
out, err := outscript.ParseSolanaAddress(addr)
if err != nil {
t.Fatalf("failed to parse system program address: %s", err)
}
roundTrip, err := out.Address("solana")
if err != nil {
t.Fatalf("failed to encode address: %s", err)
}
if roundTrip != addr {
t.Errorf("round-trip mismatch: %s != %s", roundTrip, addr)
}
// Invalid: too short
_, err = outscript.ParseSolanaAddress("abc")
if err == nil {
t.Error("expected error for short address, got nil")
}
// Invalid: not base58
_, err = outscript.ParseSolanaAddress("0000000000000000000000000000000O") // O is not in base58
if err == nil {
t.Error("expected error for invalid base58, got nil")
}
}
func TestSolanaCompactU16(t *testing.T) {
// We test compact-u16 indirectly via message serialization round-trips,
// but let's also build a transaction with specific values to verify encoding.
feePayer := must(outscript.ParseSolanaKey("11111111111111111111111111111111"))
blockhash := must(outscript.ParseSolanaKey("11111111111111111111111111111111"))
tx, err := outscript.NewSolanaTx(feePayer, blockhash)
if err != nil {
t.Fatalf("NewSolanaTx failed: %s", err)
}
data, err := tx.MarshalBinary()
if err != nil {
t.Fatalf("marshal failed: %s", err)
}
var tx2 outscript.SolanaTx
err = tx2.UnmarshalBinary(data)
if err != nil {
t.Fatalf("unmarshal failed: %s", err)
}
if len(tx2.Message.AccountKeys) != len(tx.Message.AccountKeys) {
t.Errorf("account keys count mismatch: %d != %d", len(tx2.Message.AccountKeys), len(tx.Message.AccountKeys))
}
}
func TestSolanaTxTransfer(t *testing.T) {
seed := must(hex.DecodeString("20a1c9d559159085c82ae54e35f332a2d54aab952dd5832c42d06fb0548d5f88"))
key := ed25519.NewKeyFromSeed(seed)
pub := key.Public().(ed25519.PublicKey)
var from outscript.SolanaKey
copy(from[:], pub)
to := must(outscript.ParseSolanaKey("83astBRguLMdt2h5U1Tpdq5tjFoJ6noeGwaY3mDLVcri"))
blockhash := must(outscript.ParseSolanaKey("EETubP5AKHgjPAhzPkA6E6HPBj7HtchdMWv2SzTqiYsC"))
ix := outscript.SolanaTransferInstruction(from, to, 1000000)
tx, err := outscript.NewSolanaTx(from, blockhash, ix)
if err != nil {
t.Fatalf("NewSolanaTx failed: %s", err)
}
// Verify structure before signing.
if tx.Message.Header.NumRequiredSignatures != 1 {
t.Errorf("expected 1 signer, got %d", tx.Message.Header.NumRequiredSignatures)
}
// from (signer+writable), to (writable), system program (readonly) = 3 accounts
if len(tx.Message.AccountKeys) != 3 {
t.Errorf("expected 3 account keys, got %d", len(tx.Message.AccountKeys))
}
if tx.Message.AccountKeys[0] != from {
t.Error("fee payer should be first account")
}
// Sign
err = tx.Sign(key)
if err != nil {
t.Fatalf("sign failed: %s", err)
}
// Hash should be the signature
h, err := tx.Hash()
if err != nil {
t.Fatalf("hash failed: %s", err)
}
if len(h) != 64 {
t.Errorf("expected 64-byte hash, got %d", len(h))
}
if !bytes.Equal(h, tx.Signatures[0]) {
t.Error("hash should equal the first signature")
}
// Marshal
data, err := tx.MarshalBinary()
if err != nil {
t.Fatalf("marshal failed: %s", err)
}
// Verify the serialized data is non-trivial
if len(data) < 100 {
t.Errorf("serialized tx seems too short: %d bytes", len(data))
}
}
func TestSolanaTxRoundTrip(t *testing.T) {
seed := must(hex.DecodeString("20a1c9d559159085c82ae54e35f332a2d54aab952dd5832c42d06fb0548d5f88"))
key := ed25519.NewKeyFromSeed(seed)
pub := key.Public().(ed25519.PublicKey)
var from outscript.SolanaKey
copy(from[:], pub)
to := must(outscript.ParseSolanaKey("83astBRguLMdt2h5U1Tpdq5tjFoJ6noeGwaY3mDLVcri"))
blockhash := must(outscript.ParseSolanaKey("EETubP5AKHgjPAhzPkA6E6HPBj7HtchdMWv2SzTqiYsC"))
ix := outscript.SolanaTransferInstruction(from, to, 500000)
tx, err := outscript.NewSolanaTx(from, blockhash, ix)
if err != nil {
t.Fatalf("NewSolanaTx failed: %s", err)
}
err = tx.Sign(key)
if err != nil {
t.Fatalf("sign failed: %s", err)
}
data, err := tx.MarshalBinary()
if err != nil {
t.Fatalf("marshal failed: %s", err)
}
var tx2 outscript.SolanaTx
err = tx2.UnmarshalBinary(data)
if err != nil {
t.Fatalf("unmarshal failed: %s", err)
}
// Verify signatures match
if len(tx2.Signatures) != len(tx.Signatures) {
t.Fatalf("signature count mismatch: %d != %d", len(tx2.Signatures), len(tx.Signatures))
}
for i := range tx.Signatures {
if !bytes.Equal(tx.Signatures[i], tx2.Signatures[i]) {
t.Errorf("signature %d mismatch", i)
}
}
// Verify message fields
if tx2.Message.Header != tx.Message.Header {
t.Error("header mismatch")
}
if len(tx2.Message.AccountKeys) != len(tx.Message.AccountKeys) {
t.Fatalf("account keys count mismatch")
}
for i := range tx.Message.AccountKeys {
if tx2.Message.AccountKeys[i] != tx.Message.AccountKeys[i] {
t.Errorf("account key %d mismatch", i)
}
}
if tx2.Message.RecentBlockhash != tx.Message.RecentBlockhash {
t.Error("blockhash mismatch")
}
if len(tx2.Message.Instructions) != len(tx.Message.Instructions) {
t.Fatalf("instruction count mismatch")
}
for i := range tx.Message.Instructions {
ix1 := tx.Message.Instructions[i]
ix2 := tx2.Message.Instructions[i]
if ix1.ProgramIDIndex != ix2.ProgramIDIndex {
t.Errorf("instruction %d program index mismatch", i)
}
if !bytes.Equal(ix1.AccountIndices, ix2.AccountIndices) {
t.Errorf("instruction %d account indices mismatch", i)
}
if !bytes.Equal(ix1.Data, ix2.Data) {
t.Errorf("instruction %d data mismatch", i)
}
}
// Re-marshal and verify byte-for-byte equality
data2, err := tx2.MarshalBinary()
if err != nil {
t.Fatalf("re-marshal failed: %s", err)
}
if !bytes.Equal(data, data2) {
t.Error("re-serialized bytes differ from original")
}
}