@exodus/bitcoin-api 2.6.7 → 2.6.8-hiro.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": "2.6.7",
3
+ "version": "2.6.8-hiro.1",
4
4
  "description": "Exodus bitcoin-api",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -42,6 +42,5 @@
42
42
  "@scure/base": "^1.1.3",
43
43
  "@scure/btc-signer": "^1.1.0",
44
44
  "jest-when": "^3.5.1"
45
- },
46
- "gitHead": "04954f04a1246db5d0cc3fd848ef23a91df2b623"
45
+ }
47
46
  }
@@ -10,6 +10,7 @@ export function createAccountState({ asset, ordinalsEnabled = false, brc20Enable
10
10
 
11
11
  if (ordinalsEnabled) {
12
12
  defaults.ordinalsUtxos = empty
13
+ defaults.knownBalanceUtxoIds = []
13
14
  }
14
15
 
15
16
  if (brc20Enabled) {
@@ -1,6 +1,6 @@
1
1
  import assert from 'minimalistic-assert'
2
2
 
3
- export function getOrdinalAddress({
3
+ export async function getOrdinalAddress({
4
4
  asset,
5
5
  assetClientInterface,
6
6
  walletAccount,
@@ -12,10 +12,22 @@ export function getOrdinalAddress({
12
12
  if (ordinalChainIndex === undefined) {
13
13
  return undefined
14
14
  }
15
+
16
+ const purposes = await assetClientInterface.getSupportedPurposes({
17
+ assetName: asset.name,
18
+ walletAccount,
19
+ })
20
+
21
+ const purpose = 86
22
+
23
+ if (!purposes.includes(purpose)) {
24
+ return undefined
25
+ }
26
+
15
27
  return assetClientInterface.getAddress({
16
28
  assetName: asset.name,
17
29
  walletAccount,
18
- purpose: 86,
30
+ purpose,
19
31
  chainIndex: ordinalChainIndex,
20
32
  addressIndex: 0,
21
33
  })
@@ -67,7 +67,6 @@ export class BitcoinMonitorScanner {
67
67
 
68
68
  const storedUtxos = getUtxos({ asset, accountState })
69
69
  const storedOrdinalUtxos = getOrdinalsUtxos({ asset, accountState })
70
- const ordinalAddress = await this.getOrdinalAddress({ walletAccount })
71
70
 
72
71
  const currentStoredUtxos = storedUtxos.union(storedOrdinalUtxos)
73
72
 
@@ -90,7 +89,9 @@ export class BitcoinMonitorScanner {
90
89
  : (txs) =>
91
90
  txs.filter((tx) => {
92
91
  const txItem = currentTxs.get(tx.txid)
93
- return txItem && txItem.confirmed
92
+ const confirmed = txItem && txItem.confirmed
93
+ const inscriptionsIndexed = txItem?.data?.inscriptionsIndexed
94
+ return confirmed && (!this.#ordinalsEnabled || inscriptionsIndexed)
94
95
  }).length >= txs.length
95
96
 
96
97
  const unusedAddressIndexes = await assetClientInterface.getUnusedAddressIndexes({
@@ -344,6 +345,15 @@ export class BitcoinMonitorScanner {
344
345
  currencies: { [assetName]: currency },
345
346
  }
346
347
 
348
+ if (this.#ordinalsEnabled) {
349
+ txLogItem.data = {
350
+ ...txLogItem.data,
351
+ inscriptionsIndexed: txItem.inscriptionsIndexed,
352
+ sentInscriptions: [],
353
+ receivedInscriptions: [],
354
+ }
355
+ }
356
+
347
357
  let from = []
348
358
 
349
359
  // if txItem.vin has an address that matches ours, means we've spent this tx
@@ -361,8 +371,13 @@ export class BitcoinMonitorScanner {
361
371
  txLogItem.coinAmount = txLogItem.coinAmount.sub(currency.defaultUnit(vin.value))
362
372
  isSent = true
363
373
  txLogItem.data.sent = []
364
- if (ordinalAddress && address.toString() === ordinalAddress.toString()) {
365
- txLogItem.data.isInscriptionSent = true
374
+ if (this.#ordinalsEnabled && vin.inscriptions) {
375
+ txLogItem.data.sentInscriptions.push(
376
+ ...vin.inscriptions.map((i) => ({
377
+ ...i,
378
+ value: currency.defaultUnit(vin.value).toBaseNumber(),
379
+ }))
380
+ )
366
381
  }
367
382
 
368
383
  // this is only used to exclude the utxos in the reducer which is why we don't care about the other fields
@@ -432,8 +447,13 @@ export class BitcoinMonitorScanner {
432
447
  txLogItem.data.changeAddress = address
433
448
  }
434
449
 
435
- if (ordinalAddress && address.toString() === ordinalAddress.toString()) {
436
- txLogItem.data.isInscriptionReceived = true
450
+ if (this.#ordinalsEnabled && vout.inscriptions) {
451
+ txLogItem.data.receivedInscriptions.push(
452
+ ...vout.inscriptions.map((i) => ({
453
+ ...i,
454
+ value: currency.defaultUnit(vout.value).toBaseNumber(),
455
+ }))
456
+ )
437
457
  }
438
458
 
439
459
  // it was sent to us...
@@ -450,6 +470,11 @@ export class BitcoinMonitorScanner {
450
470
  rbfEnabled: txItem.rbf,
451
471
  }
452
472
 
473
+ if (this.#ordinalsEnabled) {
474
+ output.inscriptionsIndexed = txItem.inscriptionsIndexed
475
+ output.inscriptions = vout.inscriptions || []
476
+ }
477
+
453
478
  if (this.#shouldExcludeVoutUtxo({ asset, output, txItem, vout })) {
454
479
  return
455
480
  }
@@ -540,10 +565,13 @@ export class BitcoinMonitorScanner {
540
565
  return !isEqual(chain, originalChain.chain)
541
566
  })
542
567
 
568
+ const ordinalAddress = await this.getOrdinalAddress({ walletAccount })
543
569
  const utxosData = utxoCol
544
570
  ? partitionUtxos({
545
571
  allUtxos: utxoCol,
572
+ ordinalsEnabled: this.#ordinalsEnabled,
546
573
  ordinalAddress,
574
+ knownBalanceUtxoIds: accountState.knownBalanceUtxoIds,
547
575
  })
548
576
  : {}
549
577
 
@@ -603,7 +631,9 @@ export class BitcoinMonitorScanner {
603
631
 
604
632
  const { utxos, ordinalsUtxos } = partitionUtxos({
605
633
  allUtxos: txConfirmedUtxos,
634
+ ordinalsEnabled: this.#ordinalsEnabled,
606
635
  ordinalAddress: await this.getOrdinalAddress({ walletAccount }),
636
+ knownBalanceUtxoIds: accountState.knownBalanceUtxoIds,
607
637
  })
608
638
 
609
639
  return {
@@ -14,7 +14,12 @@ import {
14
14
  createOutput as dogecoinCreateOutput,
15
15
  } from './dogecoin'
16
16
  import { findUnconfirmedSentRbfTxs } from '../tx-utils'
17
- import { getOrdinalsUtxos, getUsableUtxos, getUtxos } from '../utxos-utils'
17
+ import {
18
+ getOrdinalsUtxos,
19
+ getTransferOrdinalsUtxos,
20
+ getUsableUtxos,
21
+ getUtxos,
22
+ } from '../utxos-utils'
18
23
 
19
24
  import * as defaultBitcoinjsLib from '@exodus/bitcoinjs-lib'
20
25
 
@@ -132,6 +137,7 @@ export const createAndBroadcastTXFactory = ({
132
137
  getFeeEstimator,
133
138
  getSizeAndChangeScript = getSizeAndChangeScriptFactory(), // for decred customizations
134
139
  allowUnconfirmedRbfEnabledUtxos,
140
+ ordinalsEnabled = false,
135
141
  }) => async (
136
142
  { asset: maybeToken, walletAccount, address, amount: tokenAmount, options },
137
143
  { assetClientInterface }
@@ -167,6 +173,11 @@ export const createAndBroadcastTXFactory = ({
167
173
 
168
174
  const inscriptionIds = nft?.tokenId ? [nft?.tokenId] : brc20 ? brc20.inscriptionIds : undefined
169
175
 
176
+ assert(
177
+ ordinalsEnabled || !inscriptionIds,
178
+ 'inscriptions cannot be sent when ordinalsEnabled=false '
179
+ )
180
+
170
181
  const shuffle = (list) => {
171
182
  return inscriptionIds ? list : doShuffle(list) // don't shuffle when sending ordinal!!!!
172
183
  }
@@ -192,26 +203,9 @@ export const createAndBroadcastTXFactory = ({
192
203
 
193
204
  const currentOrdinalsUtxos = getOrdinalsUtxos({ accountState, asset })
194
205
  const transferOrdinalsUtxos = inscriptionIds
195
- ? currentOrdinalsUtxos.filter((utxo) => inscriptionIds.includes(utxo.inscriptionId)) /// this could be bulk transfer if multiple inscription ids are provided
206
+ ? getTransferOrdinalsUtxos({ inscriptionIds, ordinalsUtxos: currentOrdinalsUtxos })
196
207
  : undefined
197
208
 
198
- if (inscriptionIds) {
199
- assert(
200
- transferOrdinalsUtxos?.size === inscriptionIds.length,
201
- `Expected ordinal utxos ${inscriptionIds.length}. Found: ${transferOrdinalsUtxos?.size || 0}`
202
- )
203
-
204
- // const unconfirmedOrdinalUtxos = transferOrdinalsUtxos
205
- // .toArray()
206
- // .filter((ordinalUtxo) => !(ordinalUtxo.confirmations > 0))
207
- // assert(
208
- // !unconfirmedOrdinalUtxos.length,
209
- // `OrdinalUtxo with inscription ids ${unconfirmedOrdinalUtxos
210
- // .map((utxo) => utxo.inscriptionId)
211
- // .join(', ')} have not confirmed yet`
212
- // )
213
- }
214
-
215
209
  const insightClient = asset.baseAsset.insightClient
216
210
  const currency = asset.currency
217
211
  const feeData = await assetClientInterface.getFeeConfig({ assetName })
@@ -434,6 +428,8 @@ export const createAndBroadcastTXFactory = ({
434
428
 
435
429
  const { script, size } = getSizeAndChangeScript({ assetName, tx, rawTx, changeUtxoIndex, txId })
436
430
 
431
+ // for ordinals, used to allow users spending change utxos even when unconfirmed and ordinals are unknown
432
+ const knownBalanceUtxoIds = accountState.knownBalanceUtxoIds || []
437
433
  let remainingUtxos = usableUtxos.difference(selectedUtxos)
438
434
  if (changeUtxoIndex !== -1) {
439
435
  const address = Address.create(ourAddress.address, ourAddress.meta)
@@ -446,6 +442,8 @@ export const createAndBroadcastTXFactory = ({
446
442
  confirmations: 0,
447
443
  rbfEnabled,
448
444
  }
445
+
446
+ knownBalanceUtxoIds.push(`${changeUtxo.txId}:${changeUtxo.vout}`.toLowerCase())
449
447
  remainingUtxos = remainingUtxos.addUtxo(changeUtxo)
450
448
  }
451
449
  if (replaceTx) {
@@ -462,6 +460,7 @@ export const createAndBroadcastTXFactory = ({
462
460
  newData: {
463
461
  utxos: remainingUtxos,
464
462
  ordinalsUtxos: remainingOrdinalsUtxos,
463
+ knownBalanceUtxoIds,
465
464
  },
466
465
  })
467
466
 
@@ -522,7 +521,16 @@ export const createAndBroadcastTXFactory = ({
522
521
  inputs: selectedUtxos.toJSON(),
523
522
  replacedTxId: replaceTx ? replaceTx.txId : undefined,
524
523
  nftId: nft ? `${assetName}:${nft.tokenId}` : undefined, // it allows BE to load the nft info while the nft is in transit
525
- inscriptionIds: inscriptionIds,
524
+ inscriptionsIndexed: ordinalsEnabled ? true : undefined,
525
+ sentInscriptions: inscriptionIds
526
+ ? inscriptionIds.map((inscriptionId) => {
527
+ return {
528
+ inscriptionId,
529
+ offset: 0,
530
+ value: 0,
531
+ }
532
+ })
533
+ : undefined,
526
534
  },
527
535
  },
528
536
  ],
@@ -5,6 +5,46 @@ import assert from 'minimalistic-assert'
5
5
 
6
6
  const MAX_ORDINAL_VALUE_POSTAGE = 10000
7
7
 
8
+ export function getTransferOrdinalsUtxos({ inscriptionIds, ordinalsUtxos }) {
9
+ const transferOrdinalsUtxos = ordinalsUtxos.filter((utxo) =>
10
+ utxo.inscriptions?.some((i) => inscriptionIds.includes(i.inscriptionId))
11
+ )
12
+ const unsafeInscriptions = transferOrdinalsUtxos.toArray().flatMap(
13
+ (utxo) =>
14
+ utxo.inscriptions?.filter((i) => {
15
+ const validInscription = isValidInscription({
16
+ value: utxo.value.toBaseNumber(),
17
+ offset: i.offset,
18
+ })
19
+ return validInscription && !inscriptionIds.includes(i.inscriptionId)
20
+ }) || []
21
+ )
22
+ assert(
23
+ !unsafeInscriptions.length,
24
+ `The following inscriptions are unsafe ${unsafeInscriptions.map(
25
+ (i) => i.inscriptionId
26
+ )} when ${inscriptionIds} should be spent`
27
+ )
28
+
29
+ const transferInscriptionIds = transferOrdinalsUtxos
30
+ .toArray()
31
+ .flatMap((utxo) => utxo.inscriptions)
32
+ .filter(({ inscriptionId }) => inscriptionIds.includes(inscriptionId))
33
+
34
+ assert(
35
+ transferInscriptionIds.length === inscriptionIds.length,
36
+ `Expected inscriptions ${inscriptionIds.length}. Found: ${transferInscriptionIds.length}`
37
+ )
38
+ return transferOrdinalsUtxos
39
+ }
40
+
41
+ export function isValidInscription({ value, offset }) {
42
+ assert(typeof value === 'number', 'value must be a number')
43
+ assert(typeof offset === 'number', 'offset must be a number')
44
+ // value >= 0 in case offset, alternatively convert to string/ln
45
+ return (value >= 0 && value <= MAX_ORDINAL_VALUE_POSTAGE) || offset === 0
46
+ }
47
+
8
48
  export function getUtxos({ accountState, asset }) {
9
49
  return (
10
50
  accountState?.utxos ||
@@ -23,31 +63,41 @@ export function getOrdinalsUtxos({ accountState, asset }) {
23
63
  )
24
64
  }
25
65
 
26
- function isOrdinalUtxo({ utxo, ordinalAddress }) {
27
- if (utxo.inscriptionId) {
28
- return true
29
- }
30
- if (!ordinalAddress) {
31
- // exclude utxos splitting
66
+ export function getValidInscriptions({ utxo }) {
67
+ return (utxo.inscriptions || []).filter((i) =>
68
+ isValidInscription({ value: utxo.value.toBaseNumber(), offset: i.offset })
69
+ )
70
+ }
71
+
72
+ function isOrdinalUtxo({ utxo, ordinalsEnabled, knownBalanceUtxoIds }) {
73
+ if (!ordinalsEnabled) {
32
74
  return false
33
75
  }
34
76
 
35
- if (utxo.address.toString() === ordinalAddress.toString()) {
36
- return true // we assume any utxo to the ordinal address is a ordinal utxos just in case
77
+ const utxoId = `${utxo.txId}:${utxo.vout}`.toLowerCase()
78
+
79
+ if (knownBalanceUtxoIds?.includes(utxoId) && !utxo.inscriptionsIndexed) {
80
+ return false // this allows users see and spend change balance after sending before hiro confirmation
37
81
  }
38
82
 
39
- if (utxo.confirmations) {
40
- return false
83
+ if (!utxo.inscriptionsIndexed) {
84
+ return true
41
85
  }
42
86
 
43
- return utxo.value.toBaseNumber() <= MAX_ORDINAL_VALUE_POSTAGE // while unconfirmed, put < 10000- sats in the ordinal utxos box just in case
87
+ const validInscriptions = getValidInscriptions({ utxo })
88
+ return validInscriptions.length > 0
44
89
  }
45
90
 
46
- export function partitionUtxos({ allUtxos, ordinalAddress }) {
91
+ export function partitionUtxos({ allUtxos, ordinalsEnabled, knownBalanceUtxoIds }) {
47
92
  assert(allUtxos, 'allUtxos is required')
93
+ // assert(ordinalAddress, 'ordinalAddress is required') // not used atm we may need to tune by ordinalAddress when unconfirmed or rubbish inscriptions
48
94
  return {
49
- utxos: allUtxos.filter((utxo) => !isOrdinalUtxo({ utxo, ordinalAddress })),
50
- ordinalsUtxos: allUtxos.filter((utxo) => isOrdinalUtxo({ utxo, ordinalAddress })),
95
+ utxos: allUtxos.filter(
96
+ (utxo) => !isOrdinalUtxo({ utxo, ordinalsEnabled, knownBalanceUtxoIds })
97
+ ),
98
+ ordinalsUtxos: allUtxos.filter((utxo) =>
99
+ isOrdinalUtxo({ utxo, ordinalsEnabled, knownBalanceUtxoIds })
100
+ ),
51
101
  }
52
102
  }
53
103