@exodus/bitcoin-api 1.0.0 → 1.0.1

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": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Exodus bitcoin-api",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -35,5 +35,6 @@
35
35
  },
36
36
  "devDependencies": {
37
37
  "@noble/secp256k1": "~1.5.3"
38
- }
38
+ },
39
+ "gitHead": "16aa341d1ce1b6a86dba8516bf19b361a547d880"
39
40
  }
@@ -1,6 +1,5 @@
1
1
  // @flow
2
2
  import assert from 'minimalistic-assert'
3
- import { get } from 'lodash'
4
3
  import * as varuint from 'varuint-bitcoin'
5
4
  import { UtxoCollection } from '@exodus/models'
6
5
 
@@ -53,13 +52,16 @@ const scriptPubKeyLengths = {
53
52
  // 10 = version: 4, locktime: 4, inputs and outputs count: 1
54
53
  // 148 = txId: 32, vout: 4, count: 1, script: 107 (max), sequence: 4
55
54
  // 34 = value: 8, count: 1, scriptPubKey: 25 (P2PKH) and 23 (P2SH)
56
- export const getSizeFactory = ({ ecc }) => (
55
+ export const getSizeFactory = ({ ecc, defaultOutputType, addressApi }) => (
57
56
  asset: Object,
58
57
  inputs: Array | UtxoCollection,
59
58
  outputs: Array,
60
59
  { compressed = true } = {}
61
60
  ) => {
62
61
  assert(ecc, 'ecc is required')
62
+ assert(defaultOutputType, 'defaultOutputType is required')
63
+ assert(addressApi, 'addressApi is required')
64
+ const assetName = asset.name
63
65
  if (inputs instanceof UtxoCollection) {
64
66
  inputs = Array.from(inputs).map((utxo) => utxo.script || null)
65
67
  }
@@ -78,7 +80,7 @@ export const getSizeFactory = ({ ecc }) => (
78
80
  const scriptBuffer = Buffer.from(script, 'hex')
79
81
  const scriptType = classifyOutput(scriptBuffer)
80
82
 
81
- const supportedTypes = supportedInputTypes[asset.name] || supportedInputTypes.default
83
+ const supportedTypes = supportedInputTypes[assetName] || supportedInputTypes.default
82
84
  assert(
83
85
  supportedTypes.includes(scriptType),
84
86
  `Only ${supportedTypes.join(', ')} inputs supported right now`
@@ -93,19 +95,21 @@ export const getSizeFactory = ({ ecc }) => (
93
95
  varuint.encodingLength(outputs.length) + // outputs_len
94
96
  // output[]
95
97
  outputs.reduce((t, output) => {
96
- if (output === null) output = get(asset, 'address.versions.bech32') ? 'P2WSH' : 'P2PKH'
98
+ // if (output === null) output = get(asset, 'address.versions.bech32') ? 'P2WSH' : 'P2PKH'
99
+
100
+ if (output === null) output = defaultOutputType
97
101
 
98
102
  let scriptType = scriptClassify.types[output]
99
- const supportedTypes = supportedOutputTypes[asset.name] || supportedOutputTypes.default
103
+ const supportedTypes = supportedOutputTypes[assetName] || supportedOutputTypes.default
100
104
 
101
105
  if (!supportedTypes.includes(scriptType)) {
102
- if (asset.address.isP2PKH(output)) scriptType = P2PKH
103
- else if (asset.address.isP2SH(output)) scriptType = P2SH
104
- else if (asset.address.isP2WPKH && asset.address.isP2WPKH(output)) scriptType = P2WPKH
105
- else if (asset.address.isP2TR && asset.address.isP2TR(output)) scriptType = P2TR
106
- else if (asset.address.isP2WSH && asset.address.isP2WSH(output)) scriptType = P2WSH
106
+ if (addressApi.isP2PKH(output)) scriptType = P2PKH
107
+ else if (addressApi.isP2SH(output)) scriptType = P2SH
108
+ else if (addressApi.isP2WPKH && addressApi.isP2WPKH(output)) scriptType = P2WPKH
109
+ else if (addressApi.isP2TR && addressApi.isP2TR(output)) scriptType = P2TR
110
+ else if (addressApi.isP2WSH && addressApi.isP2WSH(output)) scriptType = P2WSH
107
111
  else {
108
- scriptType = classifyOutput(asset.address.toScriptPubKey(output))
112
+ scriptType = classifyOutput(addressApi.toScriptPubKey(output))
109
113
  }
110
114
  }
111
115
  assert(
@@ -160,5 +164,8 @@ export const getSizeFactory = ({ ecc }) => (
160
164
  return Math.ceil(weight / 4)
161
165
  }
162
166
 
163
- const getFeeEstimatorFactory = ({ ecc }) => createDefaultFeeEstimator(getSizeFactory({ ecc }))
167
+ const getFeeEstimatorFactory = ({ ecc, defaultOutputType, addressApi }) => {
168
+ const getSize = getSizeFactory({ ecc, defaultOutputType, addressApi })
169
+ return createDefaultFeeEstimator(getSize)
170
+ }
164
171
  export default getFeeEstimatorFactory
@@ -2,7 +2,6 @@
2
2
  import { UtxoCollection } from '@exodus/models'
3
3
  import { findLargeUnconfirmedTxs } from './tx-utils'
4
4
  import assert from 'minimalistic-assert'
5
- import { mapValues } from '@exodus/basic-utils'
6
5
 
7
6
  export function getUtxos({ accountState, asset }) {
8
7
  return (
@@ -28,19 +27,18 @@ export const getBalancesFactory = ({ taprootEnabled, feeData }) => {
28
27
  feeData,
29
28
  taprootEnabled,
30
29
  }).value
31
- return mapValues({ balance, spendableBalance }, (balance) => (balance.isZero ? null : balance))
30
+ return { balance, spendableBalance }
32
31
  }
33
32
  }
34
33
 
35
- const isTaprootUtxo = (asset, utxo) =>
36
- String(utxo.address).startsWith(asset.address.versions.taproot)
34
+ const isTaprootUtxo = ({ utxo }) => String(utxo.address).length === 62
37
35
 
38
36
  export function getSpendableUtxos({ asset, utxos, feeData, txSet, taprootEnabled }) {
39
37
  if (!['bitcoin', 'bitcointestnet', 'bitcoinregtest'].includes(asset.name)) return utxos
40
38
 
41
39
  if (!taprootEnabled) {
42
40
  utxos = UtxoCollection.fromArray(
43
- utxos.toArray().filter((utxo) => !isTaprootUtxo(asset, utxo)),
41
+ utxos.toArray().filter((utxo) => !isTaprootUtxo({ utxo })),
44
42
  { currency: asset.currency }
45
43
  )
46
44
  }