@exodus/bitcoin-plugin 1.5.4 → 1.5.5

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.5.4",
3
+ "version": "1.5.5",
4
4
  "description": "Exodus bitcoin-plugin",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -19,7 +19,7 @@
19
19
  },
20
20
  "dependencies": {
21
21
  "@exodus/asset-lib": "^3.7.1",
22
- "@exodus/bitcoin-api": "^2.5.4",
22
+ "@exodus/bitcoin-api": "^2.5.5",
23
23
  "@exodus/bitcoin-lib": "^2.2.8",
24
24
  "@exodus/text-encoding-utf8": "^1.0.2",
25
25
  "bitcoinjs-lib": "^6.1.5",
@@ -28,5 +28,5 @@
28
28
  "devDependencies": {
29
29
  "@exodus/keychain": "^3.0.0"
30
30
  },
31
- "gitHead": "a75b2bb02cea9bab6146a9a704ef2a191b2d3b49"
31
+ "gitHead": "34e27320059c9cb94b150c3206309bfa5ae54671"
32
32
  }
@@ -205,13 +205,21 @@ export const createBitcoinAssetFactory = ({ asset, apiUrl, coinInfoNetwork }) =>
205
205
  if (tx.sent && Array.isArray(tx.data.sent) && tx.data.sent.length > 0) {
206
206
  const feeAmount = tx.feeAmount.div(tx.data.sent.length)
207
207
  return tx.data.sent
208
- .map((to) =>
209
- tx.update({
208
+ .map((to, i) => {
209
+ if (!to.amount) {
210
+ console.warn(
211
+ `${asset.name} tx ${tx.txId} sent index ${i} does not have an amount!!!`
212
+ )
213
+ }
214
+ return tx.update({
210
215
  to: to.address,
211
- coinAmount: parseCurrency(to.amount, asset.currency).negate(),
216
+ data: { sentIndex: i },
217
+ coinAmount: to.amount
218
+ ? parseCurrency(to.amount, asset.currency).negate()
219
+ : asset.currency.ZERO,
212
220
  feeAmount,
213
221
  })
214
- )
222
+ })
215
223
  .reverse()
216
224
  }
217
225
  return tx
@@ -2,7 +2,6 @@ import assert from 'minimalistic-assert'
2
2
  import { BumpType } from '@exodus/bitcoin-lib/lib/selectors/get-can-bump-tx-factory'
3
3
  import { brc20Utils } from './brc-20-utils'
4
4
  import { createBrc20Command } from './create-brc-20-command'
5
- import { DEFAULT_BRC20_POSTAGE } from './ordinals-constants'
6
5
 
7
6
  export const brc20BalancesAndFeeAdapterFactory = ({
8
7
  inscriptionsService,
@@ -106,7 +105,7 @@ export const brc20BalancesAndFeeAdapterFactory = ({
106
105
  customFee,
107
106
  isSendAll,
108
107
  }) => {
109
- const { getInscriptionFees } = inscriptionsService
108
+ const { getInscriptionFees, createBrc20Inscription } = inscriptionsService
110
109
 
111
110
  const asset = token.baseAsset
112
111
 
@@ -126,18 +125,23 @@ export const brc20BalancesAndFeeAdapterFactory = ({
126
125
 
127
126
  const newInscriptionFees = []
128
127
 
129
- for (const index of Object.keys(newInscriptions)) {
128
+ for (const newInscription of newInscriptions) {
129
+ const inscription = createBrc20Inscription({
130
+ op: 'transfer',
131
+ tick: 'abcd',
132
+ amt: newInscription.amount.toDefaultString({ unit: false }),
133
+ })
130
134
  const newInscriptionFee = getInscriptionFees({
131
135
  asset,
132
136
  getFee,
133
137
  txSet,
134
- postage: DEFAULT_BRC20_POSTAGE,
138
+ inscription,
135
139
  customFee,
136
140
  accountState,
137
141
  feeData,
138
142
  })
139
143
  newInscriptionFees.push(newInscriptionFee)
140
- inscriptionIds.push(`new_inscription_${index}`) // this is just to count how many utxos needs to be send in the last txs
144
+ inscriptionIds.push(`new_inscription`) // this is just to count how many utxos needs to be send in the last txs
141
145
  }
142
146
 
143
147
  const { fee: lastSendingFee } = getFee({
@@ -8,7 +8,7 @@ import { DEFAULT_BRC20_POSTAGE, DEFAULT_ORDINAL_POSTAGE } from './ordinals-const
8
8
 
9
9
  import { witnessStackToScriptWitness } from 'bitcoinjs-lib/src/psbt/psbtutils'
10
10
  import getTxSequence from '@exodus/bitcoin-lib/lib/utils/get-tx-sequence'
11
- import { toXOnly, toAsyncSigner, eccFactory, getOrdinalsUtxos } from '@exodus/bitcoin-api'
11
+ import { eccFactory, getOrdinalsUtxos, toAsyncSigner, toXOnly } from '@exodus/bitcoin-api'
12
12
 
13
13
  const encoder = new TextEncoder()
14
14
  export function createInscriptionScript({ xOnlyPublicKey, inscription }) {
@@ -39,10 +39,22 @@ export const inscriptionsServiceFactory = ({ network, ordinalChainIndex }) => {
39
39
 
40
40
  let ECPair
41
41
 
42
+ function createInscription({ contentType, content, postage }) {
43
+ assert(Number.isSafeInteger(postage), 'postage must be a Safe integer')
44
+ assert(contentType, 'contentType is required integer')
45
+ assert(content, 'content is required integer')
46
+
47
+ return {
48
+ contentType: Buffer.from(encoder.encode(contentType)),
49
+ content: Buffer.from(content),
50
+ postage,
51
+ }
52
+ }
53
+
42
54
  function createTextInscription({ text, postage = DEFAULT_ORDINAL_POSTAGE }) {
43
- const contentType = Buffer.from(encoder.encode('text/plain;charset=utf-8'))
44
- const content = Buffer.from(encoder.encode(text))
45
- return { contentType, content, postage }
55
+ const contentType = 'text/plain;charset=utf-8'
56
+ const content = encoder.encode(text)
57
+ return createInscription({ contentType, content, postage })
46
58
  }
47
59
 
48
60
  function createBrc20Inscription({
@@ -68,9 +80,9 @@ export const inscriptionsServiceFactory = ({ network, ordinalChainIndex }) => {
68
80
  body = { p: 'brc-20', op, tick, max, lim, dec }
69
81
  }
70
82
 
71
- const contentType = Buffer.from(encoder.encode('text/plain;charset=utf-8'))
72
- const content = Buffer.from(encoder.encode(JSON.stringify(body)))
73
- return { contentType, content, postage }
83
+ const contentType = 'text/plain;charset=utf-8'
84
+ const content = encoder.encode(JSON.stringify(body))
85
+ return createInscription({ contentType, content, postage })
74
86
  }
75
87
 
76
88
  function createCommitTxData({ publicKey, inscription }) {
@@ -109,23 +121,13 @@ export const inscriptionsServiceFactory = ({ network, ordinalChainIndex }) => {
109
121
  }
110
122
  }
111
123
 
112
- async function createRevealTx({ commitTxData, commitTxResult, toAddress, privateKey, amount }) {
113
- assert(commitTxData, `commitTxData is required`)
114
- assert(commitTxResult, `commitTxResult is required`)
115
- assert(toAddress, `toAddress is required`)
116
- assert(privateKey instanceof Buffer, `privateKey must be a Buffer`)
117
- assert(typeof amount === 'number', `amount must be a number`)
118
-
119
- const { cblock, scriptTaproot, outputScript } = commitTxData
120
-
124
+ function createPsbt({ scriptTaproot, outputScript, cblock, commitTxResult, amount, toAddress }) {
121
125
  const tapLeafScript = {
122
126
  leafVersion: scriptTaproot.redeemVersion, // 192 0xc0
123
127
  script: outputScript,
124
128
  controlBlock: Buffer.from(cblock, 'hex'),
125
129
  }
126
130
 
127
- ECPair = ECPairFactory(ecc)
128
- const keypair = ECPair.fromPrivateKey(privateKey, { network })
129
131
  const psbt = new bitcoinjsLib.Psbt({ network })
130
132
  psbt.addInput({
131
133
  hash: commitTxResult.txId,
@@ -139,15 +141,13 @@ export const inscriptionsServiceFactory = ({ network, ordinalChainIndex }) => {
139
141
  value: amount, // generally 1000 for nfts, 549 for brc20
140
142
  address: toAddress.toString(),
141
143
  })
144
+ return { tapLeafScript, psbt }
145
+ }
142
146
 
143
- await psbt.signInputAsync(0, toAsyncSigner({ keyPair: keypair, ecc }))
144
-
145
- const signature = psbt.data.inputs[0].tapScriptSig[0].signature.toString('hex')
146
-
147
- // We have to construct our witness script in a custom finalizer
148
- const customFinalizer = (_inputIndex: number, input: any) => {
149
- const witness = [input.tapScriptSig[0].signature]
150
- .concat(outputScript)
147
+ function finalizePsbt({ psbt, signature, commitTxData, tapLeafScript }) {
148
+ const customFinalizer = () => {
149
+ const witness = [Buffer.from(signature, 'hex')]
150
+ .concat(commitTxData.outputScript)
151
151
  .concat(tapLeafScript.controlBlock)
152
152
 
153
153
  return {
@@ -156,14 +156,71 @@ export const inscriptionsServiceFactory = ({ network, ordinalChainIndex }) => {
156
156
  }
157
157
 
158
158
  psbt.finalizeInput(0, customFinalizer)
159
+ return psbt.extractTransaction()
160
+ }
159
161
 
160
- const tx = psbt.extractTransaction()
162
+ const estimationStaticData = {
163
+ toAddress: 'bc1p4vgmzfzu642zxj04525xr8szd446ydct3y44dqc7wg88wd8e4v9qzaz7k9',
164
+ publicKey: Buffer.from(
165
+ '041b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f70beaf8f588b541507fed6a642c5ab42dfdf8120a7f639de5122d47a69a8e8d1',
166
+ 'hex'
167
+ ),
168
+ txId: '2222222222222222222222222222222222222222222222222222222222222222',
169
+ signature: Buffer.alloc(64, 3),
170
+ }
161
171
 
162
- const rawTx = tx.toBuffer().toString('hex')
163
- const txId = tx.getId()
172
+ const estimateTxSize = ({ inscription }) => {
173
+ const { publicKey, txId, toAddress, signature } = estimationStaticData
164
174
 
165
- const virtualSize = tx.virtualSize()
175
+ const commitTxData = createCommitTxData({ publicKey, inscription })
176
+
177
+ const amount = inscription.postage
178
+
179
+ const commitTxResult = {
180
+ txId,
181
+ sendUtxoIndex: 0,
182
+ sendAmount: 100000,
183
+ }
184
+
185
+ const { tapLeafScript, psbt } = createPsbt({
186
+ commitTxResult,
187
+ amount,
188
+ toAddress,
189
+ ...commitTxData,
190
+ })
191
+
192
+ const tx = finalizePsbt({ signature, commitTxData, tapLeafScript, psbt })
193
+
194
+ return tx.virtualSize()
195
+ }
166
196
 
197
+ async function createRevealTx({ commitTxData, commitTxResult, toAddress, privateKey, amount }) {
198
+ assert(commitTxData, `commitTxData is required`)
199
+ assert(commitTxResult, `commitTxResult is required`)
200
+ assert(toAddress, `toAddress is required`)
201
+ assert(privateKey instanceof Buffer, `privateKey must be a Buffer`)
202
+ assert(typeof amount === 'number', `amount must be a number`)
203
+
204
+ const { tapLeafScript, psbt } = createPsbt({
205
+ commitTxResult,
206
+ amount,
207
+ toAddress,
208
+ ...commitTxData,
209
+ })
210
+
211
+ ECPair = ECPair || ECPairFactory(ecc)
212
+
213
+ const keypair = ECPair.fromPrivateKey(privateKey, { network })
214
+
215
+ await psbt.signInputAsync(0, toAsyncSigner({ keyPair: keypair, ecc }))
216
+
217
+ const signature = psbt.data.inputs[0].tapScriptSig[0].signature.toString('hex')
218
+
219
+ const tx = finalizePsbt({ amount, signature, commitTxData, tapLeafScript, psbt })
220
+
221
+ const virtualSize = tx.virtualSize()
222
+ const rawTx = tx.toBuffer().toString('hex')
223
+ const txId = tx.getId()
167
224
  const vout = 0
168
225
  const script = tx.outs[vout].script.toString('hex')
169
226
  const inscriptionId = `${txId}i0`
@@ -180,6 +237,15 @@ export const inscriptionsServiceFactory = ({ network, ordinalChainIndex }) => {
180
237
  }
181
238
  }
182
239
 
240
+ const resolveMinersFee = ({ inscription, customFee, feeData, asset }) => {
241
+ assert(Number.isSafeInteger(inscription.postage), 'inscription.postage must be a safe integer')
242
+ const txSize = estimateTxSize({ inscription })
243
+ const feePerKB = customFee || feeData.feePerKB
244
+ const minersFeeBaseUnit = Math.ceil((feePerKB.toBaseNumber() * txSize) / 1000)
245
+ assert(Number.isSafeInteger(minersFeeBaseUnit), 'minersFeeBaseUnit must be a safe integer')
246
+ return asset.currency.baseUnit(minersFeeBaseUnit)
247
+ }
248
+
183
249
  const broadcastInscription = async ({
184
250
  asset,
185
251
  walletAccount, // The portfolio that spends the fees when creating the inscription. Also, the portfolio that receives the inscription if the 2 belows are not provided
@@ -219,33 +285,17 @@ export const inscriptionsServiceFactory = ({ network, ordinalChainIndex }) => {
219
285
  const publicKey = secp256k1.pointFromScalar(privateKey, false)
220
286
  const commitTxData = createCommitTxData({ publicKey, inscription })
221
287
 
222
- // simulate to get size
223
- const { virtualSize: txSize } = await createRevealTx({
224
- commitTxData,
225
- amount: postage,
226
- commitTxResult: {
227
- txId: randomBytes(32).toString('hex'),
228
- sendUtxoIndex: 0,
229
- sendAmount: 100000,
230
- },
231
- toAddress,
232
- privateKey,
233
- })
234
-
235
288
  const feeData = await assetClientInterface.getFeeConfig({ assetName })
236
289
 
237
- const feePerKB = customFee || feeData.feePerKB
238
-
239
- const minersFee = Math.ceil((feePerKB.toBaseNumber() * txSize) / 1000)
240
-
241
- const commitAmount = minersFee + postage
290
+ const minersFee = resolveMinersFee({ inscription, customFee, feeData, asset })
291
+ const commitAmount = minersFee.add(asset.currency.baseUnit(postage))
242
292
 
243
293
  const commitTxResult = await asset.api.sendTx(
244
294
  {
245
295
  asset,
246
296
  walletAccount,
247
297
  address: commitTxData.revealAddress,
248
- amount: asset.currency.baseUnit(commitAmount),
298
+ amount: commitAmount,
249
299
  options: { isRbfAllowed: false, customFee },
250
300
  },
251
301
  { assetClientInterface }
@@ -286,20 +336,23 @@ export const inscriptionsServiceFactory = ({ network, ordinalChainIndex }) => {
286
336
  }
287
337
 
288
338
  const getInscriptionFees = ({
339
+ accountState,
289
340
  asset,
290
- getFee,
291
- txSet,
292
- postage,
293
341
  customFee,
294
- accountState,
295
342
  feeData,
343
+ getFee,
344
+ inscription,
345
+ txSet,
296
346
  }) => {
347
+ assert(accountState, 'accountState is required')
297
348
  assert(asset, 'asset is required')
298
- assert(postage, 'postage is required')
299
- const txSize = 139 // remove hardcoded number! // simulate to get size
300
- const feePerKB = customFee || feeData.feePerKB
301
- const minersFee = asset.currency.baseUnit(Math.ceil((feePerKB.toBaseNumber() * txSize) / 1000))
302
- const postageFee = asset.currency.baseUnit(postage)
349
+ assert(feeData, 'feeData is required')
350
+ assert(getFee, 'getFee is required')
351
+ assert(inscription, 'inscription is required')
352
+ assert(txSet, 'txSet is required')
353
+
354
+ const minersFee = resolveMinersFee({ inscription, customFee, feeData, asset })
355
+ const postageFee = asset.currency.baseUnit(inscription.postage)
303
356
  // Total fees: postage + fee of sending from wallet => inscribe taproot address + fee of sending inscribe taproot address => wallet
304
357
  const { fee: foundInscriptionFee } = getFee({
305
358
  asset,
@@ -326,6 +379,7 @@ export const inscriptionsServiceFactory = ({ network, ordinalChainIndex }) => {
326
379
  createCommitTxData,
327
380
  createRevealTx,
328
381
  broadcastInscription,
382
+ estimateTxSize,
329
383
  getInscriptionFees,
330
384
  }
331
385
  }
@@ -0,0 +1,18 @@
1
+ export const isNftBrc20 = (nft) => {
2
+ if (nft.isBrc20) {
3
+ return true
4
+ }
5
+ // Void/Spent brc20 are not flagged as brc20
6
+ const isTextContentType =
7
+ nft.contentType?.startsWith('text/plain') || nft.contentType?.startsWith('application/json')
8
+
9
+ if (!isTextContentType || !nft.contentBody) {
10
+ return false
11
+ }
12
+
13
+ try {
14
+ const json = JSON.parse(Buffer.from(nft.contentBody, 'base64').toString())
15
+ return json?.p.toLowerCase() === 'brc-20'
16
+ } catch (e) {}
17
+ return false
18
+ }
@@ -1,6 +1,7 @@
1
1
  import assert from 'minimalistic-assert'
2
2
  import { getInscriptionAddresses } from './get-inscription-addresses'
3
3
  import { updateAccountStateUtxos } from './ordinals-account-state-utils'
4
+ import { isNftBrc20 } from './nft-utils'
4
5
 
5
6
  export const nftsApiFactory = ({ ordinalChainIndex, asset }) => {
6
7
  assert(asset, 'asset is required')
@@ -52,14 +53,12 @@ export const nftsApiFactory = ({ ordinalChainIndex, asset }) => {
52
53
  })
53
54
  .filter(Boolean)
54
55
 
55
- const filteredNfts = nfts.filter((nft) => utxosOutputs.includes(nft.output.toLowerCase()))
56
- if (filteredNfts.length !== nfts.length) {
57
- console.log(
58
- 'Ordinal NFTs in transit',
59
- nfts.filter((nft) => !utxosOutputs.includes(nft.output.toLowerCase()))
60
- )
61
- }
62
- return filteredNfts.filter((nft) => !nft.isBrc20)
56
+ const expandedNfts = nfts.map((nft) => ({
57
+ ...nft,
58
+ inTransit: !utxosOutputs.includes(nft.output.toLowerCase()),
59
+ }))
60
+
61
+ return expandedNfts.filter((nft) => !isNftBrc20(nft))
63
62
  },
64
63
  }
65
64
  }