@exodus/bitcoin-plugin 1.5.2 → 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 +4 -3
- package/src/create-asset.js +12 -4
- package/src/ordinals/brc-20-balances-and-fee-adapter.js +76 -12
- package/src/ordinals/brc-20-utils.js +28 -11
- package/src/ordinals/get-inscription-addresses.js +2 -1
- package/src/ordinals/inscriptions-service.js +148 -55
- package/src/ordinals/nft-utils.js +18 -0
- package/src/ordinals/nfts.js +7 -8
- package/src/ordinals/reload-brc20-token-balances.js +3 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@exodus/bitcoin-plugin",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.5",
|
|
4
4
|
"description": "Exodus bitcoin-plugin",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"files": [
|
|
@@ -19,13 +19,14 @@
|
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"@exodus/asset-lib": "^3.7.1",
|
|
22
|
-
"@exodus/bitcoin-api": "^2.5.
|
|
22
|
+
"@exodus/bitcoin-api": "^2.5.5",
|
|
23
23
|
"@exodus/bitcoin-lib": "^2.2.8",
|
|
24
|
+
"@exodus/text-encoding-utf8": "^1.0.2",
|
|
24
25
|
"bitcoinjs-lib": "^6.1.5",
|
|
25
26
|
"minimalistic-assert": "^1.0.1"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|
|
28
29
|
"@exodus/keychain": "^3.0.0"
|
|
29
30
|
},
|
|
30
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "34e27320059c9cb94b150c3206309bfa5ae54671"
|
|
31
32
|
}
|
package/src/create-asset.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
|
@@ -28,12 +28,14 @@ export const brc20BalancesAndFeeAdapterFactory = ({
|
|
|
28
28
|
return !options.feeOpts.inscriptionIds?.length
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
const
|
|
31
|
+
const sendBrc20 = async (
|
|
32
32
|
{ asset: token, walletAccount, address, amount, options },
|
|
33
33
|
{ assetClientInterface }
|
|
34
34
|
) => {
|
|
35
35
|
const { broadcastInscription, createBrc20Inscription } = inscriptionsService
|
|
36
36
|
|
|
37
|
+
const { customFee, isSendAll } = options
|
|
38
|
+
|
|
37
39
|
const asset = token.baseAsset
|
|
38
40
|
const accountState = await assetClientInterface.getAccountState({
|
|
39
41
|
walletAccount,
|
|
@@ -42,7 +44,8 @@ export const brc20BalancesAndFeeAdapterFactory = ({
|
|
|
42
44
|
|
|
43
45
|
const tick = await tickInscriptionIdIndexer.getBrc20TickFromAsset(token)
|
|
44
46
|
|
|
45
|
-
const tokenAmount =
|
|
47
|
+
const tokenAmount =
|
|
48
|
+
(isSendAll ? getTokenBalance({ asset: token, accountState }) : amount) || token.currency.ZERO
|
|
46
49
|
|
|
47
50
|
if (tokenAmount.lte(token.currency.ZERO)) {
|
|
48
51
|
throw new Error(
|
|
@@ -52,17 +55,14 @@ export const brc20BalancesAndFeeAdapterFactory = ({
|
|
|
52
55
|
)
|
|
53
56
|
}
|
|
54
57
|
|
|
55
|
-
const
|
|
58
|
+
const { inscriptionIds, newInscriptions } = createBrc20Command({
|
|
56
59
|
asset: token,
|
|
57
60
|
amount: tokenAmount,
|
|
58
61
|
accountState,
|
|
59
62
|
})
|
|
60
63
|
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
for (const newInscription of brc20Command.newInscriptions) {
|
|
64
|
-
const customFee = asset.currency.baseUnit(20000) // why 64k is te default?
|
|
65
|
-
const inscription = await createBrc20Inscription({
|
|
64
|
+
for (const newInscription of newInscriptions) {
|
|
65
|
+
const inscription = createBrc20Inscription({
|
|
66
66
|
op: 'transfer',
|
|
67
67
|
tick,
|
|
68
68
|
amt: newInscription.amount.toDefaultString({ unit: false }),
|
|
@@ -96,6 +96,72 @@ export const brc20BalancesAndFeeAdapterFactory = ({
|
|
|
96
96
|
)
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
+
const getFeeBrc20 = ({
|
|
100
|
+
asset: token,
|
|
101
|
+
accountState,
|
|
102
|
+
txSet,
|
|
103
|
+
feeData,
|
|
104
|
+
amount,
|
|
105
|
+
customFee,
|
|
106
|
+
isSendAll,
|
|
107
|
+
}) => {
|
|
108
|
+
const { getInscriptionFees, createBrc20Inscription } = inscriptionsService
|
|
109
|
+
|
|
110
|
+
const asset = token.baseAsset
|
|
111
|
+
|
|
112
|
+
const availableBalance = getTokenBalance({ asset: token, accountState })
|
|
113
|
+
|
|
114
|
+
const tokenAmount = (isSendAll ? availableBalance : amount) || token.currency.ZERO
|
|
115
|
+
|
|
116
|
+
if (tokenAmount.lte(token.currency.ZERO) || availableBalance.lt(tokenAmount)) {
|
|
117
|
+
return { fee: asset.currency.ZERO }
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const { inscriptionIds, newInscriptions } = createBrc20Command({
|
|
121
|
+
asset: token,
|
|
122
|
+
amount: tokenAmount,
|
|
123
|
+
accountState,
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
const newInscriptionFees = []
|
|
127
|
+
|
|
128
|
+
for (const newInscription of newInscriptions) {
|
|
129
|
+
const inscription = createBrc20Inscription({
|
|
130
|
+
op: 'transfer',
|
|
131
|
+
tick: 'abcd',
|
|
132
|
+
amt: newInscription.amount.toDefaultString({ unit: false }),
|
|
133
|
+
})
|
|
134
|
+
const newInscriptionFee = getInscriptionFees({
|
|
135
|
+
asset,
|
|
136
|
+
getFee,
|
|
137
|
+
txSet,
|
|
138
|
+
inscription,
|
|
139
|
+
customFee,
|
|
140
|
+
accountState,
|
|
141
|
+
feeData,
|
|
142
|
+
})
|
|
143
|
+
newInscriptionFees.push(newInscriptionFee)
|
|
144
|
+
inscriptionIds.push(`new_inscription`) // this is just to count how many utxos needs to be send in the last txs
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const { fee: lastSendingFee } = getFee({
|
|
148
|
+
asset,
|
|
149
|
+
accountState,
|
|
150
|
+
txSet,
|
|
151
|
+
feeData,
|
|
152
|
+
amount: asset.currency.ZERO,
|
|
153
|
+
customFee,
|
|
154
|
+
brc20: { inscriptionIds },
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
const fee = newInscriptionFees.reduce(
|
|
158
|
+
(total, newInscriptionFee) => total.add(newInscriptionFee.totalFee),
|
|
159
|
+
lastSendingFee
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
return { fee, lastSendingFee, newInscriptionFees }
|
|
163
|
+
}
|
|
164
|
+
|
|
99
165
|
return {
|
|
100
166
|
canBumpTx: (args) => {
|
|
101
167
|
return isToken(args) ? { bumpType: BumpType.NONE } : canBumpTx(args)
|
|
@@ -106,7 +172,7 @@ export const brc20BalancesAndFeeAdapterFactory = ({
|
|
|
106
172
|
},
|
|
107
173
|
|
|
108
174
|
getFee: (args) => {
|
|
109
|
-
return isToken(args) ?
|
|
175
|
+
return isToken(args) ? getFeeBrc20(args) : getFee(args)
|
|
110
176
|
},
|
|
111
177
|
|
|
112
178
|
getAvailableBalance: (args) => {
|
|
@@ -118,9 +184,7 @@ export const brc20BalancesAndFeeAdapterFactory = ({
|
|
|
118
184
|
},
|
|
119
185
|
|
|
120
186
|
sendTx: (...args) => {
|
|
121
|
-
return isToken(args[0]) && isInscription(...args)
|
|
122
|
-
? createAndBroadcastInscription(...args)
|
|
123
|
-
: sendTx(...args)
|
|
187
|
+
return isToken(args[0]) && isInscription(...args) ? sendBrc20(...args) : sendTx(...args)
|
|
124
188
|
},
|
|
125
189
|
}
|
|
126
190
|
}
|
|
@@ -15,28 +15,45 @@ export const brc20Utils = {
|
|
|
15
15
|
assert(accountState, 'accountState is required')
|
|
16
16
|
assert(asset, 'asset is required')
|
|
17
17
|
const assetBalances = Object.values(accountState?.brc20Balances?.[asset.name] || {})
|
|
18
|
+
const knownInscriptionIds = brc20Utils.getKnownInscriptionIds({ asset, accountState })
|
|
18
19
|
return assetBalances
|
|
19
20
|
.flatMap((item) => item.transferableList)
|
|
20
|
-
.map((item) =>
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
.map((item) => {
|
|
22
|
+
const sent = item.sent
|
|
23
|
+
const ordinalUtxoKnown = knownInscriptionIds.includes(item.inscriptionId)
|
|
24
|
+
return {
|
|
25
|
+
inscriptionId: item.inscriptionId,
|
|
26
|
+
inscriptionNumber: item.inscriptionNumber,
|
|
27
|
+
txId: item.txId,
|
|
28
|
+
amount: asset.currency.defaultUnit(item.brc20Amt),
|
|
29
|
+
sent: sent,
|
|
30
|
+
isOrdinalUtxoKnown: ordinalUtxoKnown,
|
|
31
|
+
valid: !sent && ordinalUtxoKnown,
|
|
32
|
+
}
|
|
33
|
+
})
|
|
34
|
+
.sort((a, b) => {
|
|
35
|
+
// valid first, then by inscription number descending
|
|
36
|
+
if (a.valid && !b.valid) return -1
|
|
37
|
+
if (!a.valid && b.valid) return 1
|
|
38
|
+
return b.inscriptionNumber - a.inscriptionNumber
|
|
39
|
+
})
|
|
25
40
|
},
|
|
26
41
|
|
|
27
42
|
getTransferableList: ({ asset, accountState }) => {
|
|
28
|
-
const knownInscriptionIds = brc20Utils.getKnownInscriptionIds({ asset, accountState })
|
|
29
43
|
const transferableList = brc20Utils.getAllTransferableList({ asset, accountState })
|
|
30
44
|
// return the list of transferable inscription items that we hold of the utxos
|
|
31
|
-
return transferableList
|
|
45
|
+
return transferableList
|
|
46
|
+
.filter((item) => item.valid)
|
|
47
|
+
.sort((a, b) => {
|
|
48
|
+
// let's consume old ones first!
|
|
49
|
+
return a.inscriptionNumber - b.inscriptionNumber
|
|
50
|
+
})
|
|
32
51
|
},
|
|
33
52
|
|
|
34
53
|
getInTransitBalance: ({ asset, accountState }) => {
|
|
35
|
-
const knownInscriptionIds = brc20Utils.getKnownInscriptionIds({ asset, accountState })
|
|
36
54
|
const transferableList = brc20Utils.getAllTransferableList({ asset, accountState })
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
.reduce((amount, item) => amount.add(item.amount), asset.currency.ZERO)
|
|
55
|
+
const inTransitList = transferableList.filter((item) => !item.isOrdinalUtxoKnown && !item.sent)
|
|
56
|
+
return inTransitList.reduce((amount, item) => amount.add(item.amount), asset.currency.ZERO)
|
|
40
57
|
},
|
|
41
58
|
|
|
42
59
|
isToken: ({ asset }) => {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import assert from 'minimalistic-assert'
|
|
2
|
+
import { getOrdinalsUtxos } from '@exodus/bitcoin-api'
|
|
2
3
|
|
|
3
4
|
export const getInscriptionAddresses = async ({
|
|
4
5
|
assetClientInterface: aci,
|
|
@@ -17,7 +18,7 @@ export const getInscriptionAddresses = async ({
|
|
|
17
18
|
|
|
18
19
|
// all the known utxos addresses are monitored,
|
|
19
20
|
// just in case the user received it to a standard btc address
|
|
20
|
-
const allUtxos = accountState.
|
|
21
|
+
const allUtxos = getOrdinalsUtxos({ accountState, asset }).union(accountState.utxos)
|
|
21
22
|
const addresses = allUtxos.toArray().map((utxos) => utxos.address)
|
|
22
23
|
const specialAddress1 = await aci.getAddress({
|
|
23
24
|
assetName,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import assert from 'minimalistic-assert'
|
|
2
2
|
import * as bitcoinjsLib from 'bitcoinjs-lib'
|
|
3
|
-
|
|
3
|
+
import { TextEncoder } from '@exodus/text-encoding-utf8'
|
|
4
4
|
import ECPairFactory from 'ecpair'
|
|
5
5
|
import { randomBytes } from '@exodus/crypto/randomBytes'
|
|
6
6
|
import secp256k1 from 'tiny-secp256k1'
|
|
@@ -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 {
|
|
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 =
|
|
44
|
-
const content =
|
|
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 =
|
|
72
|
-
const content =
|
|
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
|
-
|
|
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
|
-
|
|
144
|
-
|
|
145
|
-
|
|
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
|
-
|
|
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
|
-
|
|
163
|
-
const txId =
|
|
172
|
+
const estimateTxSize = ({ inscription }) => {
|
|
173
|
+
const { publicKey, txId, toAddress, signature } = estimationStaticData
|
|
164
174
|
|
|
165
|
-
const
|
|
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
|
+
}
|
|
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 })
|
|
166
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
|
|
@@ -199,6 +265,11 @@ export const inscriptionsServiceFactory = ({ network, ordinalChainIndex }) => {
|
|
|
199
265
|
|
|
200
266
|
const resolvedReceiverWalletAccount = receiverWalletAccount || walletAccount
|
|
201
267
|
|
|
268
|
+
const accountState = await assetClientInterface.getAccountState({
|
|
269
|
+
assetName,
|
|
270
|
+
walletAccount: resolvedReceiverWalletAccount,
|
|
271
|
+
})
|
|
272
|
+
|
|
202
273
|
const toAddress =
|
|
203
274
|
receiverAddress ||
|
|
204
275
|
(await assetClientInterface.getAddress({
|
|
@@ -214,33 +285,17 @@ export const inscriptionsServiceFactory = ({ network, ordinalChainIndex }) => {
|
|
|
214
285
|
const publicKey = secp256k1.pointFromScalar(privateKey, false)
|
|
215
286
|
const commitTxData = createCommitTxData({ publicKey, inscription })
|
|
216
287
|
|
|
217
|
-
// simulate to get size
|
|
218
|
-
const { virtualSize: txSize } = await createRevealTx({
|
|
219
|
-
commitTxData,
|
|
220
|
-
amount: postage,
|
|
221
|
-
commitTxResult: {
|
|
222
|
-
txId: randomBytes(32).toString('hex'),
|
|
223
|
-
sendUtxoIndex: 0,
|
|
224
|
-
sendAmount: 100000,
|
|
225
|
-
},
|
|
226
|
-
toAddress,
|
|
227
|
-
privateKey,
|
|
228
|
-
})
|
|
229
|
-
|
|
230
288
|
const feeData = await assetClientInterface.getFeeConfig({ assetName })
|
|
231
289
|
|
|
232
|
-
const
|
|
233
|
-
|
|
234
|
-
const minersFee = Math.ceil((feePerKB.toBaseNumber() * txSize) / 1000)
|
|
235
|
-
|
|
236
|
-
const commitAmount = minersFee + postage
|
|
290
|
+
const minersFee = resolveMinersFee({ inscription, customFee, feeData, asset })
|
|
291
|
+
const commitAmount = minersFee.add(asset.currency.baseUnit(postage))
|
|
237
292
|
|
|
238
293
|
const commitTxResult = await asset.api.sendTx(
|
|
239
294
|
{
|
|
240
295
|
asset,
|
|
241
296
|
walletAccount,
|
|
242
297
|
address: commitTxData.revealAddress,
|
|
243
|
-
amount:
|
|
298
|
+
amount: commitAmount,
|
|
244
299
|
options: { isRbfAllowed: false, customFee },
|
|
245
300
|
},
|
|
246
301
|
{ assetClientInterface }
|
|
@@ -255,12 +310,10 @@ export const inscriptionsServiceFactory = ({ network, ordinalChainIndex }) => {
|
|
|
255
310
|
})
|
|
256
311
|
|
|
257
312
|
if (!receiverAddress && resolvedReceiverWalletAccount) {
|
|
258
|
-
const accountState = await assetClientInterface.getAccountState({
|
|
259
|
-
assetName,
|
|
260
|
-
walletAccount: resolvedReceiverWalletAccount,
|
|
261
|
-
})
|
|
262
313
|
// only add the utxo
|
|
263
|
-
const
|
|
314
|
+
const storedOrdinalsUtxos = getOrdinalsUtxos({ accountState, asset })
|
|
315
|
+
|
|
316
|
+
const ordinalsUtxos = storedOrdinalsUtxos.addUtxo({
|
|
264
317
|
txId: revealTx.txId,
|
|
265
318
|
address: toAddress,
|
|
266
319
|
vout: revealTx.vout,
|
|
@@ -282,11 +335,51 @@ export const inscriptionsServiceFactory = ({ network, ordinalChainIndex }) => {
|
|
|
282
335
|
return { ...revealTx, ...commitTxData }
|
|
283
336
|
}
|
|
284
337
|
|
|
338
|
+
const getInscriptionFees = ({
|
|
339
|
+
accountState,
|
|
340
|
+
asset,
|
|
341
|
+
customFee,
|
|
342
|
+
feeData,
|
|
343
|
+
getFee,
|
|
344
|
+
inscription,
|
|
345
|
+
txSet,
|
|
346
|
+
}) => {
|
|
347
|
+
assert(accountState, 'accountState is required')
|
|
348
|
+
assert(asset, 'asset is required')
|
|
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)
|
|
356
|
+
// Total fees: postage + fee of sending from wallet => inscribe taproot address + fee of sending inscribe taproot address => wallet
|
|
357
|
+
const { fee: foundInscriptionFee } = getFee({
|
|
358
|
+
asset,
|
|
359
|
+
accountState,
|
|
360
|
+
txSet,
|
|
361
|
+
feeData,
|
|
362
|
+
amount: minersFee.add(postageFee),
|
|
363
|
+
customFee,
|
|
364
|
+
isSendAll: false,
|
|
365
|
+
receiveAddress: 'P2TR',
|
|
366
|
+
})
|
|
367
|
+
|
|
368
|
+
return {
|
|
369
|
+
totalFee: foundInscriptionFee.add(minersFee).add(postageFee),
|
|
370
|
+
foundInscriptionFee,
|
|
371
|
+
minersFee,
|
|
372
|
+
postageFee,
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
285
376
|
return {
|
|
286
377
|
createTextInscription,
|
|
287
378
|
createBrc20Inscription,
|
|
288
379
|
createCommitTxData,
|
|
289
380
|
createRevealTx,
|
|
290
381
|
broadcastInscription,
|
|
382
|
+
estimateTxSize,
|
|
383
|
+
getInscriptionFees,
|
|
291
384
|
}
|
|
292
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
|
+
}
|
package/src/ordinals/nfts.js
CHANGED
|
@@ -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
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
}
|
|
@@ -8,7 +8,7 @@ const toTxLog = ({ historyItem, baseAsset, asset }) => {
|
|
|
8
8
|
const isSend = historyItem.brc20Op === 'send'
|
|
9
9
|
const isReceive = historyItem.brc20Op === 'receive'
|
|
10
10
|
|
|
11
|
-
const data = {
|
|
11
|
+
const data = { operation: historyItem.brc20Op }
|
|
12
12
|
return {
|
|
13
13
|
txId: historyItem.txId,
|
|
14
14
|
date: new Date(historyItem.createdAt),
|
|
@@ -108,13 +108,14 @@ export const reloadBrc20TokenBalances = async ({
|
|
|
108
108
|
.map((event) => event.inscriptionId)
|
|
109
109
|
|
|
110
110
|
const transferableList = events
|
|
111
|
-
.filter((event) => event.brc20Op === 'transfer'
|
|
111
|
+
.filter((event) => event.brc20Op === 'transfer')
|
|
112
112
|
.map((event) => {
|
|
113
113
|
return {
|
|
114
114
|
inscriptionId: event.inscriptionId,
|
|
115
115
|
inscriptionNumber: event.inscriptionNumber,
|
|
116
116
|
brc20Amt: event.brc20Amt,
|
|
117
117
|
txId: event.txId,
|
|
118
|
+
sent: sentList.includes(event.inscriptionId),
|
|
118
119
|
}
|
|
119
120
|
})
|
|
120
121
|
return {
|