@exodus/bitcoin-api 2.1.1 → 2.2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/bitcoin-api",
3
- "version": "2.1.1",
3
+ "version": "2.2.0",
4
4
  "description": "Exodus bitcoin-api",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -40,5 +40,5 @@
40
40
  "@exodus/bip-schnorr": "0.6.6-fork-1",
41
41
  "@noble/secp256k1": "~1.5.3"
42
42
  },
43
- "gitHead": "d74d7ee6a7ca8da4373ea7b6aad6b245a50cae27"
43
+ "gitHead": "822be47e11cc1537913d678bc2771ac4c09eb899"
44
44
  }
@@ -2,11 +2,12 @@ import bs58check from 'bs58check'
2
2
  import bech32 from 'bech32'
3
3
  import assert from 'minimalistic-assert'
4
4
  import { identity, pickBy } from 'lodash'
5
+ import * as bitcoinjsOriginal from '@exodus/bitcoinjs-lib'
5
6
 
6
7
  export const createBtcLikeAddress = ({
7
8
  versions,
8
9
  coinInfo,
9
- bitcoinjsLib,
10
+ bitcoinjsLib: bitcoinjsLibFork,
10
11
  ecc,
11
12
  useBip86 = false,
12
13
  validateFunctions = {},
@@ -50,11 +51,11 @@ export const createBtcLikeAddress = ({
50
51
  const isP2TR =
51
52
  validateFunctions.isP2TR ||
52
53
  (useBip86 &&
53
- bitcoinjsLib &&
54
+ bitcoinjsLibFork &&
54
55
  ((addr) => {
55
56
  try {
56
57
  const network = coinInfo.toBitcoinJS()
57
- bitcoinjsLib.payments.p2tr({ address: addr, network }, { eccLib: ecc })
58
+ bitcoinjsLibFork.payments.p2tr({ address: addr, network }, { eccLib: ecc })
58
59
  return true
59
60
  } catch (e) {
60
61
  return false
@@ -88,13 +89,16 @@ export const createBtcLikeAddress = ({
88
89
  purposeValidators.find(({ validator }) => validator(string))?.purpose
89
90
  const validate = (string) => Object.values(resolvedValidateFunctions).some((fn) => fn(string))
90
91
 
91
- const toScriptPubKey =
92
- coinInfo && bitcoinjsLib?.address?.toOutputScript
93
- ? (string) => {
94
- const network = coinInfo.toBitcoinJS()
95
- return bitcoinjsLib.address.toOutputScript(string, network, ecc)
96
- }
97
- : undefined
92
+ const toScriptPubKey = (string) => {
93
+ const network = coinInfo.toBitcoinJS()
94
+ return bitcoinjsOriginal.address.toOutputScript(string, network, ecc)
95
+ }
96
+
97
+ const fromScriptPubKey = (scriptPubKey) => {
98
+ if (typeof scriptPubKey === 'string') scriptPubKey = Buffer.from(scriptPubKey, 'hex')
99
+ const network = coinInfo.toBitcoinJS()
100
+ return bitcoinjsOriginal.address.fromOutputScript(scriptPubKey, network)
101
+ }
98
102
 
99
103
  return pickBy(
100
104
  {
@@ -103,6 +107,7 @@ export const createBtcLikeAddress = ({
103
107
  validate,
104
108
  resolvePurpose,
105
109
  toScriptPubKey,
110
+ fromScriptPubKey,
106
111
  ...extraFunctions,
107
112
  },
108
113
  identity
@@ -1,19 +1,14 @@
1
1
  import bs58check from 'bs58check'
2
2
  import wif from 'wif'
3
- import createHash from 'create-hash'
4
3
  import bech32 from 'bech32'
5
4
  import assert from 'minimalistic-assert'
6
5
  import { identity, pickBy } from 'lodash'
7
6
  import * as defaultBitcoinjsLib from '@exodus/bitcoinjs-lib'
7
+ import secp256k1 from 'secp256k1'
8
+ import { hash160 } from './hash-utils'
8
9
 
9
10
  export const publicKeyToHashFactory = (p2pkh) => (publicKey) => {
10
- const sha = createHash('sha256')
11
- .update(publicKey)
12
- .digest()
13
- const pubKeyHash = createHash('rmd160')
14
- .update(sha)
15
- .digest()
16
- const payload = Buffer.concat([Buffer.from([p2pkh]), pubKeyHash])
11
+ const payload = Buffer.concat([Buffer.from([p2pkh]), hash160(publicKey)])
17
12
  return bs58check.encode(payload)
18
13
  }
19
14
 
@@ -54,12 +49,7 @@ export const createBtcLikeKeys = ({
54
49
  const encodePublicBech32 =
55
50
  encodePublicBech32Custom || versions.bech32 !== undefined
56
51
  ? (publicKey) => {
57
- const sha = createHash('sha256')
58
- .update(publicKey)
59
- .digest()
60
- const pubKeyHash = createHash('rmd160')
61
- .update(sha)
62
- .digest()
52
+ const pubKeyHash = hash160(publicKey)
63
53
  const witnessVersion = Buffer.from([0])
64
54
  const witnessProgram = Buffer.concat([
65
55
  witnessVersion,
@@ -82,8 +72,9 @@ export const createBtcLikeKeys = ({
82
72
  const encodeNestedP2WPKH =
83
73
  encodeNestedP2WPKHCustom || bitcoinjsLib
84
74
  ? (publicKey) => {
75
+ const pubkey = secp256k1.publicKeyConvert(publicKey, true)
85
76
  const witnessProgram = bitcoinjsLib.payments.p2wpkh({
86
- pubkey: publicKey,
77
+ pubkey,
87
78
  }).output
88
79
 
89
80
  const witnessProgramHash = bitcoinjsLib.crypto.hash160(witnessProgram)
@@ -0,0 +1,19 @@
1
+ import createHash from 'create-hash'
2
+ export function hash160(buffer) {
3
+ const sha256Hash = sha256(buffer)
4
+ try {
5
+ return createHash('ripemd160')
6
+ .update(sha256Hash)
7
+ .digest()
8
+ } catch {
9
+ return createHash('rmd160')
10
+ .update(sha256Hash)
11
+ .digest()
12
+ }
13
+ }
14
+
15
+ export function sha256(buffer) {
16
+ return createHash('sha256')
17
+ .update(buffer)
18
+ .digest()
19
+ }