@exodus/bitcoin-plugin 1.32.0 → 1.32.2
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/CHANGELOG.md +22 -0
- package/package.json +4 -6
- package/src/create-asset.js +13 -83
- package/src/ordinals/ordinals-constants.js +0 -5
- package/src/web3/createSimulateTransactions.js +16 -6
- package/src/ordinals/brc-20-balances-and-fee-adapter.js +0 -212
- package/src/ordinals/brc-20-client.js +0 -120
- package/src/ordinals/brc-20-utils.js +0 -137
- package/src/ordinals/create-brc-20-command.js +0 -51
- package/src/ordinals/create-token-factory.js +0 -31
- package/src/ordinals/inscriptions-service.js +0 -377
- package/src/ordinals/reload-brc20-token-balances.js +0 -158
- package/src/ordinals/tick-inscription-id-indexer.js +0 -49
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import { getOrdinalsUtxos } from '@exodus/bitcoin-api'
|
|
2
|
-
import lodash from 'lodash'
|
|
3
|
-
import assert from 'minimalistic-assert'
|
|
4
|
-
|
|
5
|
-
const { sumBy } = lodash
|
|
6
|
-
|
|
7
|
-
export const brc20Utils = {
|
|
8
|
-
getKnownInscriptionIds: ({ asset, accountState }) => {
|
|
9
|
-
assert(accountState, 'accountState is required')
|
|
10
|
-
assert(asset, 'asset is required')
|
|
11
|
-
return getOrdinalsUtxos({ accountState, asset })
|
|
12
|
-
.toArray()
|
|
13
|
-
.flatMap((utxo) => utxo.inscriptions?.map((i) => i.inscriptionId))
|
|
14
|
-
},
|
|
15
|
-
|
|
16
|
-
getAllTransferableList: ({ accountState, asset }) => {
|
|
17
|
-
assert(accountState, 'accountState is required')
|
|
18
|
-
assert(asset, 'asset is required')
|
|
19
|
-
const assetBalances = Object.values(accountState?.brc20Balances?.[asset.name] || {})
|
|
20
|
-
const knownInscriptionIds = brc20Utils.getKnownInscriptionIds({ asset, accountState })
|
|
21
|
-
return assetBalances
|
|
22
|
-
.flatMap((item) => item.transferableList)
|
|
23
|
-
.map((item) => {
|
|
24
|
-
const sent = item.sent
|
|
25
|
-
const ordinalUtxoKnown = knownInscriptionIds.includes(item.inscriptionId)
|
|
26
|
-
return {
|
|
27
|
-
inscriptionId: item.inscriptionId,
|
|
28
|
-
inscriptionNumber: item.inscriptionNumber,
|
|
29
|
-
txId: item.txId,
|
|
30
|
-
amount: asset.currency.defaultUnit(item.brc20Amt),
|
|
31
|
-
sent,
|
|
32
|
-
isOrdinalUtxoKnown: ordinalUtxoKnown,
|
|
33
|
-
valid: !sent && ordinalUtxoKnown,
|
|
34
|
-
}
|
|
35
|
-
})
|
|
36
|
-
.sort((a, b) => {
|
|
37
|
-
// valid first, then by inscription number descending
|
|
38
|
-
if (a.valid && !b.valid) return -1
|
|
39
|
-
if (!a.valid && b.valid) return 1
|
|
40
|
-
return b.inscriptionNumber - a.inscriptionNumber
|
|
41
|
-
})
|
|
42
|
-
},
|
|
43
|
-
|
|
44
|
-
getTransferableList: ({ asset, accountState }) => {
|
|
45
|
-
const transferableList = brc20Utils.getAllTransferableList({ asset, accountState })
|
|
46
|
-
// return the list of transferable inscription items that we hold of the utxos
|
|
47
|
-
return transferableList
|
|
48
|
-
.filter((item) => item.valid)
|
|
49
|
-
.sort((a, b) => {
|
|
50
|
-
// let's consume old ones first!
|
|
51
|
-
return a.inscriptionNumber - b.inscriptionNumber
|
|
52
|
-
})
|
|
53
|
-
},
|
|
54
|
-
|
|
55
|
-
getInTransitBalance: ({ asset, accountState }) => {
|
|
56
|
-
const transferableList = brc20Utils.getAllTransferableList({ asset, accountState })
|
|
57
|
-
const inTransitList = transferableList.filter((item) => !item.isOrdinalUtxoKnown && !item.sent)
|
|
58
|
-
return inTransitList.reduce((amount, item) => amount.add(item.amount), asset.currency.ZERO)
|
|
59
|
-
},
|
|
60
|
-
|
|
61
|
-
isToken: ({ asset }) => {
|
|
62
|
-
assert(asset, 'asset must be provided')
|
|
63
|
-
return asset.name !== asset.baseAsset.name
|
|
64
|
-
},
|
|
65
|
-
|
|
66
|
-
getTokenBalance: ({ asset, accountState }) => {
|
|
67
|
-
assert(asset, 'asset must be provided')
|
|
68
|
-
assert(accountState, 'accountState must be provided')
|
|
69
|
-
assert(brc20Utils.isToken({ asset }), `${asset.name} is must be a token`)
|
|
70
|
-
const assetBalances = Object.values(accountState?.brc20Balances?.[asset.name] || {})
|
|
71
|
-
const sum = sumBy(assetBalances, 'brc20OwnerTotalBalance')
|
|
72
|
-
return asset.currency
|
|
73
|
-
.defaultUnit(sum || 0)
|
|
74
|
-
.sub(brc20Utils.getInTransitBalance({ asset, accountState }))
|
|
75
|
-
.clampLowerZero()
|
|
76
|
-
},
|
|
77
|
-
|
|
78
|
-
getAvailableBalance: ({ asset, accountState }) => {
|
|
79
|
-
assert(asset, 'asset must be provided')
|
|
80
|
-
assert(accountState, 'accountState must be provided')
|
|
81
|
-
assert(brc20Utils.isToken({ asset }), `${asset.name} is must be a token`)
|
|
82
|
-
const assetBalances = Object.values(accountState?.brc20Balances?.[asset.name] || {})
|
|
83
|
-
const sum = sumBy(assetBalances, 'brc20OwnerAvailableBalance')
|
|
84
|
-
return asset.currency.defaultUnit(sum || 0)
|
|
85
|
-
},
|
|
86
|
-
|
|
87
|
-
getTransferableBalance: ({ asset, accountState }) => {
|
|
88
|
-
assert(asset, 'asset must be provided')
|
|
89
|
-
assert(accountState, 'accountState must be provided')
|
|
90
|
-
assert(brc20Utils.isToken({ asset }), `${asset.name} is must be a token`)
|
|
91
|
-
const assetBalances = Object.values(accountState?.brc20Balances?.[asset.name] || {})
|
|
92
|
-
const sum = sumBy(assetBalances, 'brc20OwnerTransferBalance')
|
|
93
|
-
return asset.currency
|
|
94
|
-
.defaultUnit(sum || 0)
|
|
95
|
-
.sub(brc20Utils.getInTransitBalance({ asset, accountState }))
|
|
96
|
-
.clampLowerZero()
|
|
97
|
-
},
|
|
98
|
-
|
|
99
|
-
getAllSublists(transferableList) {
|
|
100
|
-
const result = []
|
|
101
|
-
|
|
102
|
-
const generateSublists = (sublist, index) => {
|
|
103
|
-
if (index === transferableList.length) {
|
|
104
|
-
result.push(sublist)
|
|
105
|
-
return
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// Include the current element in the sublist
|
|
109
|
-
generateSublists([...sublist, transferableList[index]], index + 1)
|
|
110
|
-
|
|
111
|
-
// Exclude the current element from the sublist
|
|
112
|
-
generateSublists(sublist, index + 1)
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
generateSublists([], 0)
|
|
116
|
-
return result
|
|
117
|
-
},
|
|
118
|
-
|
|
119
|
-
findTransferableItems({ transferableList, amount, currency }) {
|
|
120
|
-
const sublists = brc20Utils
|
|
121
|
-
.getAllSublists(transferableList)
|
|
122
|
-
.map((transferableItems) => {
|
|
123
|
-
const transferableItemsAmount = transferableItems.reduce(
|
|
124
|
-
(acc, item) => acc.add(item.amount),
|
|
125
|
-
currency.ZERO
|
|
126
|
-
)
|
|
127
|
-
return { transferableItemsAmount, transferableItems }
|
|
128
|
-
})
|
|
129
|
-
.filter((item) => item.transferableItemsAmount.lte(amount))
|
|
130
|
-
const found = sublists.find((item) => item.transferableItemsAmount.equals(amount))
|
|
131
|
-
if (found) {
|
|
132
|
-
return found
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
return sublists.sort((a, b) => b.transferableItems.length - a.transferableItems.length)[0]
|
|
136
|
-
},
|
|
137
|
-
}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import assert from 'minimalistic-assert'
|
|
2
|
-
|
|
3
|
-
import { brc20Utils } from './brc-20-utils.js'
|
|
4
|
-
|
|
5
|
-
export const createBrc20Command = ({ asset, amount, accountState }) => {
|
|
6
|
-
assert(asset.name !== asset.baseAsset.name, `${asset.name} should be a token!`)
|
|
7
|
-
|
|
8
|
-
const brc20BalanceMultipleAddresses = accountState?.brc20Balances?.[asset.name] || {}
|
|
9
|
-
const balance = brc20Utils.getTokenBalance({ asset, accountState })
|
|
10
|
-
const transferableList = brc20Utils.getTransferableList({ asset, accountState })
|
|
11
|
-
assert(
|
|
12
|
-
balance.gte(amount),
|
|
13
|
-
`Not enough ${asset.name} balance. Wanted ${amount.toDefaultString({
|
|
14
|
-
unit: true,
|
|
15
|
-
})}, but got ${balance.toDefaultString({ unit: true })}`
|
|
16
|
-
)
|
|
17
|
-
|
|
18
|
-
const transferableItemsResult = brc20Utils.findTransferableItems({
|
|
19
|
-
transferableList,
|
|
20
|
-
amount,
|
|
21
|
-
currency: asset.currency,
|
|
22
|
-
})
|
|
23
|
-
const brc20 = {
|
|
24
|
-
inscriptionIds: transferableItemsResult.transferableItems.map((i) => i.inscriptionId),
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
let requireInscriptionAmount = amount.sub(transferableItemsResult.transferableItemsAmount)
|
|
28
|
-
|
|
29
|
-
brc20.newInscriptions = Object.entries(brc20BalanceMultipleAddresses)
|
|
30
|
-
.map(([address, assetBalance]) => {
|
|
31
|
-
const availableBalance = asset.currency.defaultUnit(assetBalance.brc20OwnerAvailableBalance)
|
|
32
|
-
const amount = requireInscriptionAmount.gt(availableBalance)
|
|
33
|
-
? availableBalance
|
|
34
|
-
: requireInscriptionAmount
|
|
35
|
-
requireInscriptionAmount = requireInscriptionAmount.sub(amount)
|
|
36
|
-
return {
|
|
37
|
-
amount,
|
|
38
|
-
address,
|
|
39
|
-
}
|
|
40
|
-
})
|
|
41
|
-
.filter((i) => i.amount.gt(asset.currency.ZERO))
|
|
42
|
-
|
|
43
|
-
assert(
|
|
44
|
-
balance.gte(amount),
|
|
45
|
-
`Not enough ${asset.name} balance. Wanted ${amount.toDefaultString({
|
|
46
|
-
unit: true,
|
|
47
|
-
})}, but got ${balance.toDefaultString({ unit: true })}`
|
|
48
|
-
)
|
|
49
|
-
|
|
50
|
-
return brc20
|
|
51
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
export function validateAssetId(assetId) {
|
|
2
|
-
if (typeof assetId !== 'string') return false
|
|
3
|
-
const rgx = /^[\da-f]{64}i\d+$/
|
|
4
|
-
return rgx.test(assetId)
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export const createTokenFactory = ({ address, bip44, keys, getBalances }, networkAssets) => {
|
|
8
|
-
const createToken = ({ features: providedFeatures, ...tokenDef }) => {
|
|
9
|
-
const features = { ...providedFeatures }
|
|
10
|
-
return {
|
|
11
|
-
...tokenDef,
|
|
12
|
-
api: {
|
|
13
|
-
getBalances,
|
|
14
|
-
features,
|
|
15
|
-
hasFeature: (feature) => !!features[feature],
|
|
16
|
-
},
|
|
17
|
-
address,
|
|
18
|
-
bip44,
|
|
19
|
-
keys,
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return {
|
|
24
|
-
validateAssetId,
|
|
25
|
-
createToken,
|
|
26
|
-
getTokens: () =>
|
|
27
|
-
Object.values(networkAssets)
|
|
28
|
-
.filter((asset) => asset.name !== asset.baseAsset.name)
|
|
29
|
-
.map(createToken),
|
|
30
|
-
}
|
|
31
|
-
}
|
|
@@ -1,377 +0,0 @@
|
|
|
1
|
-
import { getOrdinalsUtxos, toAsyncSigner } from '@exodus/bitcoin-api'
|
|
2
|
-
import { getTxSequence } from '@exodus/bitcoin-lib'
|
|
3
|
-
import * as bitcoinjsLib from '@exodus/bitcoinjs'
|
|
4
|
-
import { randomBytes } from '@exodus/crypto/randomBytes'
|
|
5
|
-
import { privateKeyToPublicKey, publicKeyToX } from '@exodus/crypto/secp256k1'
|
|
6
|
-
import { TextEncoder } from '@exodus/text-encoding-utf8'
|
|
7
|
-
import assert from 'minimalistic-assert'
|
|
8
|
-
|
|
9
|
-
import { DEFAULT_BRC20_POSTAGE, DEFAULT_ORDINAL_POSTAGE } from './ordinals-constants.js'
|
|
10
|
-
|
|
11
|
-
const encoder = new TextEncoder()
|
|
12
|
-
export function createInscriptionScript({ xOnlyPublicKey, inscription }) {
|
|
13
|
-
assert(xOnlyPublicKey instanceof Buffer, `xOnlyPublicKey must be a Buffer`)
|
|
14
|
-
assert(inscription, `inscription is required`)
|
|
15
|
-
assert(inscription.content instanceof Buffer, `inscription.content must be a Buffer`)
|
|
16
|
-
assert(inscription.contentType instanceof Buffer, `inscription.content must be a Buffer`)
|
|
17
|
-
const protocolId = Buffer.from(encoder.encode('ord'))
|
|
18
|
-
return [
|
|
19
|
-
xOnlyPublicKey,
|
|
20
|
-
bitcoinjsLib.opcodes.OP_CHECKSIG,
|
|
21
|
-
bitcoinjsLib.opcodes.OP_0,
|
|
22
|
-
bitcoinjsLib.opcodes.OP_IF,
|
|
23
|
-
protocolId,
|
|
24
|
-
1,
|
|
25
|
-
1, // double 1 to avoid hitting asMinimalOP
|
|
26
|
-
inscription.contentType,
|
|
27
|
-
bitcoinjsLib.opcodes.OP_0,
|
|
28
|
-
inscription.content,
|
|
29
|
-
bitcoinjsLib.opcodes.OP_ENDIF,
|
|
30
|
-
]
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export const inscriptionsServiceFactory = ({ network, ordinalChainIndex }) => {
|
|
34
|
-
assert(network, 'network is required')
|
|
35
|
-
assert(typeof ordinalChainIndex === 'number', 'ordinalChainIndex must be a number')
|
|
36
|
-
|
|
37
|
-
function createInscription({ contentType, content, postage }) {
|
|
38
|
-
assert(Number.isSafeInteger(postage), 'postage must be a Safe integer')
|
|
39
|
-
assert(contentType, 'contentType is required integer')
|
|
40
|
-
assert(content, 'content is required integer')
|
|
41
|
-
|
|
42
|
-
return {
|
|
43
|
-
contentType: Buffer.from(encoder.encode(contentType)),
|
|
44
|
-
content: Buffer.from(content),
|
|
45
|
-
postage,
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function createTextInscription({ text, postage = DEFAULT_ORDINAL_POSTAGE }) {
|
|
50
|
-
const contentType = 'text/plain;charset=utf-8'
|
|
51
|
-
const content = encoder.encode(text)
|
|
52
|
-
return createInscription({ contentType, content, postage })
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function createBrc20Inscription({
|
|
56
|
-
op,
|
|
57
|
-
tick,
|
|
58
|
-
amt,
|
|
59
|
-
max,
|
|
60
|
-
lim,
|
|
61
|
-
dec,
|
|
62
|
-
postage = DEFAULT_BRC20_POSTAGE,
|
|
63
|
-
}) {
|
|
64
|
-
let body
|
|
65
|
-
|
|
66
|
-
assert(['deploy', 'mint', 'transfer'].includes(op), `invalid brc20 op ${op}`)
|
|
67
|
-
assert(typeof tick === 'string', 'tick is required')
|
|
68
|
-
|
|
69
|
-
if (['mint', 'transfer'].includes(op)) {
|
|
70
|
-
assert(typeof amt === 'string', 'amt is required')
|
|
71
|
-
body = { p: 'brc-20', op, tick, amt }
|
|
72
|
-
} else {
|
|
73
|
-
assert(typeof max === 'string', 'max is required')
|
|
74
|
-
assert(typeof lim === 'string', 'lim is required')
|
|
75
|
-
body = { p: 'brc-20', op, tick, max, lim, dec }
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const contentType = 'text/plain;charset=utf-8'
|
|
79
|
-
const content = encoder.encode(JSON.stringify(body))
|
|
80
|
-
return createInscription({ contentType, content, postage })
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function createCommitTxData({ publicKey, inscription }) {
|
|
84
|
-
assert(publicKey, 'encodePublic is required')
|
|
85
|
-
assert(inscription, 'inscription is required')
|
|
86
|
-
const xOnlyPublicKey = publicKeyToX({ publicKey, format: 'buffer' })
|
|
87
|
-
const script = createInscriptionScript({ xOnlyPublicKey, inscription })
|
|
88
|
-
|
|
89
|
-
const outputScript = bitcoinjsLib.script.compile(script)
|
|
90
|
-
|
|
91
|
-
const scriptTree = {
|
|
92
|
-
output: outputScript,
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
const scriptTaproot = bitcoinjsLib.payments.p2tr({
|
|
96
|
-
internalPubkey: xOnlyPublicKey,
|
|
97
|
-
scriptTree,
|
|
98
|
-
redeem: scriptTree,
|
|
99
|
-
network,
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
const tapleaf = scriptTaproot.hash.toString('hex')
|
|
103
|
-
|
|
104
|
-
const revealAddress = scriptTaproot.address
|
|
105
|
-
const tpubkey = scriptTaproot.pubkey.toString('hex')
|
|
106
|
-
const cblock = scriptTaproot.witness?.[scriptTaproot.witness.length - 1].toString('hex')
|
|
107
|
-
|
|
108
|
-
return {
|
|
109
|
-
script,
|
|
110
|
-
tapleaf,
|
|
111
|
-
tpubkey,
|
|
112
|
-
cblock,
|
|
113
|
-
revealAddress,
|
|
114
|
-
scriptTaproot,
|
|
115
|
-
outputScript,
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function createPsbt({ scriptTaproot, outputScript, cblock, commitTxResult, amount, toAddress }) {
|
|
120
|
-
const tapLeafScript = {
|
|
121
|
-
leafVersion: scriptTaproot.redeemVersion, // 192 0xc0
|
|
122
|
-
script: outputScript,
|
|
123
|
-
controlBlock: Buffer.from(cblock, 'hex'),
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
const psbt = new bitcoinjsLib.Psbt({ network })
|
|
127
|
-
psbt.addInput({
|
|
128
|
-
hash: commitTxResult.txId,
|
|
129
|
-
index: commitTxResult.sendUtxoIndex,
|
|
130
|
-
witnessUtxo: { value: commitTxResult.sendAmount, script: scriptTaproot.output },
|
|
131
|
-
tapLeafScript: [tapLeafScript],
|
|
132
|
-
sequence: getTxSequence(false),
|
|
133
|
-
})
|
|
134
|
-
|
|
135
|
-
psbt.addOutput({
|
|
136
|
-
value: amount, // generally 1000 for nfts, 549 for brc20
|
|
137
|
-
address: toAddress.toString(),
|
|
138
|
-
})
|
|
139
|
-
return { tapLeafScript, psbt }
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
function finalizePsbt({ psbt, signature, commitTxData, tapLeafScript }) {
|
|
143
|
-
const customFinalizer = () => {
|
|
144
|
-
const witness = [
|
|
145
|
-
Buffer.from(signature, 'hex'),
|
|
146
|
-
commitTxData.outputScript,
|
|
147
|
-
tapLeafScript.controlBlock,
|
|
148
|
-
]
|
|
149
|
-
|
|
150
|
-
return {
|
|
151
|
-
finalScriptWitness: bitcoinjsLib.witnessStackToScriptWitness(witness),
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
psbt.finalizeInput(0, customFinalizer)
|
|
156
|
-
return psbt.extractTransaction()
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
const estimationStaticData = {
|
|
160
|
-
toAddress: 'bc1p4vgmzfzu642zxj04525xr8szd446ydct3y44dqc7wg88wd8e4v9qzaz7k9',
|
|
161
|
-
publicKey: Buffer.from(
|
|
162
|
-
'041b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f70beaf8f588b541507fed6a642c5ab42dfdf8120a7f639de5122d47a69a8e8d1',
|
|
163
|
-
'hex'
|
|
164
|
-
),
|
|
165
|
-
txId: '2222222222222222222222222222222222222222222222222222222222222222',
|
|
166
|
-
signature: Buffer.alloc(64, 3),
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const estimateTxSize = ({ inscription }) => {
|
|
170
|
-
const { publicKey, txId, toAddress, signature } = estimationStaticData
|
|
171
|
-
|
|
172
|
-
const commitTxData = createCommitTxData({ publicKey, inscription })
|
|
173
|
-
|
|
174
|
-
const amount = inscription.postage
|
|
175
|
-
|
|
176
|
-
const commitTxResult = {
|
|
177
|
-
txId,
|
|
178
|
-
sendUtxoIndex: 0,
|
|
179
|
-
sendAmount: 100_000,
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
const { tapLeafScript, psbt } = createPsbt({
|
|
183
|
-
commitTxResult,
|
|
184
|
-
amount,
|
|
185
|
-
toAddress,
|
|
186
|
-
...commitTxData,
|
|
187
|
-
})
|
|
188
|
-
|
|
189
|
-
const tx = finalizePsbt({ signature, commitTxData, tapLeafScript, psbt })
|
|
190
|
-
|
|
191
|
-
return tx.virtualSize()
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
async function createRevealTx({ commitTxData, commitTxResult, toAddress, privateKey, amount }) {
|
|
195
|
-
assert(commitTxData, `commitTxData is required`)
|
|
196
|
-
assert(commitTxResult, `commitTxResult is required`)
|
|
197
|
-
assert(toAddress, `toAddress is required`)
|
|
198
|
-
assert(privateKey instanceof Buffer, `privateKey must be a Buffer`)
|
|
199
|
-
assert(typeof amount === 'number', `amount must be a number`)
|
|
200
|
-
|
|
201
|
-
const { tapLeafScript, psbt } = createPsbt({
|
|
202
|
-
commitTxResult,
|
|
203
|
-
amount,
|
|
204
|
-
toAddress,
|
|
205
|
-
...commitTxData,
|
|
206
|
-
})
|
|
207
|
-
|
|
208
|
-
const publicKey = privateKeyToPublicKey({ privateKey, format: 'buffer' })
|
|
209
|
-
await psbt.signInputAsync(0, toAsyncSigner({ privateKey, publicKey }))
|
|
210
|
-
|
|
211
|
-
const signature = psbt.data.inputs[0].tapScriptSig[0].signature.toString('hex')
|
|
212
|
-
|
|
213
|
-
const tx = finalizePsbt({ amount, signature, commitTxData, tapLeafScript, psbt })
|
|
214
|
-
|
|
215
|
-
const virtualSize = tx.virtualSize()
|
|
216
|
-
const rawTx = tx.toBuffer().toString('hex')
|
|
217
|
-
const txId = tx.getId()
|
|
218
|
-
const vout = 0
|
|
219
|
-
const script = tx.outs[vout].script.toString('hex')
|
|
220
|
-
const inscriptionId = `${txId}i0`
|
|
221
|
-
|
|
222
|
-
return {
|
|
223
|
-
txId,
|
|
224
|
-
rawTx,
|
|
225
|
-
inscriptionId,
|
|
226
|
-
signature,
|
|
227
|
-
virtualSize,
|
|
228
|
-
vout,
|
|
229
|
-
script,
|
|
230
|
-
value: amount,
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
const resolveMinersFee = ({ inscription, customFee, feeData, asset }) => {
|
|
235
|
-
assert(Number.isSafeInteger(inscription.postage), 'inscription.postage must be a safe integer')
|
|
236
|
-
const txSize = estimateTxSize({ inscription })
|
|
237
|
-
const feePerKB = customFee || feeData.feePerKB
|
|
238
|
-
const minersFeeBaseUnit = Math.ceil((feePerKB.toBaseNumber() * txSize) / 1000)
|
|
239
|
-
assert(Number.isSafeInteger(minersFeeBaseUnit), 'minersFeeBaseUnit must be a safe integer')
|
|
240
|
-
return asset.currency.baseUnit(minersFeeBaseUnit)
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
const broadcastInscription = async ({
|
|
244
|
-
asset,
|
|
245
|
-
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
|
|
246
|
-
receiverWalletAccount, // Optional: send the inscription to a different portfolio
|
|
247
|
-
receiverAddress, // Optional: send the inscription to a third party wallet
|
|
248
|
-
assetClientInterface,
|
|
249
|
-
inscription,
|
|
250
|
-
privateKey = randomBytes(32),
|
|
251
|
-
customFee,
|
|
252
|
-
}) => {
|
|
253
|
-
assert(asset, 'asset is required')
|
|
254
|
-
assert(walletAccount, 'walletAccount is required')
|
|
255
|
-
assert(assetClientInterface, 'assetClientInterface is required')
|
|
256
|
-
assert(inscription, 'inscription is required')
|
|
257
|
-
|
|
258
|
-
const assetName = asset.name
|
|
259
|
-
|
|
260
|
-
const resolvedReceiverWalletAccount = receiverWalletAccount || walletAccount
|
|
261
|
-
|
|
262
|
-
const accountState = await assetClientInterface.getAccountState({
|
|
263
|
-
assetName,
|
|
264
|
-
walletAccount: resolvedReceiverWalletAccount,
|
|
265
|
-
})
|
|
266
|
-
|
|
267
|
-
const toAddress =
|
|
268
|
-
receiverAddress ||
|
|
269
|
-
(await assetClientInterface.getAddress({
|
|
270
|
-
assetName,
|
|
271
|
-
walletAccount: resolvedReceiverWalletAccount,
|
|
272
|
-
purpose: 86,
|
|
273
|
-
chainIndex: ordinalChainIndex,
|
|
274
|
-
addressIndex: 0,
|
|
275
|
-
}))
|
|
276
|
-
|
|
277
|
-
const postage = inscription.postage
|
|
278
|
-
|
|
279
|
-
const publicKey = privateKeyToPublicKey({ privateKey, compressed: false, format: 'buffer' })
|
|
280
|
-
const commitTxData = createCommitTxData({ publicKey, inscription })
|
|
281
|
-
|
|
282
|
-
const feeData = await assetClientInterface.getFeeConfig({ assetName })
|
|
283
|
-
|
|
284
|
-
const minersFee = resolveMinersFee({ inscription, customFee, feeData, asset })
|
|
285
|
-
const commitAmount = minersFee.add(asset.currency.baseUnit(postage))
|
|
286
|
-
|
|
287
|
-
const commitTxResult = await asset.api.sendTx({
|
|
288
|
-
asset,
|
|
289
|
-
walletAccount,
|
|
290
|
-
address: commitTxData.revealAddress,
|
|
291
|
-
amount: commitAmount,
|
|
292
|
-
options: { isRbfAllowed: false, customFee },
|
|
293
|
-
})
|
|
294
|
-
|
|
295
|
-
const revealTx = await createRevealTx({
|
|
296
|
-
commitTxData,
|
|
297
|
-
amount: postage,
|
|
298
|
-
commitTxResult,
|
|
299
|
-
toAddress,
|
|
300
|
-
privateKey,
|
|
301
|
-
})
|
|
302
|
-
|
|
303
|
-
if (!receiverAddress && resolvedReceiverWalletAccount) {
|
|
304
|
-
// only add the utxo
|
|
305
|
-
const storedOrdinalsUtxos = getOrdinalsUtxos({ accountState, asset })
|
|
306
|
-
|
|
307
|
-
const ordinalsUtxos = storedOrdinalsUtxos.addUtxo({
|
|
308
|
-
txId: revealTx.txId,
|
|
309
|
-
address: toAddress,
|
|
310
|
-
vout: revealTx.vout,
|
|
311
|
-
script: revealTx.script,
|
|
312
|
-
value: asset.currency.baseUnit(revealTx.value),
|
|
313
|
-
confirmations: 0,
|
|
314
|
-
inscriptions: [{ inscriptionId: revealTx.inscriptionId, offset: 0 }],
|
|
315
|
-
inscriptionIndexed: false,
|
|
316
|
-
rbfEnabled: false,
|
|
317
|
-
})
|
|
318
|
-
|
|
319
|
-
await assetClientInterface.updateAccountState({
|
|
320
|
-
assetName,
|
|
321
|
-
walletAccount: resolvedReceiverWalletAccount,
|
|
322
|
-
newData: { ordinalsUtxos },
|
|
323
|
-
})
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
await asset.api.broadcastTx(revealTx.rawTx)
|
|
327
|
-
return { ...revealTx, ...commitTxData }
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
const getInscriptionFees = ({
|
|
331
|
-
accountState,
|
|
332
|
-
asset,
|
|
333
|
-
customFee,
|
|
334
|
-
feeData,
|
|
335
|
-
getFee,
|
|
336
|
-
inscription,
|
|
337
|
-
txSet,
|
|
338
|
-
}) => {
|
|
339
|
-
assert(accountState, 'accountState is required')
|
|
340
|
-
assert(asset, 'asset is required')
|
|
341
|
-
assert(feeData, 'feeData is required')
|
|
342
|
-
assert(getFee, 'getFee is required')
|
|
343
|
-
assert(inscription, 'inscription is required')
|
|
344
|
-
assert(txSet, 'txSet is required')
|
|
345
|
-
|
|
346
|
-
const minersFee = resolveMinersFee({ inscription, customFee, feeData, asset })
|
|
347
|
-
const postageFee = asset.currency.baseUnit(inscription.postage)
|
|
348
|
-
// Total fees: postage + fee of sending from wallet => inscribe taproot address + fee of sending inscribe taproot address => wallet
|
|
349
|
-
const { fee: foundInscriptionFee } = getFee({
|
|
350
|
-
asset,
|
|
351
|
-
accountState,
|
|
352
|
-
txSet,
|
|
353
|
-
feeData,
|
|
354
|
-
amount: minersFee.add(postageFee),
|
|
355
|
-
customFee,
|
|
356
|
-
isSendAll: false,
|
|
357
|
-
receiveAddress: 'P2TR',
|
|
358
|
-
})
|
|
359
|
-
|
|
360
|
-
return {
|
|
361
|
-
totalFee: foundInscriptionFee.add(minersFee).add(postageFee),
|
|
362
|
-
foundInscriptionFee,
|
|
363
|
-
minersFee,
|
|
364
|
-
postageFee,
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
return {
|
|
369
|
-
createTextInscription,
|
|
370
|
-
createBrc20Inscription,
|
|
371
|
-
createCommitTxData,
|
|
372
|
-
createRevealTx,
|
|
373
|
-
broadcastInscription,
|
|
374
|
-
estimateTxSize,
|
|
375
|
-
getInscriptionFees,
|
|
376
|
-
}
|
|
377
|
-
}
|