@exodus/bitcoin-api 2.3.2 → 2.3.3

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/bitcoin-api",
3
- "version": "2.3.2",
3
+ "version": "2.3.3",
4
4
  "description": "Exodus bitcoin-api",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -40,5 +40,5 @@
40
40
  "@exodus/bitcoin-meta": "^1.0.1",
41
41
  "@noble/secp256k1": "~1.5.3"
42
42
  },
43
- "gitHead": "5bc09581d58d29a80871f4fbb189ff2dcf389b96"
43
+ "gitHead": "87922c8e20b6e152449dd6e35185587fa78bba25"
44
44
  }
@@ -1,5 +1,5 @@
1
1
  import bs58check from 'bs58check'
2
- import bech32 from 'bech32'
2
+ import * as bech32 from 'bech32'
3
3
  import assert from 'minimalistic-assert'
4
4
  import { identity, pickBy } from 'lodash'
5
5
  import * as bitcoinjsOriginal from '@exodus/bitcoinjs-lib'
@@ -1,6 +1,6 @@
1
1
  import bs58check from 'bs58check'
2
2
  import wif from 'wif'
3
- import bech32 from 'bech32'
3
+ import * as bech32 from 'bech32'
4
4
  import assert from 'minimalistic-assert'
5
5
  import { identity, pickBy } from 'lodash'
6
6
  import * as defaultBitcoinjsLib from '@exodus/bitcoinjs-lib'
package/src/move-funds.js CHANGED
@@ -41,13 +41,20 @@ export const moveFundsFactory = ({
41
41
  assert(address, 'address is required')
42
42
  assert(signTx, 'signTx is required')
43
43
  assert(getAddressesFromPrivateKey, 'getAddressesFromPrivateKey is required')
44
- async function prepareFunds(assetName, input, options = {}) {
45
- const { toAddress, assetClientInterface, MoveFundsError, walletAccount } = options
46
- assert(MoveFundsError, 'MoveFundsError is required') // should we move MoveFundsError to asset libs?
44
+
45
+ async function prepareSendFundsTx({
46
+ assetName,
47
+ walletAccount,
48
+ input,
49
+ toAddress,
50
+ assetClientInterface,
51
+ MoveFundsError,
52
+ }) {
53
+ assert(asset.name === assetName, `expected asset ${asset.name} but got assetName ${assetName}`)
54
+ assert(walletAccount, 'walletAccount is required')
47
55
  assert(toAddress, 'toAddress is required')
48
56
  assert(assetClientInterface, 'assetClientInterface is required')
49
- assert(walletAccount, 'walletAccount is required')
50
- assert(asset.name === assetName, `expected asset ${asset.name} but got assetName ${assetName}`)
57
+ assert(MoveFundsError, 'MoveFundsError is required') // should we move MoveFundsError to asset libs?
51
58
 
52
59
  const formatProps = {
53
60
  asset,
@@ -69,71 +76,78 @@ export const moveFundsFactory = ({
69
76
  multiAddressMode: true,
70
77
  })
71
78
 
72
- let found, address, utxos
73
- for (address of addresses) {
74
- const selfSend = receiveAddresses.some(
75
- (receiveAddress) => String(receiveAddress) === String(address)
76
- )
77
- if (selfSend) {
78
- throw new MoveFundsError('private-key-own-key', formatProps)
79
- }
79
+ const findFromAddress = async () => {
80
+ for (const currentAddress of addresses) {
81
+ const selfSend = receiveAddresses.some(
82
+ (receiveAddress) => String(receiveAddress) === String(currentAddress)
83
+ )
84
+ if (selfSend) {
85
+ throw new MoveFundsError('private-key-own-key', formatProps)
86
+ }
80
87
 
81
- utxos = await getUtxos({ asset, address })
88
+ const utxos = await getUtxos({ asset, address: currentAddress })
82
89
 
83
- if (!utxos.value.isZero) {
84
- found = true
85
- break
90
+ if (!utxos.value.isZero) {
91
+ return { fromAddress: currentAddress, utxos }
92
+ }
86
93
  }
94
+ throw new MoveFundsError('balance-zero', {
95
+ ...formatProps,
96
+ fromAddress: `${addresses.slice(0, -1).join(', ')}, or ${addresses[addresses.length - 1]}`,
97
+ })
87
98
  }
88
- if (!found) {
89
- formatProps.fromAddress = `${addresses.slice(0, -1).join(', ')}, or ${
90
- addresses[addresses.length - 1]
91
- }`
92
- throw new MoveFundsError('balance-zero', formatProps)
93
- }
94
- const fromAddress = address
95
- formatProps.fromAddress = fromAddress
96
99
 
100
+ const { fromAddress, utxos } = await findFromAddress()
101
+ formatProps.fromAddress = fromAddress
97
102
  const feeData = await assetClientInterface.getFeeConfig({ assetName })
98
- const fee = getFee({ asset, feeData, utxos, compressed })
103
+ const { fee, sizeKB } = getFee({ asset, feeData, utxos, compressed })
99
104
 
100
105
  let amount = utxos.value.sub(fee)
101
106
  if (amount.isNegative) {
102
107
  throw new MoveFundsError('balance-negative', formatProps)
103
108
  }
104
109
 
105
- return { fromAddress, toAddress, amount, fee, utxos, privateKey }
106
- }
107
-
108
- const sendFunds = async (
109
- assetName,
110
- { fromAddress, toAddress, amount, fee, utxos, privateKey }
111
- ) => {
112
- assert(fromAddress, 'fromAddress is required')
113
- assert(toAddress, 'toAddress is required')
114
- assert(fee, 'fee is required')
115
- assert(utxos, 'utxos is required')
116
- assert(privateKey, 'privateKey is required')
117
- const selected = utxos
118
- const privateKeysAddressMap = {
119
- [fromAddress]: privateKey,
120
- }
121
110
  const unsignedTx = {
122
111
  txData: {
123
- inputs: createInputs(assetName, selected.toArray()),
112
+ inputs: createInputs(assetName, utxos.toArray()),
124
113
  outputs: [createOutput(assetName, toAddress, amount)],
125
114
  },
126
115
  txMeta: {
127
- addressPathsMap: selected.getAddressPathsMap(),
116
+ addressPathsMap: utxos.getAddressPathsMap(),
128
117
  },
129
118
  }
130
119
  const nonWitnessTxs = await getNonWitnessTxs(
131
120
  { name: assetName, address }, // pretty ugly hack!
132
- selected,
121
+ utxos,
133
122
  insightClient
134
123
  )
135
124
  Object.assign(unsignedTx.txMeta, { rawTxs: nonWitnessTxs })
136
125
 
126
+ return { fromAddress, toAddress, amount, fee, utxos, unsignedTx, sizeKB, privateKey }
127
+ }
128
+
129
+ const sendFunds = async ({
130
+ assetName,
131
+ fromAddress,
132
+ toAddress,
133
+ amount,
134
+ fee,
135
+ unsignedTx,
136
+ privateKey,
137
+ }) => {
138
+ // the response from prepareSendFundsTx
139
+ assert(assetName, 'assetName is required')
140
+ assert(fromAddress, 'fromAddress is required')
141
+ assert(toAddress, 'toAddress is required')
142
+ assert(amount, 'amount is required')
143
+ assert(fee, 'fee is required')
144
+ assert(unsignedTx, 'unsignedTx is required')
145
+ assert(privateKey, 'privateKey is required')
146
+
147
+ const privateKeysAddressMap = {
148
+ [fromAddress]: privateKey,
149
+ }
150
+
137
151
  const { rawTx, txId } = await signTx({ unsignedTx, privateKeysAddressMap })
138
152
 
139
153
  await insightClient.broadcastTx(rawTx.toString('hex'))
@@ -158,7 +172,9 @@ export const moveFundsFactory = ({
158
172
  function getFee({ asset, feeData, utxos, compressed }) {
159
173
  const { feePerKB } = feeData
160
174
  const feeEstimator = getFeeEstimator(asset, feePerKB, { compressed })
161
- return feeEstimator({ inputs: utxos, outputs: [null] }).toDefault()
175
+ const fee = feeEstimator({ inputs: utxos, outputs: [null] })
176
+ const sizeKB = fee.toDefaultNumber() / feePerKB
177
+ return { fee, sizeKB }
162
178
  }
163
179
 
164
180
  function isValidPrivateKey(privateKey) {
@@ -171,7 +187,7 @@ export const moveFundsFactory = ({
171
187
  }
172
188
 
173
189
  return {
174
- prepareFunds,
190
+ prepareSendFundsTx,
175
191
  sendFunds,
176
192
  }
177
193
  }