@exodus/bitcoin-plugin 1.2.0 → 1.4.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.
Files changed (43) hide show
  1. package/jest.config.js +7 -0
  2. package/package.json +3 -4
  3. package/src/__tests__/address.test.js +142 -0
  4. package/src/__tests__/balances.test.js +67 -0
  5. package/src/__tests__/bitcoin-private-seed-safe-report.json +2754 -0
  6. package/src/__tests__/bitcoin-signing.test.js +184 -0
  7. package/src/__tests__/fee-data.test.js +61 -0
  8. package/src/__tests__/fee-estimator.test.js +174 -0
  9. package/src/__tests__/fixtures/segwit-all-unsigned-tx.js +87 -0
  10. package/src/__tests__/fixtures/segwit-legacy-change-unsigned-tx.js +38 -0
  11. package/src/__tests__/fixtures/segwit-segwit-change-unsigned-tx.js +96 -0
  12. package/src/__tests__/fixtures/taproot-15000sats-unsigned-tx.js +149 -0
  13. package/src/__tests__/fixtures/taproot-all-unsigned-tx.js +231 -0
  14. package/src/__tests__/fixtures/taproot-unsigned-tx.js +85 -0
  15. package/src/__tests__/get-size.test.js +70 -0
  16. package/src/__tests__/index.test.js +65 -0
  17. package/src/__tests__/keys.test.js +97 -0
  18. package/src/__tests__/monitor.integration.test.js +170 -0
  19. package/src/__tests__/move-funds.test.js +408 -0
  20. package/src/__tests__/parse-unsigned-tx.test.js +46 -0
  21. package/src/__tests__/reports/unit-test-safe-report.json +2790 -0
  22. package/src/__tests__/send-tx.test.js +145 -0
  23. package/src/__tests__/sign-tx.test.js +32 -0
  24. package/src/__tests__/unsigned-btclike-desktop.json +40 -0
  25. package/src/__tests__/wallet.test.js +78 -0
  26. package/src/create-asset.js +70 -7
  27. package/src/ordinals/__tests__/brc-20-client.integration.test.js +86 -0
  28. package/src/ordinals/__tests__/brc-20-utils.test.js +165 -0
  29. package/src/ordinals/__tests__/fixtures/inscriptions-send-usigned-tx.js +125 -0
  30. package/src/ordinals/__tests__/fixtures/nft-send-usigned-tx.js +97 -0
  31. package/src/ordinals/__tests__/monitor-ordinals-enabled-safe-report-with-inscriptions.json +2908 -0
  32. package/src/ordinals/__tests__/monitor-ordinals-enabled-safe-report.json +2906 -0
  33. package/src/ordinals/__tests__/monitor-ordinals-enabled.integration.test.js +16 -0
  34. package/src/ordinals/__tests__/nfts-api.test.js +221 -0
  35. package/src/ordinals/__tests__/ordinals-send-tx.test.js +103 -0
  36. package/src/ordinals/brc-20-balances-and-fee-adapter.js +41 -0
  37. package/src/ordinals/brc-20-utils.js +79 -0
  38. package/src/ordinals/create-token-factory.js +30 -0
  39. package/src/ordinals/get-inscription-addresses.js +42 -0
  40. package/src/ordinals/nfts.js +65 -0
  41. package/src/ordinals/ordinals-account-state-utils.js +55 -0
  42. package/src/ordinals/ordinals-constants.js +4 -0
  43. package/src/ordinals/reload-brc20-token-balances.js +154 -0
@@ -0,0 +1,97 @@
1
+ import { createAsset } from '../index'
2
+
3
+ import { runVectorTests } from '@exodus/assets-testing'
4
+ const asset = createAsset()
5
+
6
+ const vectors = {
7
+ encodePrivate: [
8
+ [
9
+ '_0x021289b7ad4577f8c9d01c16e16c8bd08b3e702d678dea65c1b312f4ef87b89fa1',
10
+ true,
11
+ 'KwHjqYApDWbWjG5uvv9cnYiDbAXJdV6sDpZc3jHiEvD3LTfs2vje',
12
+ ],
13
+ [
14
+ '_0x021289b7ad4577f8c9d01c16e16c8bd08b3e702d678dea65c1b312f4ef87b89fa1',
15
+ false,
16
+ '5HqCX2dD4Hg5vXc6ebFBpvrfeeW4zf5xLDb82yhwf3YGwMJQN31',
17
+ ],
18
+ ],
19
+
20
+ encodePublic: [
21
+ [
22
+ '_0x021289b7ad4577f8c9d01c16e16c8bd08b3e702d678dea65c1b312f4ef87b89fa1',
23
+ { purpose: 44 },
24
+ '1KS3LDX6BfkD1GGVDiVuB5WsER1SLPyGcc',
25
+ ],
26
+ [
27
+ '_0x000289b7ad4577f8c9d01c16e16c8bd08b3e702d678dea65c1b312f4ef87b89fa1',
28
+ { purpose: 44 },
29
+ '1DSrT5njrFWjz5cvZKpueYfm6mdao3wDxZ',
30
+ ],
31
+ [
32
+ '_0x021289b7ad4577f8c9d01c16e16c8bd08b3e702d678dea65c1b312f4ef87b89fa1',
33
+ { purpose: 49 },
34
+ '3FhD2njgrovQXt5AJGDWLQnkRqX1yAnsBy',
35
+ ],
36
+ [
37
+ '_0x021289b7ad4577f8c9d01c16e16c8bd08b3e702d678dea65c1b312f4ef87b89fa1',
38
+ { purpose: 84 },
39
+ 'bc1qeghfha5yjp3ytf322zhsknyj44rudupan6jrm2',
40
+ ],
41
+ [
42
+ '_0x000289b7ad4577f8c9d01c16e16c8bd08b3e702d678dea65c1b312f4ef87b89fa1',
43
+ { purpose: 84 },
44
+ 'bc1q3zzjlh0s763276sgfyh0u8d3w5th37x8xxctjn',
45
+ ],
46
+ [
47
+ '_0x021289b7ad4577f8c9d01c16e16c8bd08b3e702d678dea65c1b312f4ef87b89fa1',
48
+ { purpose: 86 },
49
+ 'bc1phcnqwv23hvfhz6z5lqkxk6tpxh77z2ne06f389mzulp7ap303prsruvgha',
50
+ ],
51
+ [
52
+ '_0x000289b7ad4577f8c9d01c16e16c8bd08b3e702d678dea65c1b312f4ef87b89fa1',
53
+ { purpose: 86 },
54
+ 'bc1pcpqut9w2zperwm7y3s9uegddrm0008ch9rz8grpuw0megg2cv9wqa2lwgv',
55
+ ],
56
+ [
57
+ '_0x04d0de0aaeaefad02b8bdc8a01a1b8b11c696bd3d66a2c5f10780d95b7df42645cd85228a6fb29940e858e7e55842ae2bd115d1ed7cc0e82d934e929c97648cb0a',
58
+ { purpose: 44 },
59
+ '1GAehh7TsJAHuUAeKZcXf5CnwuGuGgyX2S',
60
+ ],
61
+ [
62
+ '_0x04d0de0aaeaefad02b8bdc8a01a1b8b11c696bd3d66a2c5f10780d95b7df42645cd85228a6fb29940e858e7e55842ae2bd115d1ed7cc0e82d934e929c97648cb0a',
63
+ { purpose: 49 },
64
+ '3D9iyFHi1Zs9KoyynUfrL82rGhJfYTfSG4',
65
+ ],
66
+ [
67
+ '_0x04d0de0aaeaefad02b8bdc8a01a1b8b11c696bd3d66a2c5f10780d95b7df42645cd85228a6fb29940e858e7e55842ae2bd115d1ed7cc0e82d934e929c97648cb0a',
68
+ { purpose: 84 },
69
+ 'bc1q5ew35guafmrxvepax5x8hw8ugnfgsyfgvh0ntr',
70
+ ],
71
+ [
72
+ '_0x04d0de0aaeaefad02b8bdc8a01a1b8b11c696bd3d66a2c5f10780d95b7df42645cd85228a6fb29940e858e7e55842ae2bd115d1ed7cc0e82d934e929c97648cb0a',
73
+ { purpose: 86 },
74
+ 'bc1pdj78vjhv4wukfzfu3qyvwclcewrsyfq8fyx2gvs8s850smsgw0yq8ykfa8',
75
+ ],
76
+ ],
77
+ encodePublicFromWIF: [
78
+ ['5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ', '1GAehh7TsJAHuUAeKZcXf5CnwuGuGgyX2S'],
79
+ ['5Kb8kLf9zgWQnogidDA76MzPL6TsZZY36hWXMssSzNydYXYB9KF', '1CC3X2gu58d6wXUWMffpuzN9JAfTUWu4Kj'],
80
+ ],
81
+
82
+ encodePublicBech32FromWIF: [
83
+ [
84
+ '5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ',
85
+ 'bc1q5ew35guafmrxvepax5x8hw8ugnfgsyfgvh0ntr',
86
+ ],
87
+ [
88
+ '5Kb8kLf9zgWQnogidDA76MzPL6TsZZY36hWXMssSzNydYXYB9KF',
89
+ 'bc1q0tqql9ul7r0jlh9k2asaeru7lzehzskm4z2sm6',
90
+ ],
91
+ ],
92
+ }
93
+
94
+ const api = `keys`
95
+ describe(`${asset.name} ${api} vector tests`, () => {
96
+ runVectorTests({ asset, vectors, api })
97
+ })
@@ -0,0 +1,170 @@
1
+ import { createAsset } from '../index'
2
+ import { UtxoCollection } from '@exodus/models'
3
+ import { mapValues } from 'lodash'
4
+
5
+ import { getPrivateSeed, monitorIntegrationTester } from '@exodus/assets-testing'
6
+ import * as path from 'path'
7
+ import {
8
+ collectResults,
9
+ collectUnusedAddressIndexes,
10
+ } from '@exodus/bitcoin-api/src/__tests__/bitcoin-testing-utils'
11
+ const asset = createAsset()
12
+ const expectedResults = {
13
+ exodus_0: {
14
+ balance: '0.0011 BTC',
15
+ chains: [
16
+ {
17
+ chain: [1, 0],
18
+ purpose: 44,
19
+ },
20
+ {
21
+ chain: [3, 16],
22
+ purpose: 84,
23
+ },
24
+ {
25
+ chain: [1, 0],
26
+ purpose: 86,
27
+ },
28
+ ],
29
+ txs: {
30
+ size: 49,
31
+ },
32
+ utxos: {
33
+ addresses: ['bc1qz0p68ljq4xslgl09g8rdfzjse7dshm4w6jsh48'],
34
+ size: 2,
35
+ },
36
+ },
37
+ exodus_1: {
38
+ balance: '0.01228053 BTC',
39
+ chains: [
40
+ {
41
+ chain: [2, 0],
42
+ purpose: 44,
43
+ },
44
+ {
45
+ chain: [11, 4],
46
+ purpose: 84,
47
+ },
48
+ {
49
+ chain: [3, 0],
50
+ purpose: 86,
51
+ },
52
+ ],
53
+ txs: {
54
+ size: 28,
55
+ },
56
+ utxos: {
57
+ addresses: [
58
+ 'bc1qgxz38crda20mlre427ygezgu67kqvqfr4heayn',
59
+ 'bc1qux2sjzksvgzwu2wdlvgtm8ggfp0azwff53yar6',
60
+ 'bc1qpk5g380fe5u0tfd8gz9h3lsga402q49kf2c5ny',
61
+ ],
62
+ size: 3,
63
+ },
64
+ },
65
+ exodus_2: {
66
+ balance: '0 BTC',
67
+ chains: [
68
+ {
69
+ chain: [0, 0],
70
+ purpose: 44,
71
+ },
72
+ {
73
+ chain: [0, 0],
74
+ purpose: 84,
75
+ },
76
+ {
77
+ chain: [0, 0],
78
+ purpose: 86,
79
+ },
80
+ ],
81
+ txs: {
82
+ size: 0,
83
+ },
84
+ utxos: {
85
+ addresses: [],
86
+ size: 0,
87
+ },
88
+ },
89
+ exodus_3: {
90
+ balance: '0 BTC',
91
+ chains: [
92
+ {
93
+ chain: [1, 0],
94
+ purpose: 44,
95
+ },
96
+ {
97
+ chain: [1, 0],
98
+ purpose: 84,
99
+ },
100
+ {
101
+ chain: [0, 0],
102
+ purpose: 86,
103
+ },
104
+ ],
105
+ txs: {
106
+ size: 12,
107
+ },
108
+ utxos: {
109
+ addresses: [],
110
+ size: 0,
111
+ },
112
+ },
113
+ }
114
+ const safeReportFile = path.join(__dirname, 'bitcoin-private-seed-safe-report.json')
115
+ const walletAccountCount = 4
116
+ describe(`bitcoin monitor integration test`, () => {
117
+ monitorIntegrationTester({
118
+ asset,
119
+ seed: getPrivateSeed(),
120
+ walletAccountCount,
121
+ safeReportFile,
122
+ asserter: async ({ wallet, asset }) => {
123
+ // assertion for Fernando's dev wallet.
124
+ // To enable in CI:
125
+ // Extract addresses from seed rather than ofc, shared the seed
126
+ // Create generic enough assertions, no specific numbers
127
+
128
+ const aci = wallet.assetClientInterface
129
+
130
+ const results = await collectResults({ asset, aci })
131
+ expect(results).toEqual(expectedResults)
132
+
133
+ // BE strategy, resolving unusedAddressIndexes from the TXLOG
134
+ const unusedAddressIndexes = await collectUnusedAddressIndexes({
135
+ asset,
136
+ addressesProvider: wallet.addressesProvider,
137
+ walletAccounts: wallet.walletAccounts,
138
+ })
139
+
140
+ // MOBILE strategy, resolving in monitor and saving unusedAddressIndexes in storage
141
+ const expectedUnusedAddressIndexes = mapValues(expectedResults, (result) => result.chains)
142
+ // Because there is no gap, both are the same.
143
+ expect(unusedAddressIndexes).toEqual(expectedUnusedAddressIndexes)
144
+ },
145
+ })
146
+ })
147
+
148
+ describe(`bitcoin monitor integration test: refresh fetches missing utxos!`, () => {
149
+ monitorIntegrationTester({
150
+ asset,
151
+ seed: getPrivateSeed(),
152
+ walletAccountCount,
153
+ asserter: async ({ wallet, monitor }) => {
154
+ const aci = wallet.assetClientInterface
155
+
156
+ expect(await collectResults({ asset, aci })).toEqual(expectedResults)
157
+
158
+ await aci.updateAccountState({
159
+ assetName: 'bitcoin',
160
+ walletAccount: `exodus_0`,
161
+ newData: { utxos: UtxoCollection.createEmpty({ currency: asset.currency }) },
162
+ })
163
+ // await monitor.stop()
164
+ // await monitor.start({ refresh: true })
165
+ // update with refresh waits for current ticks to finish rather than skip it
166
+ await monitor.update({ refresh: true })
167
+ expect(await collectResults({ asset, aci })).toEqual(expectedResults)
168
+ },
169
+ })
170
+ })
@@ -0,0 +1,408 @@
1
+ import { createAsset } from '../index'
2
+
3
+ import { getPrivateSeed, walletTester } from '@exodus/assets-testing'
4
+ import path from 'path'
5
+ import { getSchnorrEntropy } from '@exodus/bitcoin-api/src/tx-sign/default-entropy'
6
+ const asset = createAsset()
7
+
8
+ jest.mock('@exodus/bitcoin-api/src/tx-sign/default-entropy')
9
+
10
+ getSchnorrEntropy.mockImplementation(() => {
11
+ return Buffer.from('1230000000000000000000000000000000000000000000000000000000000000', 'hex')
12
+ })
13
+
14
+ const importSafeReportFile = path.join(__dirname, 'reports/unit-test-safe-report.json')
15
+
16
+ const assetName = asset.name
17
+ describe(`bitcoin move funds`, () => {
18
+ afterEach(() => {
19
+ jest.restoreAllMocks()
20
+ })
21
+
22
+ class MoveFundsError extends Error {
23
+ constructor(message, extra) {
24
+ super(message)
25
+ this.extra = extra
26
+ }
27
+ }
28
+
29
+ walletTester({
30
+ asset,
31
+ seed: getPrivateSeed(),
32
+ importSafeReportFile,
33
+ walletAccountCount: 4,
34
+ tests: {
35
+ 'can move from segwit to legacy': async ({ asset, assetClientInterface, getPrivateKey }) => {
36
+ const toAddress = '1Azpg7QEdQ8FHhNLzEpbpo1Uowh3t7B3Ex'
37
+
38
+ const { privateKey, address: fromAddress } = await getPrivateKey({
39
+ asset,
40
+ purpose: 84,
41
+ accountIndex: 1,
42
+ chainIndex: 1,
43
+ addressIndex: 2,
44
+ })
45
+
46
+ const insightClient = asset.insightClient
47
+ const accountIndex = 1
48
+
49
+ const rawUtxos = [
50
+ {
51
+ address: fromAddress,
52
+ txId: '71cbb7e880b66a2fafecf7feac8dfbd59151ddc785ae6ee81bf9297a91c9d32e',
53
+ confirmations: 6048,
54
+ value: 0.1,
55
+ vout: 0,
56
+ height: 765218,
57
+ script: '001491223b6a34a067a8ba0a8d3127090cd64a1b97b9',
58
+ asset: undefined,
59
+ },
60
+ ]
61
+
62
+ jest.spyOn(insightClient, 'broadcastTx').mockImplementation(() => true)
63
+ jest.spyOn(insightClient, 'fetchUTXOs').mockImplementation(([address]) => {
64
+ if (address === fromAddress) {
65
+ return rawUtxos
66
+ }
67
+ return []
68
+ })
69
+
70
+ const prepareResult = await asset.api.moveFunds.prepareSendFundsTx({
71
+ assetName,
72
+ input: privateKey,
73
+ assetClientInterface,
74
+ toAddress,
75
+ walletAccount: `exodus_${accountIndex}`,
76
+ MoveFundsError,
77
+ })
78
+ expect(Object.keys(prepareResult)).toEqual([
79
+ 'fromAddress',
80
+ 'toAddress',
81
+ 'amount',
82
+ 'fee',
83
+ 'utxos',
84
+ 'unsignedTx',
85
+ 'sizeKB',
86
+ 'privateKey',
87
+ ])
88
+
89
+ expect(prepareResult.fromAddress).toEqual(fromAddress)
90
+ expect(prepareResult.toAddress).toEqual(toAddress)
91
+ expect(prepareResult.fee.toBaseString({ unit: true })).toEqual('8296 satoshis')
92
+ expect(prepareResult.amount.toBaseString({ unit: true })).toEqual('9991704 satoshis')
93
+ expect(prepareResult.utxos.value.toBaseString({ unit: true })).toEqual('10000000 satoshis')
94
+ expect(prepareResult.utxos.value.sub(prepareResult.fee)).toEqual(prepareResult.amount)
95
+ expect(prepareResult.utxos.size).toEqual(rawUtxos.length)
96
+ expect(prepareResult.privateKey).toEqual(privateKey)
97
+ expect(prepareResult.sizeKB).toEqual(0.122)
98
+
99
+ const sendFundsResult = await asset.api.moveFunds.sendFunds({
100
+ assetName,
101
+ ...prepareResult,
102
+ })
103
+
104
+ expect(Object.keys(sendFundsResult)).toEqual([
105
+ 'txId',
106
+ 'fromAddress',
107
+ 'toAddress',
108
+ 'amount',
109
+ 'fee',
110
+ ])
111
+
112
+ expect(sendFundsResult.txId).toEqual(
113
+ '4d71a7ec4866ef7ccc40a574e6bd661a8219c0dffd1f507d8026039907d03cb5'
114
+ )
115
+ expect(sendFundsResult.fromAddress).toEqual(fromAddress)
116
+ expect(sendFundsResult.toAddress).toEqual(toAddress)
117
+ expect(sendFundsResult.fee).toEqual(prepareResult.fee)
118
+ expect(sendFundsResult.amount).toEqual(prepareResult.amount)
119
+ expect(insightClient.broadcastTx).toHaveBeenCalledTimes(1)
120
+ expect(insightClient.broadcastTx).toBeCalledWith(
121
+ '020000000001012ed3c9917a29f91be86eae85c7dd5191d5fb8dacfef7ecaf2f6ab680e8b7cb710000000000ffffffff0118769800000000001976a9146da8a36d9bad2762d037ef60f974df6fd4c7798c88ac0247304402206ae5d4bb619a2a22816c582eedbd09a299034663c7f8d61912eeb3306a32e46b022015e1b090f4143b1824aefc0ec4cba8c1d1f104f99b97d8677ae53aab727e2843012103822f479dce9d7880edb4dd486732336603286d43ae6db4e04599d673a938093100000000'
122
+ )
123
+ },
124
+
125
+ 'can move from taproot to legacy': async ({ asset, assetClientInterface, getPrivateKey }) => {
126
+ const toAddress = '1KS3LDX6BfkD1GGVDiVuB5WsER1SLPyGcc'
127
+
128
+ const { privateKey, address: fromAddress } = await getPrivateKey({
129
+ asset,
130
+ purpose: 86,
131
+ accountIndex: 1,
132
+ chainIndex: 0,
133
+ addressIndex: 0,
134
+ })
135
+
136
+ const insightClient = asset.insightClient
137
+ const accountIndex = 0
138
+
139
+ const rawUtxos = [
140
+ {
141
+ address: fromAddress,
142
+ txId: '71cbb7e880b66a2fafecf7feac8dfbd59151ddc785ae6ee81bf9297a91c9d32e',
143
+ confirmations: 6048,
144
+ value: 0.1,
145
+ vout: 0,
146
+ height: 765218,
147
+ script: '5120b5c7dcfa43fca2e989ec0ede55a533687957b2fa2557faa82b509b74ee14f537',
148
+ asset: undefined,
149
+ },
150
+ ]
151
+
152
+ jest.spyOn(insightClient, 'broadcastTx').mockImplementation(() => true)
153
+ jest.spyOn(insightClient, 'fetchUTXOs').mockImplementation(([address]) => {
154
+ if (address === fromAddress) {
155
+ return rawUtxos
156
+ }
157
+ return []
158
+ })
159
+ const prepareResult = await asset.api.moveFunds.prepareSendFundsTx({
160
+ assetName,
161
+ input: privateKey,
162
+ assetClientInterface,
163
+ toAddress,
164
+ walletAccount: `exodus_${accountIndex}`,
165
+ MoveFundsError,
166
+ })
167
+ expect(Object.keys(prepareResult)).toEqual([
168
+ 'fromAddress',
169
+ 'toAddress',
170
+ 'amount',
171
+ 'fee',
172
+ 'utxos',
173
+ 'unsignedTx',
174
+ 'sizeKB',
175
+ 'privateKey',
176
+ ])
177
+
178
+ expect(prepareResult.fromAddress).toEqual(fromAddress)
179
+ expect(prepareResult.toAddress).toEqual(toAddress)
180
+ expect(prepareResult.fee.toBaseString({ unit: true })).toEqual('7548 satoshis')
181
+ expect(prepareResult.amount.toBaseString({ unit: true })).toEqual('9992452 satoshis')
182
+ expect(prepareResult.utxos.value.toBaseString({ unit: true })).toEqual('10000000 satoshis')
183
+ expect(prepareResult.utxos.value.sub(prepareResult.fee)).toEqual(prepareResult.amount)
184
+ expect(prepareResult.utxos.size).toEqual(rawUtxos.length)
185
+ expect(prepareResult.privateKey).toEqual(privateKey)
186
+ expect(prepareResult.sizeKB).toEqual(0.111)
187
+
188
+ const sendFundsResult = await asset.api.moveFunds.sendFunds({
189
+ assetName,
190
+ ...prepareResult,
191
+ })
192
+
193
+ expect(Object.keys(sendFundsResult)).toEqual([
194
+ 'txId',
195
+ 'fromAddress',
196
+ 'toAddress',
197
+ 'amount',
198
+ 'fee',
199
+ ])
200
+
201
+ expect(sendFundsResult.txId).toEqual(
202
+ '8e54712bd6c16303dc89b652d87ba61da34fab17e37aac6bb7449dc9fc47f991'
203
+ )
204
+ expect(sendFundsResult.fromAddress).toEqual(fromAddress)
205
+ expect(sendFundsResult.toAddress).toEqual(toAddress)
206
+ expect(sendFundsResult.fee).toEqual(prepareResult.fee)
207
+ expect(sendFundsResult.amount).toEqual(prepareResult.amount)
208
+ expect(insightClient.broadcastTx).toHaveBeenCalledTimes(1)
209
+ expect(insightClient.broadcastTx).toBeCalledWith(
210
+ '020000000001012ed3c9917a29f91be86eae85c7dd5191d5fb8dacfef7ecaf2f6ab680e8b7cb710000000000ffffffff0104799800000000001976a914ca2e9bf684906245a62a50af0b4c92ad47c6f03d88ac01403cf28e0f3ad3bda2bc66979bb9f3f221e2eba59b910c6aaaaf5779927373279955b9dae3edbdd66bfa4b1dcb7dac795c61531c4e345ceceb07c1ae6390ccb3c900000000'
211
+ )
212
+ },
213
+
214
+ 'can move from legacy to segwit': async ({ asset, assetClientInterface, getPrivateKey }) => {
215
+ const toAddress = 'bc1q3yt56war2meuc0jkp85y9ty58flkwf9nxmt2rk'
216
+
217
+ const { privateKey, address: fromAddress } = await getPrivateKey({
218
+ asset,
219
+ purpose: 44,
220
+ accountIndex: 1,
221
+ chainIndex: 0,
222
+ addressIndex: 1,
223
+ })
224
+
225
+ const insightClient = asset.insightClient
226
+ const accountIndex = 0
227
+
228
+ const rawUtxos = [
229
+ {
230
+ address: fromAddress,
231
+ txId: '2096181ad3ce844c1e5f8e033d9caaf84202c1f06d6b8cd67b26a2574a7567db',
232
+ confirmations: 12036,
233
+ value: 0.0002,
234
+ vout: 2,
235
+ height: 759232,
236
+ script: '76a91439ed58f4ac11f1239d9a71b6f6eb82f050e5916288ac',
237
+ asset: undefined,
238
+ },
239
+ ]
240
+
241
+ const rawTxs = {
242
+ '2096181ad3ce844c1e5f8e033d9caaf84202c1f06d6b8cd67b26a2574a7567db':
243
+ '02000000000101ca9ca8b726f6b212056270e0bc746457a7aeefc1ebe0022d74c2e70a45ac6fa70000000000fdffffff03a08601000000000016001418d1c1c15e818969f10c3563723e25ca06cd99e51fdbef0200000000160014d55ed98cefa2cfd7bf1489e15311d586ea55dbda204e0000000000001976a91439ed58f4ac11f1239d9a71b6f6eb82f050e5916288ac02483045022100c432f274be60fd883e8840d1181c103b8ee3892a09f2c8b781f78cfec805d4f802207a3fba3aca4eecc247e3d6538b1d1b91cce185606416eec4ec33c3c0a7d385ea0121031447ea71d2d2172c0560bc52afa7f3e87fae66c18b22e832a205a5662063eb4b00000000',
244
+ }
245
+
246
+ jest.spyOn(insightClient, 'broadcastTx').mockImplementation(() => true)
247
+ jest.spyOn(insightClient, 'fetchRawTx').mockImplementation(async (txId) => {
248
+ expect(rawTxs[txId]).toBeDefined()
249
+ return rawTxs[txId]
250
+ })
251
+
252
+ jest.spyOn(insightClient, 'fetchUTXOs').mockImplementation(([address]) => {
253
+ if (address === fromAddress) {
254
+ return rawUtxos
255
+ }
256
+ return []
257
+ })
258
+ const prepareResult = await asset.api.moveFunds.prepareSendFundsTx({
259
+ assetName,
260
+ input: privateKey,
261
+ assetClientInterface,
262
+ toAddress,
263
+ walletAccount: `exodus_${accountIndex}`,
264
+ MoveFundsError,
265
+ })
266
+ expect(Object.keys(prepareResult)).toEqual([
267
+ 'fromAddress',
268
+ 'toAddress',
269
+ 'amount',
270
+ 'fee',
271
+ 'utxos',
272
+ 'unsignedTx',
273
+ 'sizeKB',
274
+ 'privateKey',
275
+ ])
276
+ expect(prepareResult.fromAddress).toEqual(fromAddress)
277
+ expect(prepareResult.toAddress).toEqual(toAddress)
278
+ expect(prepareResult.fee.toBaseString({ unit: true })).toEqual('13668 satoshis')
279
+ expect(prepareResult.amount.toBaseString({ unit: true })).toEqual('6332 satoshis')
280
+ expect(prepareResult.utxos.value.toBaseString({ unit: true })).toEqual('20000 satoshis')
281
+ expect(prepareResult.utxos.value.sub(prepareResult.fee)).toEqual(prepareResult.amount)
282
+ expect(prepareResult.utxos.size).toEqual(rawUtxos.length)
283
+ expect(prepareResult.privateKey).toEqual(privateKey)
284
+ expect(prepareResult.sizeKB).toEqual(0.20099999999999998)
285
+
286
+ const sendFundsResult = await asset.api.moveFunds.sendFunds({
287
+ assetName,
288
+ ...prepareResult,
289
+ })
290
+
291
+ expect(Object.keys(sendFundsResult)).toEqual([
292
+ 'txId',
293
+ 'fromAddress',
294
+ 'toAddress',
295
+ 'amount',
296
+ 'fee',
297
+ ])
298
+
299
+ expect(sendFundsResult.txId).toEqual(
300
+ '7e4dc767ca0dbfeab3c3dc3269449e1062ad6bc15e208b7d43a6726a2ecfb77e'
301
+ )
302
+ expect(sendFundsResult.fromAddress).toEqual(fromAddress)
303
+ expect(sendFundsResult.toAddress).toEqual(toAddress)
304
+ expect(sendFundsResult.fee).toEqual(prepareResult.fee)
305
+ expect(sendFundsResult.amount).toEqual(prepareResult.amount)
306
+
307
+ expect(insightClient.broadcastTx).toHaveBeenCalledTimes(1)
308
+ expect(insightClient.broadcastTx).toBeCalledWith(
309
+ '0200000001db67754a57a2267bd68c6b6df0c10242f8aa9c3d038e5f1e4c84ced31a189620020000006a473044022061dc43e5aa15f716257bb5538771cb9bb872827f48c343d20ee6d2c8e970138302207f6ad29083d95bced68f64c46633d470892cab9154fad991b926674a56808adb012102fad2af50eac67b388a83a69c62d666675b1a9899e7f3a4f59fbb832e2cb6ea85ffffffff01bc1800000000000016001489174d3ba356f3cc3e5609e842ac943a7f6724b300000000'
310
+ )
311
+ },
312
+
313
+ 'cannot move 0': async ({ asset, assetClientInterface }) => {
314
+ const toAddress = '1Azpg7QEdQ8FHhNLzEpbpo1Uowh3t7B3Ex'
315
+ const input = 'KwFHiWMbL5X7oDQ9Cf24bX6zVrHStaGMu5h3omriuhXDA1Kujf6x'
316
+ const insightClient = asset.insightClient
317
+
318
+ jest.spyOn(insightClient, 'broadcastTx').mockImplementation(() => true)
319
+ jest.spyOn(insightClient, 'fetchUTXOs').mockImplementation(([address]) => {
320
+ return []
321
+ })
322
+
323
+ try {
324
+ await asset.api.moveFunds.prepareSendFundsTx({
325
+ assetName,
326
+ input,
327
+ assetClientInterface,
328
+ toAddress,
329
+ walletAccount: 'exodus_0',
330
+ MoveFundsError,
331
+ })
332
+ expect(true).toEqual(false)
333
+ } catch (e) {
334
+ expect(e.message).toEqual('balance-zero')
335
+ expect(e.extra).toEqual({
336
+ input: expect.any(String),
337
+ asset: expect.any(Object),
338
+ fromAddress:
339
+ '1Da2gQPyb7EQdiAYfuveAJ7vQxKQizFDDF, 3CaFGT9ntyqCgSD9m9SUCQs2B4bVLFxDwN, bc1q38s2x49tcl25tvg998t53kaaje3gyf5lzs56ul, or bc1pk765u7msse4mpj9zyrepqewdagk4qhrnkp5twq886rpr4t09vv3q7q6fpj',
340
+ fromAddresses: [
341
+ '1Da2gQPyb7EQdiAYfuveAJ7vQxKQizFDDF',
342
+ '3CaFGT9ntyqCgSD9m9SUCQs2B4bVLFxDwN',
343
+ 'bc1q38s2x49tcl25tvg998t53kaaje3gyf5lzs56ul',
344
+ 'bc1pk765u7msse4mpj9zyrepqewdagk4qhrnkp5twq886rpr4t09vv3q7q6fpj',
345
+ ],
346
+ })
347
+ }
348
+ },
349
+
350
+ 'cannot move to own address': async ({ asset, assetClientInterface, getPrivateKey }) => {
351
+ const accountIndex = 2
352
+
353
+ const { privateKey, address: fromAddress } = await getPrivateKey({
354
+ asset,
355
+ purpose: 84,
356
+ accountIndex,
357
+ chainIndex: 0,
358
+ addressIndex: 0,
359
+ })
360
+
361
+ const toAddress = fromAddress
362
+
363
+ expect(toAddress).toEqual('bc1q80wm35xel29d2xd79e4uveudejwmh3jnecvnl4')
364
+
365
+ const insightClient = asset.insightClient
366
+
367
+ const rawUtxos = [
368
+ {
369
+ address: fromAddress,
370
+ txId: 'bd6abad81417d362de666d92f885f9ce6a11386af876ccc6e755dd7ecd3f093a',
371
+ confirmations: 3590,
372
+ value: 0.201,
373
+ vout: 0,
374
+ height: 767664,
375
+ script: '001413c3a3fe40a9a1f47de541c6d48a50cf9b0beeae',
376
+ asset: undefined,
377
+ },
378
+ ]
379
+
380
+ jest.spyOn(insightClient, 'broadcastTx').mockImplementation(() => true)
381
+ jest.spyOn(insightClient, 'fetchUTXOs').mockImplementation(([address]) => {
382
+ if (address === fromAddress) {
383
+ return rawUtxos
384
+ }
385
+ return []
386
+ })
387
+
388
+ try {
389
+ await asset.api.moveFunds.prepareSendFundsTx({
390
+ assetName,
391
+ input: privateKey,
392
+ assetClientInterface,
393
+ toAddress,
394
+ walletAccount: `exodus_${accountIndex}`,
395
+ MoveFundsError,
396
+ })
397
+ expect(true).toEqual(false)
398
+ } catch (e) {
399
+ expect(e.message).toEqual('private-key-own-key')
400
+ expect(e.extra).toEqual({
401
+ asset: expect.any(Object),
402
+ input: expect.any(String),
403
+ })
404
+ }
405
+ },
406
+ },
407
+ })
408
+ })