@exodus/bitcoin-plugin 1.0.3 → 1.0.6-ordinals.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-plugin",
3
- "version": "1.0.3",
3
+ "version": "1.0.6-ordinals.1",
4
4
  "description": "Exodus bitcoin-plugin",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -19,12 +19,12 @@
19
19
  },
20
20
  "dependencies": {
21
21
  "@exodus/asset-lib": "^3.7.1",
22
- "@exodus/bitcoin-api": "^2.3.7",
23
- "@exodus/bitcoin-lib": "^2.2.3",
22
+ "@exodus/bitcoin-api": "^2.3.8-ordinals.1",
23
+ "@exodus/bitcoin-lib": "^2.2.5",
24
24
  "minimalistic-assert": "^1.0.1"
25
25
  },
26
26
  "devDependencies": {
27
+ "@exodus/nfts-proxy": "^2.6.0",
27
28
  "@exodus/keychain": "^3.0.0"
28
- },
29
- "gitHead": "7a83d2ff5d672615938fba868779293df74ff51d"
29
+ }
30
30
  }
@@ -24,11 +24,15 @@ import {
24
24
  import { bitcoinFeeDataFactory } from './fee-data'
25
25
  import BitcoinFeeMonitor from './fee-monitor'
26
26
  import assert from 'minimalistic-assert'
27
+ import { nftsApiFactory } from './nfts'
27
28
 
28
29
  export const createBitcoinAssetFactory = ({ base, apiUrl, coinInfoNetwork }) => ({
29
30
  allowUnconfirmedRbfEnabledUtxos = false,
31
+ ordinalChainIndex = 2,
32
+ ordinalsEnabled = false,
30
33
  overrideCallback = ({ asset }) => asset,
31
34
  } = {}) => {
35
+ assert(typeof ordinalChainIndex === 'number', 'ordinalChainIndex must be a number')
32
36
  assert(base, 'base is required')
33
37
  assert(apiUrl, 'apiUrl is required')
34
38
  const asset = connectAsset(base)
@@ -60,6 +64,7 @@ export const createBitcoinAssetFactory = ({ base, apiUrl, coinInfoNetwork }) =>
60
64
  feesApi: true,
61
65
  moveFunds: true,
62
66
  multipleAddresses: true,
67
+ nfts: ordinalsEnabled,
63
68
  }
64
69
 
65
70
  const getFeeEstimator = getFeeEstimatorFactory({
@@ -102,17 +107,28 @@ export const createBitcoinAssetFactory = ({ base, apiUrl, coinInfoNetwork }) =>
102
107
 
103
108
  const getBalances = getBalancesFactory({ feeData, getSpendableBalance })
104
109
 
110
+ const createHistoryMonitor = (args) =>
111
+ createBitcoinMonitor({
112
+ insightClient,
113
+ ordinalsEnabled,
114
+ ordinalChainIndex,
115
+ apiUrl,
116
+ ...args,
117
+ })
118
+
119
+ const createFeeMonitor = (args) =>
120
+ new BitcoinFeeMonitor({ ...args, insight: () => insightClient, assetName: asset.name })
121
+
105
122
  const api = {
106
123
  defaultAddressPath: 'm/0/0',
107
124
  broadcastTx: (...args) => insightClient.broadcastTx(...args),
108
- createFeeMonitor: (args) =>
109
- new BitcoinFeeMonitor({ ...args, insight: () => insightClient, assetName: asset.name }),
110
- createHistoryMonitor: (args) => createBitcoinMonitor({ insightClient, apiUrl, ...args }),
125
+ createFeeMonitor,
126
+ createHistoryMonitor,
111
127
  getFeeData: () => feeData,
112
128
  getBalances,
113
129
  getKeyIdentifier: createGetKeyIdentifier({ bip44 }),
114
130
  hasFeature: (feature) => !!features[feature],
115
- createAccountState: () => createAccountState({ asset: asset }),
131
+ createAccountState: () => createAccountState({ asset: asset, ordinalsEnabled }),
116
132
  parseUnsignedTx,
117
133
  parseTx: ({ unsignedTx }) => parseUnsignedTx(unsignedTx),
118
134
  signUnsignedTx: (unsignedTx, privateKey, opts) => signTx({ unsignedTx, privateKey, ...opts }), // legacy
@@ -120,6 +136,10 @@ export const createBitcoinAssetFactory = ({ base, apiUrl, coinInfoNetwork }) =>
120
136
  signTx,
121
137
  getFee,
122
138
  moveFunds,
139
+ initialize: ({ assetClientInterface }) => {
140
+ // workaround until createAsset receives assetClientInterface
141
+ api.nfts = nftsApiFactory({ assetClientInterface, ordinalChainIndex, asset })
142
+ },
123
143
  }
124
144
 
125
145
  const fullAsset = {
package/src/nfts.js ADDED
@@ -0,0 +1,109 @@
1
+ import assert from 'minimalistic-assert'
2
+ import { getOrdinalsUtxos, getUtxos, partitionUtxos } from '@exodus/bitcoin-api'
3
+ import { UtxoCollection } from '@exodus/models'
4
+ import { pickBy } from 'lodash'
5
+
6
+ export const nftsApiFactory = ({ assetClientInterface: aci, ordinalChainIndex, asset }) => {
7
+ assert(aci, 'assetClientInterface is required')
8
+ assert(asset, 'asset is required')
9
+ assert(ordinalChainIndex)
10
+ const assetName = asset.name
11
+
12
+ return {
13
+ // this extension returns all possible addresses that may contain nfts
14
+ getNftsAddresses: async ({ walletAccount: walletAccountInstance }) => {
15
+ const walletAccount = walletAccountInstance.toString()
16
+ const accountState = await aci.getAccountState({ walletAccount, assetName })
17
+
18
+ // all the known utxos addresses are monitored,
19
+ // just in case the user received it to a standard btc address
20
+ const allUtxos = accountState.ordinalsUtxos.union(accountState.utxos)
21
+ const addresses = allUtxos.toArray().map((utxos) => utxos.address)
22
+ const specialAddress1 = await aci.getAddress({
23
+ assetName,
24
+ walletAccount,
25
+ purpose: 86,
26
+ chainIndex: ordinalChainIndex,
27
+ addressIndex: 0,
28
+ })
29
+
30
+ const specialAddress2 = await aci.getAddress({
31
+ assetName,
32
+ walletAccount,
33
+ purpose: 86,
34
+ chainIndex: 0,
35
+ addressIndex: 0,
36
+ })
37
+
38
+ const allAddresses = [specialAddress1, specialAddress2, ...addresses].map((t) =>
39
+ t.address.toString()
40
+ )
41
+ return [...new Set(allAddresses)]
42
+ },
43
+
44
+ /*
45
+ This extension performs 2 operations.
46
+ 1. It updates the utxo.data.inscriptionId if there is a nft found via the monitor
47
+ 2. It removes nfts that are in transit, monitor still founds them even when the utxos doesn't exist (it was spent)
48
+ */
49
+ postProcessNfts: async ({ nfts, walletAccount: walletAccountInstance }) => {
50
+ const walletAccount = walletAccountInstance.toString()
51
+ const accountState = await aci.getAccountState({ assetName, walletAccount })
52
+ const storedUtxos = getUtxos({ accountState, asset })
53
+ const storedOrdinalsUtxos = getOrdinalsUtxos({ accountState, asset })
54
+
55
+ const allStoredUtxos = storedUtxos.union(storedOrdinalsUtxos)
56
+ const allUpdatedUtxos = UtxoCollection.fromArray(
57
+ allStoredUtxos.toArray().map((utxo) => {
58
+ if (utxo.inscriptionId) {
59
+ return utxo
60
+ }
61
+ const output = `${utxo.txId}:${utxo.vout}`.toLowerCase()
62
+ const inscriptionId = nfts.find((nft) => nft.output.toLowerCase() === output)?.tokenId
63
+ if (inscriptionId) {
64
+ return { ...utxo, inscriptionId }
65
+ }
66
+ return utxo
67
+ }),
68
+ {
69
+ currency: asset.currency,
70
+ }
71
+ )
72
+
73
+ if (!allUpdatedUtxos.equals(allStoredUtxos)) {
74
+ // nfts sever has found a new inscription
75
+ const { utxos, ordinalsUtxos } = partitionUtxos({
76
+ allUtxos: allUpdatedUtxos,
77
+ ordinalChainIndex: ordinalChainIndex,
78
+ })
79
+ await aci.updateAccountState({
80
+ assetName,
81
+ walletAccount,
82
+ newData: pickBy(
83
+ {
84
+ utxos,
85
+ ordinalsUtxos,
86
+ },
87
+ Boolean
88
+ ),
89
+ })
90
+ }
91
+
92
+ const utxosOutputs = allUpdatedUtxos
93
+ .toArray()
94
+ .map((utxo) => {
95
+ return `${utxo.txId}:${utxo.vout}`.toLowerCase()
96
+ })
97
+ .filter(Boolean)
98
+
99
+ const filteredNfts = nfts.filter((nft) => utxosOutputs.includes(nft.output.toLowerCase()))
100
+ if (filteredNfts.length !== nfts.length) {
101
+ console.log(
102
+ 'Ordinal NFTs in transit',
103
+ nfts.filter((nft) => !utxosOutputs.includes(nft.output.toLowerCase()))
104
+ )
105
+ }
106
+ return filteredNfts
107
+ },
108
+ }
109
+ }