@alephium/ledger-app 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,10 @@
1
1
  import SpeculosTransport from '@ledgerhq/hw-transport-node-speculos'
2
2
  import AlephiumApp, { GROUP_NUM } from '../src'
3
- import blake from 'blakejs'
4
3
  import fetch from 'node-fetch'
5
- import { groupOfAddress, transactionVerifySignature } from '@alephium/web3'
4
+ import { ALPH_TOKEN_ID, Address, KeyType, NodeProvider, ONE_ALPH, binToHex, codec, groupOfAddress, node, transactionVerifySignature, waitForTxConfirmation, web3 } from '@alephium/web3'
5
+ import { getSigner, mintToken, transfer } from '@alephium/web3-test'
6
+ import { PrivateKeyWallet } from '@alephium/web3-wallet'
7
+ import blake from 'blakejs'
6
8
 
7
9
  function sleep(ms) {
8
10
  return new Promise((resolve) => setTimeout(resolve, ms))
@@ -24,6 +26,8 @@ function getRandomInt(min, max) {
24
26
 
25
27
  describe('sdk', () => {
26
28
  const apduPort = 9999
29
+ const nodeProvider = new NodeProvider("http://127.0.0.1:22973")
30
+ web3.setCurrentNodeProvider(nodeProvider)
27
31
  let pathIndex: number
28
32
  let path: string
29
33
 
@@ -36,7 +40,7 @@ describe('sdk', () => {
36
40
  const transport = await SpeculosTransport.open({ apduPort })
37
41
  const app = new AlephiumApp(transport)
38
42
  const version = await app.getVersion()
39
- expect(version).toBe('0.1.0')
43
+ expect(version).toBe('0.2.0')
40
44
  await app.close()
41
45
  })
42
46
 
@@ -52,60 +56,367 @@ describe('sdk', () => {
52
56
  it('should get public key for group', async () => {
53
57
  const transport = await SpeculosTransport.open({ apduPort })
54
58
  const app = new AlephiumApp(transport)
55
- Array(GROUP_NUM).forEach(async (_, group) => {
59
+ for (let group = 0; group < GROUP_NUM; group++) {
56
60
  const [account, hdIndex] = await app.getAccount(path, group)
57
61
  expect(hdIndex >= pathIndex).toBe(true)
58
62
  expect(groupOfAddress(account.address)).toBe(group)
59
63
  expect(account.keyType).toBe('default')
60
- })
64
+ }
61
65
  await app.close()
62
66
  })
63
67
 
64
68
  it('should get public key for group for Schnorr signature', async () => {
65
69
  const transport = await SpeculosTransport.open({ apduPort })
66
70
  const app = new AlephiumApp(transport)
67
- Array(GROUP_NUM).forEach(async (_, group) => {
71
+ for (let group = 0; group < GROUP_NUM; group++) {
68
72
  await expect(app.getAccount(path, group, 'bip340-schnorr')).rejects.toThrow('BIP340-Schnorr is not supported yet')
69
- })
73
+ }
70
74
  await app.close()
71
75
  })
72
76
 
73
- it('should sign hash', async () => {
77
+ async function transferToAddress(address: Address, amount: bigint = ONE_ALPH * 10n) {
78
+ const balance0 = await getALPHBalance(address)
79
+ const fromAccount = await getSigner()
80
+ const transferResult = await transfer(fromAccount, address, ALPH_TOKEN_ID, amount)
81
+ await waitForTxConfirmation(transferResult.txId, 1, 1000)
82
+ const balance1 = await getALPHBalance(address)
83
+ expect(balance1 - balance0).toEqual(amount)
84
+ }
85
+
86
+ async function getALPHBalance(address: Address) {
87
+ const balances = await nodeProvider.addresses.getAddressesAddressBalance(address)
88
+ return BigInt(balances.balance)
89
+ }
90
+
91
+ async function clickAndApprove(times: number) {
92
+ for (let i = 0; i < times; i++) {
93
+ await pressButton('right')
94
+ }
95
+ await pressButton('both')
96
+ }
97
+
98
+ it('should transfer alph to p2pkh address', async () => {
74
99
  const transport = await SpeculosTransport.open({ apduPort })
75
100
  const app = new AlephiumApp(transport)
101
+ const [testAccount] = await app.getAccount(path)
102
+ await transferToAddress(testAccount.address)
76
103
 
77
- const [account] = await app.getAccount(path)
78
- console.log(account)
104
+ const buildTxResult = await nodeProvider.transactions.postTransactionsBuild({
105
+ fromPublicKey: testAccount.publicKey,
106
+ destinations: [
107
+ {
108
+ address: '1BmVCLrjttchZMW7i6df7mTdCKzHpy38bgDbVL1GqV6P7',
109
+ attoAlphAmount: (ONE_ALPH * 2n).toString(),
110
+ },
111
+ {
112
+ address: '1BmVCLrjttchZMW7i6df7mTdCKzHpy38bgDbVL1GqV6P7',
113
+ attoAlphAmount: (ONE_ALPH * 3n).toString(),
114
+ },
115
+ ]
116
+ })
117
+
118
+ function approve(index: number) {
119
+ if (index >= 7) return
120
+ if (index >= 3) { // outputs and signature
121
+ setTimeout(async () => {
122
+ await clickAndApprove(6)
123
+ approve(index + 1)
124
+ }, 1000)
125
+ } else {
126
+ setTimeout(async () => {
127
+ await clickAndApprove(3)
128
+ approve(index + 1)
129
+ }, 1000)
130
+ }
131
+ }
132
+
133
+ approve(0)
134
+ const signature = await app.signUnsignedTx(path, Buffer.from(buildTxResult.unsignedTx, 'hex'))
135
+ expect(transactionVerifySignature(buildTxResult.txId, testAccount.publicKey, signature)).toBe(true)
136
+
137
+ const submitResult = await nodeProvider.transactions.postTransactionsSubmit({
138
+ unsignedTx: buildTxResult.unsignedTx,
139
+ signature: signature
140
+ })
141
+ await waitForTxConfirmation(submitResult.txId, 1, 1000)
142
+ const balance1 = await getALPHBalance(testAccount.address)
143
+ expect(balance1 < (ONE_ALPH * 5n)).toEqual(true)
79
144
 
80
- const hash = Buffer.from(blake.blake2b(Buffer.from([0, 1, 2, 3, 4]), undefined, 32))
81
- setTimeout(async () => {
82
- await pressButton('both') // review message
83
- await pressButton('both') // done review
84
- await pressButton('right') // select signing
85
- await pressButton('both') // done selection
86
- }, 1000)
87
- const signature = await app.signHash(path, hash)
88
- console.log(signature)
89
145
  await app.close()
146
+ }, 120000)
90
147
 
91
- expect(transactionVerifySignature(hash.toString('hex'), account.publicKey, signature)).toBe(true)
92
- }, 10000)
148
+ it('should transfer alph to multisig address', async () => {
149
+ const transport = await SpeculosTransport.open({ apduPort })
150
+ const app = new AlephiumApp(transport)
151
+ const [testAccount] = await app.getAccount(path)
152
+ await transferToAddress(testAccount.address)
93
153
 
94
- it('should reject signing', async () => {
154
+ const multiSigAddress = 'X3KYVteDjsKuUP1F68Nv9iEUecnnkMuwjbC985AnA6MvciDFJ5bAUEso2Sd7sGrwZ5rfNLj7Rp4n9XjcyzDiZsrPxfhNkPYcDm3ce8pQ9QasNFByEufMi3QJ3cS9Vk6cTpqNcq';
155
+ const buildTxResult = await nodeProvider.transactions.postTransactionsBuild({
156
+ fromPublicKey: testAccount.publicKey,
157
+ destinations: [
158
+ {
159
+ address: multiSigAddress,
160
+ attoAlphAmount: (ONE_ALPH * 2n).toString(),
161
+ },
162
+ {
163
+ address: multiSigAddress,
164
+ attoAlphAmount: (ONE_ALPH * 3n).toString(),
165
+ },
166
+ ]
167
+ })
168
+
169
+ function approve(index: number) {
170
+ if (index >= 7) return
171
+ if (index == 3 || index == 4) { // multi-sig outputs
172
+ setTimeout(async () => {
173
+ await clickAndApprove(11)
174
+ approve(index + 1)
175
+ }, 1000)
176
+ } else if (index > 4) { // change output and signature
177
+ setTimeout(async () => {
178
+ await clickAndApprove(6)
179
+ approve(index + 1)
180
+ }, 1000)
181
+ } else {
182
+ setTimeout(async () => {
183
+ await clickAndApprove(3)
184
+ approve(index + 1)
185
+ }, 1000)
186
+ }
187
+ }
188
+
189
+ approve(0);
190
+ const signature = await app.signUnsignedTx(path, Buffer.from(buildTxResult.unsignedTx, 'hex'))
191
+ expect(transactionVerifySignature(buildTxResult.txId, testAccount.publicKey, signature)).toBe(true)
192
+
193
+ const submitResult = await nodeProvider.transactions.postTransactionsSubmit({
194
+ unsignedTx: buildTxResult.unsignedTx,
195
+ signature: signature
196
+ })
197
+ await waitForTxConfirmation(submitResult.txId, 1, 1000)
198
+ const balance1 = await getALPHBalance(testAccount.address)
199
+ expect(balance1 < (ONE_ALPH * 5n)).toEqual(true)
200
+
201
+ await app.close()
202
+ }, 120000)
203
+
204
+ it('should transfer token to multisig address', async () => {
95
205
  const transport = await SpeculosTransport.open({ apduPort })
96
206
  const app = new AlephiumApp(transport)
207
+ const [testAccount] = await app.getAccount(path)
208
+ await transferToAddress(testAccount.address)
97
209
 
98
- const [account] = await app.getAccount(path)
99
- console.log(account)
210
+ const tokenInfo = await mintToken(testAccount.address, 2222222222222222222222222n);
211
+
212
+ const multiSigAddress = 'X3KYVteDjsKuUP1F68Nv9iEUecnnkMuwjbC985AnA6MvciDFJ5bAUEso2Sd7sGrwZ5rfNLj7Rp4n9XjcyzDiZsrPxfhNkPYcDm3ce8pQ9QasNFByEufMi3QJ3cS9Vk6cTpqNcq';
213
+ const buildTxResult = await nodeProvider.transactions.postTransactionsBuild({
214
+ fromPublicKey: testAccount.publicKey,
215
+ destinations: [
216
+ {
217
+ address: multiSigAddress,
218
+ attoAlphAmount: (ONE_ALPH * 5n).toString(),
219
+ tokens: [
220
+ {
221
+ id: tokenInfo.contractId,
222
+ amount: "1111111111111111111111111",
223
+ }
224
+ ]
225
+ }
226
+ ]
227
+ })
228
+
229
+ function approve(index: number) {
230
+ if (index > 7) return
231
+ if (index <= 2) {
232
+ setTimeout(async () => {
233
+ await clickAndApprove(3)
234
+ approve(index + 1)
235
+ }, 1000)
236
+ } else if (index === 3) { // multi-sig token output
237
+ setTimeout(async () => {
238
+ await clickAndApprove(17)
239
+ approve(index + 1)
240
+ }, 1000)
241
+ } else if (index === 4) { // multi-sig alph output
242
+ setTimeout(async () => {
243
+ await clickAndApprove(11)
244
+ approve(index + 1)
245
+ }, 1000)
246
+ } else if (index === 5) { // token change output
247
+ setTimeout(async () => {
248
+ await clickAndApprove(12)
249
+ approve(index + 1)
250
+ }, 1000)
251
+ } else if (index >= 6) { // alph change output and signature
252
+ setTimeout(async () => {
253
+ await clickAndApprove(6)
254
+ approve(index + 1)
255
+ }, 1000)
256
+ }
257
+ }
258
+
259
+ approve(0);
260
+ const signature = await app.signUnsignedTx(path, Buffer.from(buildTxResult.unsignedTx, 'hex'))
261
+ expect(transactionVerifySignature(buildTxResult.txId, testAccount.publicKey, signature)).toBe(true)
262
+
263
+ const submitResult = await nodeProvider.transactions.postTransactionsSubmit({
264
+ unsignedTx: buildTxResult.unsignedTx,
265
+ signature: signature
266
+ })
267
+ await waitForTxConfirmation(submitResult.txId, 1, 1000)
268
+ const balances = await nodeProvider.addresses.getAddressesAddressBalance(testAccount.address)
269
+ const alphBalance = BigInt(balances.balance)
270
+ expect(alphBalance < (ONE_ALPH * 5n)).toEqual(true)
271
+
272
+ expect(balances.tokenBalances!.length).toEqual(1)
273
+ const token = balances.tokenBalances![0]
274
+ expect(token.id).toEqual(tokenInfo.contractId)
275
+ expect(token.amount).toEqual('1111111111111111111111111')
276
+
277
+ await app.close()
278
+ }, 120000)
279
+
280
+ it('should transfer from multiple inputs', async () => {
281
+ const transport = await SpeculosTransport.open({ apduPort })
282
+ const app = new AlephiumApp(transport)
283
+ const [testAccount] = await app.getAccount(path)
284
+ for (let i = 0; i < 20; i += 1) {
285
+ await transferToAddress(testAccount.address, ONE_ALPH)
286
+ }
287
+
288
+ const buildTxResult = await nodeProvider.transactions.postTransactionsBuild({
289
+ fromPublicKey: testAccount.publicKey,
290
+ destinations: [
291
+ {
292
+ address: '1BmVCLrjttchZMW7i6df7mTdCKzHpy38bgDbVL1GqV6P7',
293
+ attoAlphAmount: (ONE_ALPH * 19n).toString(),
294
+ }
295
+ ]
296
+ })
297
+
298
+ function approve(index: number) {
299
+ if (index >= 6) return
300
+ if (index >= 3) { // outputs and signature
301
+ setTimeout(async () => {
302
+ await clickAndApprove(6)
303
+ approve(index + 1)
304
+ }, 1000)
305
+ } else {
306
+ setTimeout(async () => {
307
+ await clickAndApprove(3)
308
+ approve(index + 1)
309
+ }, 1000)
310
+ }
311
+ }
312
+
313
+ approve(0)
314
+ const signature = await app.signUnsignedTx(path, Buffer.from(buildTxResult.unsignedTx, 'hex'))
315
+ expect(transactionVerifySignature(buildTxResult.txId, testAccount.publicKey, signature)).toBe(true)
316
+
317
+ const submitResult = await nodeProvider.transactions.postTransactionsSubmit({
318
+ unsignedTx: buildTxResult.unsignedTx,
319
+ signature: signature
320
+ })
321
+ await waitForTxConfirmation(submitResult.txId, 1, 1000)
322
+ const balance = await getALPHBalance(testAccount.address)
323
+ expect(balance < (ONE_ALPH * 1n)).toEqual(true)
324
+
325
+ await app.close()
326
+ }, 120000)
327
+
328
+ function getAccount(groupIndex: number): { account: PrivateKeyWallet, unlockScript: string } {
329
+ const useDefaultKeyType = Math.random() >= 0.5
330
+ if (useDefaultKeyType) {
331
+ const account = PrivateKeyWallet.Random(groupIndex)
332
+ return { account, unlockScript: '00' + account.publicKey }
333
+ }
334
+
335
+ const account = PrivateKeyWallet.Random(groupIndex, nodeProvider, 'bip340-schnorr')
336
+ const unlockScript = '02' + `0101000000000458144020${account.publicKey}8685` + '00'
337
+ return { account, unlockScript }
338
+ }
339
+
340
+ it('should transfer from different input addresses', async () => {
341
+ const transport = await SpeculosTransport.open({ apduPort })
342
+ const app = new AlephiumApp(transport)
343
+ const [testAccount] = await app.getAccount(path)
344
+ const { account: newAccount, unlockScript: unlockScript0 } = getAccount(testAccount.group)
345
+ for (let i = 0; i < 2; i += 1) {
346
+ await transferToAddress(testAccount.address, ONE_ALPH)
347
+ await transferToAddress(newAccount.address, ONE_ALPH)
348
+ }
349
+
350
+ const utxos0 = await nodeProvider.addresses.getAddressesAddressUtxos(newAccount.address)
351
+ expect(utxos0.utxos.length).toEqual(2)
352
+ const utxos1 = await nodeProvider.addresses.getAddressesAddressUtxos(testAccount.address)
353
+ expect(utxos1.utxos.length).toEqual(2)
354
+
355
+ const useSameAsPrevious = Math.random() >= 0.5
356
+ const inputs0: node.AssetInput[] = utxos0.utxos.map((utxo, index) => {
357
+ const unlockScript = index > 0 && useSameAsPrevious ? '03' : unlockScript0
358
+ return { outputRef: utxo.ref, unlockScript }
359
+ })
360
+ const unlockScript1 = '00' + testAccount.publicKey
361
+ const inputs1: node.AssetInput[] = utxos1.utxos.map((utxo, index) => {
362
+ const unlockScript = index > 0 && useSameAsPrevious ? '03' : unlockScript1
363
+ return { outputRef: utxo.ref, unlockScript }
364
+ })
365
+ const unsignedTx: node.UnsignedTx = {
366
+ txId: '',
367
+ version: 0,
368
+ networkId: 4,
369
+ gasAmount: 100000,
370
+ gasPrice: (ONE_ALPH / 100000n).toString(),
371
+ inputs: inputs0.concat(inputs1),
372
+ fixedOutputs: [{
373
+ hint: 0,
374
+ key: '',
375
+ attoAlphAmount: (ONE_ALPH * 3n).toString(),
376
+ address: testAccount.address,
377
+ tokens: [],
378
+ lockTime: 0,
379
+ message: ''
380
+ }]
381
+ }
382
+ const txBytes = codec.unsignedTxCodec.encodeApiUnsignedTx(unsignedTx)
383
+ const signResult0 = await newAccount.signUnsignedTx({
384
+ signerAddress: newAccount.address,
385
+ unsignedTx: binToHex(txBytes)
386
+ })
387
+
388
+ function approve(index: number) {
389
+ if (index > 6) return
390
+ if (index === 3 || index === 4) { // inputs
391
+ setTimeout(async () => {
392
+ await clickAndApprove(5)
393
+ approve(index + 1)
394
+ }, 1000)
395
+ } else if (index >= 5) { // outputs and tx id
396
+ setTimeout(async () => {
397
+ await clickAndApprove(6)
398
+ approve(index + 1)
399
+ }, 1000)
400
+ } else {
401
+ setTimeout(async () => {
402
+ await clickAndApprove(3)
403
+ approve(index + 1)
404
+ }, 1000)
405
+ }
406
+ }
407
+
408
+ approve(0)
409
+ const signature1 = await app.signUnsignedTx(path, Buffer.from(txBytes))
410
+ expect(transactionVerifySignature(signResult0.txId, testAccount.publicKey, signature1)).toBe(true)
411
+
412
+ const submitResult = await nodeProvider.multisig.postMultisigSubmit({
413
+ unsignedTx: binToHex(txBytes),
414
+ signatures: [signResult0.signature, signature1]
415
+ })
416
+ await waitForTxConfirmation(submitResult.txId, 1, 1000)
417
+ const balance = await getALPHBalance(testAccount.address)
418
+ expect(balance).toEqual(ONE_ALPH * 3n)
100
419
 
101
- const hash = Buffer.from(blake.blake2b(Buffer.from([0, 1, 2, 3, 4]), undefined, 32))
102
- setTimeout(async () => {
103
- await pressButton('both') // review message
104
- await pressButton('both') // done review
105
- await pressButton('left') // select signing
106
- await pressButton('both') // done selection
107
- }, 1000)
108
- await expect(app.signHash(path, hash)).rejects.toThrow()
109
420
  await app.close()
110
- }, 10000)
421
+ }, 120000)
111
422
  })