@exodus/bitcoin-plugin 1.5.0-alpha.0 → 1.5.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 +4 -3
- package/src/create-asset.js +38 -15
- package/src/ordinals/brc-20-balances-and-fee-adapter.js +85 -0
- package/src/ordinals/brc-20-client.js +14 -1
- package/src/ordinals/brc-20-utils.js +39 -0
- package/src/ordinals/create-brc-20-command.js +50 -0
- package/src/ordinals/create-token-factory.js +2 -2
- package/src/ordinals/inscriptions-service.js +292 -0
- package/src/ordinals/nfts.js +1 -1
- package/src/ordinals/ordinals-account-state-utils.js +9 -2
- package/src/ordinals/ordinals-constants.js +7 -0
- package/src/ordinals/reload-brc20-token-balances.js +22 -14
- package/src/ordinals/tick-inscription-id-indexer.js +48 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@exodus/bitcoin-plugin",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.1",
|
|
4
4
|
"description": "Exodus bitcoin-plugin",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"files": [
|
|
@@ -19,12 +19,13 @@
|
|
|
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.1",
|
|
23
23
|
"@exodus/bitcoin-lib": "^2.2.8",
|
|
24
|
+
"bitcoinjs-lib": "^6.1.5",
|
|
24
25
|
"minimalistic-assert": "^1.0.1"
|
|
25
26
|
},
|
|
26
27
|
"devDependencies": {
|
|
27
28
|
"@exodus/keychain": "^3.0.0"
|
|
28
29
|
},
|
|
29
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "562c288cde1c1d7c7ab2c7bc6b1804ccca9b37f2"
|
|
30
31
|
}
|
package/src/create-asset.js
CHANGED
|
@@ -8,7 +8,6 @@ import {
|
|
|
8
8
|
createBitcoinMonitor,
|
|
9
9
|
createBtcLikeAddress,
|
|
10
10
|
createBtcLikeKeys,
|
|
11
|
-
eccFactory,
|
|
12
11
|
getBalancesFactory,
|
|
13
12
|
getAddressesFromPrivateKeyFactory,
|
|
14
13
|
getBtcVersions,
|
|
@@ -30,6 +29,8 @@ import { brc20ApiClientFactory } from './ordinals/brc-20-client'
|
|
|
30
29
|
import { nftsApiFactory } from './ordinals/nfts'
|
|
31
30
|
import { brc20BalancesAndFeeAdapterFactory } from './ordinals/brc-20-balances-and-fee-adapter'
|
|
32
31
|
import { parseCurrency } from '@exodus/bitcoin-api/src/fee/fee-utils'
|
|
32
|
+
import { inscriptionsServiceFactory } from './ordinals/inscriptions-service'
|
|
33
|
+
import { tickInscriptionIdIndexerFactory } from './ordinals/tick-inscription-id-indexer'
|
|
33
34
|
|
|
34
35
|
export const createBitcoinAssetFactory = ({ asset, apiUrl, coinInfoNetwork }) => ({
|
|
35
36
|
allowUnconfirmedRbfEnabledUtxos = false,
|
|
@@ -51,18 +52,15 @@ export const createBitcoinAssetFactory = ({ asset, apiUrl, coinInfoNetwork }) =>
|
|
|
51
52
|
const useBip84 = true
|
|
52
53
|
const useBip86 = true
|
|
53
54
|
|
|
54
|
-
const ecc = eccFactory()
|
|
55
|
-
|
|
56
55
|
const versions = getBtcVersions(coinInfo)
|
|
57
56
|
const address = createBtcLikeAddress({
|
|
58
57
|
versions,
|
|
59
58
|
coinInfo,
|
|
60
59
|
useBip86,
|
|
61
60
|
bitcoinjsLib,
|
|
62
|
-
ecc,
|
|
63
61
|
})
|
|
64
62
|
|
|
65
|
-
const keys = createBtcLikeKeys({ coinInfo, versions, useBip86
|
|
63
|
+
const keys = createBtcLikeKeys({ coinInfo, versions, useBip86 })
|
|
66
64
|
const bip44 = bip44Constants['BTC']
|
|
67
65
|
|
|
68
66
|
const allowedPurposes = [44, 49, 84, 86]
|
|
@@ -80,7 +78,6 @@ export const createBitcoinAssetFactory = ({ asset, apiUrl, coinInfoNetwork }) =>
|
|
|
80
78
|
}
|
|
81
79
|
|
|
82
80
|
const getFeeEstimator = getFeeEstimatorFactory({
|
|
83
|
-
ecc,
|
|
84
81
|
defaultOutputType: 'P2WSH',
|
|
85
82
|
addressApi: address,
|
|
86
83
|
})
|
|
@@ -97,21 +94,45 @@ export const createBitcoinAssetFactory = ({ asset, apiUrl, coinInfoNetwork }) =>
|
|
|
97
94
|
getSpendableBalance: baseFeeResolver.getSpendableBalance,
|
|
98
95
|
})
|
|
99
96
|
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
97
|
+
const brc20Client = brc20Enabled ? brc20ApiClientFactory() : undefined
|
|
98
|
+
const tickInscriptionIdIndexer = brc20Enabled
|
|
99
|
+
? tickInscriptionIdIndexerFactory({ brc20Client })
|
|
100
|
+
: undefined
|
|
103
101
|
|
|
104
|
-
const
|
|
102
|
+
const baseSendTx = createAndBroadcastTXFactory({
|
|
105
103
|
getFeeEstimator,
|
|
106
104
|
allowUnconfirmedRbfEnabledUtxos,
|
|
107
105
|
})
|
|
106
|
+
const inscriptionsService = brc20Enabled
|
|
107
|
+
? inscriptionsServiceFactory({
|
|
108
|
+
asset,
|
|
109
|
+
network: coinInfo.toBitcoinJS(),
|
|
110
|
+
ordinalChainIndex,
|
|
111
|
+
})
|
|
112
|
+
: undefined
|
|
113
|
+
|
|
114
|
+
const {
|
|
115
|
+
getFee,
|
|
116
|
+
getBalances,
|
|
117
|
+
getAvailableBalance,
|
|
118
|
+
canBumpTx,
|
|
119
|
+
getSpendableBalance,
|
|
120
|
+
sendTx,
|
|
121
|
+
} = brc20Enabled
|
|
122
|
+
? brc20BalancesAndFeeAdapterFactory({
|
|
123
|
+
inscriptionsService,
|
|
124
|
+
...baseFeeResolver,
|
|
125
|
+
getBalances: baseGetBalances,
|
|
126
|
+
sendTx: baseSendTx,
|
|
127
|
+
tickInscriptionIdIndexer,
|
|
128
|
+
})
|
|
129
|
+
: { ...baseFeeResolver, getBalances: baseGetBalances, sendTx: baseSendTx }
|
|
108
130
|
|
|
109
131
|
const signTx = signTxFactory({
|
|
110
132
|
assetName: asset.name,
|
|
111
133
|
resolvePurpose: address.resolvePurpose,
|
|
112
134
|
keys,
|
|
113
135
|
coinInfo,
|
|
114
|
-
ecc,
|
|
115
136
|
})
|
|
116
137
|
|
|
117
138
|
const moveFunds = moveFundsFactory({
|
|
@@ -147,18 +168,19 @@ export const createBitcoinAssetFactory = ({ asset, apiUrl, coinInfoNetwork }) =>
|
|
|
147
168
|
if (ordinalsEnabled) {
|
|
148
169
|
nfts.initialize({ assetClientInterface })
|
|
149
170
|
}
|
|
171
|
+
|
|
150
172
|
if (brc20Enabled) {
|
|
151
|
-
const brc20Client = brc20ApiClientFactory()
|
|
152
173
|
monitor.addHook('after-tick', async ({ walletAccount }) => {
|
|
153
|
-
const {
|
|
174
|
+
const { unknownDeployInscriptionIds } = await reloadBrc20TokenBalances({
|
|
154
175
|
walletAccount,
|
|
155
176
|
asset,
|
|
156
177
|
ordinalChainIndex,
|
|
157
178
|
brc20Client,
|
|
158
179
|
assetClientInterface,
|
|
180
|
+
tickInscriptionIdIndexer,
|
|
159
181
|
})
|
|
160
|
-
if (
|
|
161
|
-
monitor.emit('unknown-tokens',
|
|
182
|
+
if (unknownDeployInscriptionIds.length) {
|
|
183
|
+
monitor.emit('unknown-tokens', unknownDeployInscriptionIds)
|
|
162
184
|
}
|
|
163
185
|
})
|
|
164
186
|
}
|
|
@@ -242,6 +264,7 @@ export const createBitcoinAssetFactory = ({ asset, apiUrl, coinInfoNetwork }) =>
|
|
|
242
264
|
canBumpTx,
|
|
243
265
|
getAvailableBalance,
|
|
244
266
|
getSpendableBalance,
|
|
267
|
+
inscriptions: inscriptionsService,
|
|
245
268
|
}
|
|
246
269
|
return overrideCallback({ asset: fullAsset })
|
|
247
270
|
}
|
|
@@ -1,22 +1,101 @@
|
|
|
1
1
|
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
|
+
import { createBrc20Command } from './create-brc-20-command'
|
|
4
5
|
|
|
5
6
|
export const brc20BalancesAndFeeAdapterFactory = ({
|
|
7
|
+
inscriptionsService,
|
|
6
8
|
getBalances,
|
|
7
9
|
getFee,
|
|
8
10
|
getAvailableBalance,
|
|
9
11
|
canBumpTx,
|
|
10
12
|
getSpendableBalance,
|
|
13
|
+
sendTx,
|
|
14
|
+
tickInscriptionIdIndexer,
|
|
11
15
|
}) => {
|
|
16
|
+
assert(inscriptionsService, 'inscriptionsService is required')
|
|
12
17
|
assert(getBalances, 'getBalances is required')
|
|
13
18
|
assert(getFee, 'getFee is required')
|
|
14
19
|
assert(getAvailableBalance, 'getAvailableBalance is required')
|
|
15
20
|
assert(canBumpTx, 'canBumpTx is required')
|
|
16
21
|
assert(getSpendableBalance, 'getSpendableBalance is required')
|
|
22
|
+
assert(sendTx, 'sendTx is required')
|
|
23
|
+
assert(tickInscriptionIdIndexer, 'unknownDeployInscriptionIds is required')
|
|
17
24
|
|
|
18
25
|
const { isToken, getTokenBalance } = brc20Utils
|
|
19
26
|
|
|
27
|
+
const isInscription = ({ options }) => {
|
|
28
|
+
return !options.feeOpts.inscriptionIds?.length
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const createAndBroadcastInscription = async (
|
|
32
|
+
{ asset: token, walletAccount, address, amount, options },
|
|
33
|
+
{ assetClientInterface }
|
|
34
|
+
) => {
|
|
35
|
+
const { broadcastInscription, createBrc20Inscription } = inscriptionsService
|
|
36
|
+
|
|
37
|
+
const asset = token.baseAsset
|
|
38
|
+
const accountState = await assetClientInterface.getAccountState({
|
|
39
|
+
walletAccount,
|
|
40
|
+
assetName: asset.name,
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
const tick = await tickInscriptionIdIndexer.getBrc20TickFromAsset(token)
|
|
44
|
+
|
|
45
|
+
const tokenAmount = options.isSendAll ? getTokenBalance({ asset: token, accountState }) : amount
|
|
46
|
+
|
|
47
|
+
if (tokenAmount.lte(token.currency.ZERO)) {
|
|
48
|
+
throw new Error(
|
|
49
|
+
`Cannot send ${tokenAmount.toDefaultString({ unit: true })} amount for asset :${
|
|
50
|
+
token.name
|
|
51
|
+
}. Tick: ${tick}`
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const brc20Command = createBrc20Command({
|
|
56
|
+
asset: token,
|
|
57
|
+
amount: tokenAmount,
|
|
58
|
+
accountState,
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
const inscriptionIds = brc20Command.inscriptionIds
|
|
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({
|
|
66
|
+
op: 'transfer',
|
|
67
|
+
tick,
|
|
68
|
+
amt: newInscription.amount.toDefaultString({ unit: false }),
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
const inscriptionResult = await broadcastInscription({
|
|
72
|
+
asset,
|
|
73
|
+
assetClientInterface,
|
|
74
|
+
walletAccount,
|
|
75
|
+
receiverWalletAccount: walletAccount,
|
|
76
|
+
inscription,
|
|
77
|
+
customFee,
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
inscriptionIds.push(inscriptionResult.inscriptionId)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return sendTx(
|
|
84
|
+
{
|
|
85
|
+
asset: token,
|
|
86
|
+
walletAccount,
|
|
87
|
+
address,
|
|
88
|
+
amount: tokenAmount,
|
|
89
|
+
options: {
|
|
90
|
+
...options,
|
|
91
|
+
isRbfAllowed: false,
|
|
92
|
+
brc20: { inscriptionIds },
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
{ assetClientInterface }
|
|
96
|
+
)
|
|
97
|
+
}
|
|
98
|
+
|
|
20
99
|
return {
|
|
21
100
|
canBumpTx: (args) => {
|
|
22
101
|
return isToken(args) ? { bumpType: BumpType.NONE } : canBumpTx(args)
|
|
@@ -37,5 +116,11 @@ export const brc20BalancesAndFeeAdapterFactory = ({
|
|
|
37
116
|
getBalances: (args) => {
|
|
38
117
|
return isToken(args) ? { balance: getTokenBalance(args) } : getBalances(args)
|
|
39
118
|
},
|
|
119
|
+
|
|
120
|
+
sendTx: (...args) => {
|
|
121
|
+
return isToken(args[0]) && isInscription(...args)
|
|
122
|
+
? createAndBroadcastInscription(...args)
|
|
123
|
+
: sendTx(...args)
|
|
124
|
+
},
|
|
40
125
|
}
|
|
41
126
|
}
|
|
@@ -49,6 +49,13 @@ export const brc20ApiClientFactory = ({
|
|
|
49
49
|
return request(`/v2/ord/btc/brc20/tick_infos/${encodeURIComponent(tick)}`)
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
const getTickerInfoByDeployInscriptionId = async ({ deployInscriptionId }) => {
|
|
53
|
+
assert(deployInscriptionId, 'deployInscriptionId is required')
|
|
54
|
+
return request(
|
|
55
|
+
`/v2/ord/btc/brc20/deployed_inscription/${encodeURIComponent(deployInscriptionId)}`
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
|
|
52
59
|
const removeNullUndefined = (obj) =>
|
|
53
60
|
Object.fromEntries(Object.entries(obj).filter(([_, v]) => v !== undefined))
|
|
54
61
|
|
|
@@ -91,5 +98,11 @@ export const brc20ApiClientFactory = ({
|
|
|
91
98
|
})
|
|
92
99
|
}
|
|
93
100
|
|
|
94
|
-
return {
|
|
101
|
+
return {
|
|
102
|
+
setUrl,
|
|
103
|
+
getOwnerTickBalances,
|
|
104
|
+
getTickerInfo,
|
|
105
|
+
getTickerInfoByDeployInscriptionId,
|
|
106
|
+
getTokenTransactionHistory,
|
|
107
|
+
}
|
|
95
108
|
}
|
|
@@ -76,4 +76,43 @@ export const brc20Utils = {
|
|
|
76
76
|
.sub(brc20Utils.getInTransitBalance({ asset, accountState }))
|
|
77
77
|
.clampLowerZero()
|
|
78
78
|
},
|
|
79
|
+
|
|
80
|
+
getAllSublists(transferableList) {
|
|
81
|
+
const result = []
|
|
82
|
+
|
|
83
|
+
const generateSublists = (sublist, index) => {
|
|
84
|
+
if (index === transferableList.length) {
|
|
85
|
+
result.push(sublist)
|
|
86
|
+
return
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Include the current element in the sublist
|
|
90
|
+
generateSublists([...sublist, transferableList[index]], index + 1)
|
|
91
|
+
|
|
92
|
+
// Exclude the current element from the sublist
|
|
93
|
+
generateSublists(sublist, index + 1)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
generateSublists([], 0)
|
|
97
|
+
return result
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
findTransferableItems({ transferableList, amount, currency }) {
|
|
101
|
+
const sublists = brc20Utils
|
|
102
|
+
.getAllSublists(transferableList)
|
|
103
|
+
.map((transferableItems) => {
|
|
104
|
+
const transferableItemsAmount = transferableItems.reduce(
|
|
105
|
+
(acc, item) => acc.add(item.amount),
|
|
106
|
+
currency.ZERO
|
|
107
|
+
)
|
|
108
|
+
return { transferableItemsAmount, transferableItems }
|
|
109
|
+
})
|
|
110
|
+
.filter((item) => item.transferableItemsAmount.lte(amount))
|
|
111
|
+
const found = sublists.find((item) => item.transferableItemsAmount.equals(amount))
|
|
112
|
+
if (found) {
|
|
113
|
+
return found
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return sublists.sort((a, b) => b.transferableItems.length - a.transferableItems.length)[0]
|
|
117
|
+
},
|
|
79
118
|
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import assert from 'minimalistic-assert'
|
|
2
|
+
import { brc20Utils } from './brc-20-utils'
|
|
3
|
+
|
|
4
|
+
export const createBrc20Command = ({ asset, amount, accountState }) => {
|
|
5
|
+
assert(asset.name !== asset.baseAsset.name, `${asset.name} should be a token!`)
|
|
6
|
+
|
|
7
|
+
const brc20BalanceMultipleAddresses = accountState?.brc20Balances?.[asset.name] || {}
|
|
8
|
+
const balance = brc20Utils.getTokenBalance({ asset, accountState })
|
|
9
|
+
const transferableList = brc20Utils.getTransferableList({ asset, accountState })
|
|
10
|
+
assert(
|
|
11
|
+
balance.gte(amount),
|
|
12
|
+
`Not enough ${asset.name} balance. Wanted ${amount.toDefaultString({
|
|
13
|
+
unit: true,
|
|
14
|
+
})}, but got ${balance.toDefaultString({ unit: true })}`
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
const transferableItemsResult = brc20Utils.findTransferableItems({
|
|
18
|
+
transferableList,
|
|
19
|
+
amount,
|
|
20
|
+
currency: asset.currency,
|
|
21
|
+
})
|
|
22
|
+
const brc20 = {
|
|
23
|
+
inscriptionIds: transferableItemsResult.transferableItems.map((i) => i.inscriptionId),
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
let requireInscriptionAmount = amount.sub(transferableItemsResult.transferableItemsAmount)
|
|
27
|
+
|
|
28
|
+
brc20.newInscriptions = Object.entries(brc20BalanceMultipleAddresses)
|
|
29
|
+
.map(([address, assetBalance]) => {
|
|
30
|
+
const availableBalance = asset.currency.defaultUnit(assetBalance.brc20OwnerAvailableBalance)
|
|
31
|
+
const amount = requireInscriptionAmount.gt(availableBalance)
|
|
32
|
+
? availableBalance
|
|
33
|
+
: requireInscriptionAmount
|
|
34
|
+
requireInscriptionAmount = requireInscriptionAmount.sub(amount)
|
|
35
|
+
return {
|
|
36
|
+
amount,
|
|
37
|
+
address,
|
|
38
|
+
}
|
|
39
|
+
})
|
|
40
|
+
.filter((i) => i.amount.gt(asset.currency.ZERO))
|
|
41
|
+
|
|
42
|
+
assert(
|
|
43
|
+
balance.gte(amount),
|
|
44
|
+
`Not enough ${asset.name} balance. Wanted ${amount.toDefaultString({
|
|
45
|
+
unit: true,
|
|
46
|
+
})}, but got ${balance.toDefaultString({ unit: true })}`
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
return brc20
|
|
50
|
+
}
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import assert from 'minimalistic-assert'
|
|
2
|
+
import * as bitcoinjsLib from 'bitcoinjs-lib'
|
|
3
|
+
|
|
4
|
+
import ECPairFactory from 'ecpair'
|
|
5
|
+
import { randomBytes } from '@exodus/crypto/randomBytes'
|
|
6
|
+
import secp256k1 from 'tiny-secp256k1'
|
|
7
|
+
import { DEFAULT_BRC20_POSTAGE, DEFAULT_ORDINAL_POSTAGE } from './ordinals-constants'
|
|
8
|
+
|
|
9
|
+
import { witnessStackToScriptWitness } from 'bitcoinjs-lib/src/psbt/psbtutils'
|
|
10
|
+
import getTxSequence from '@exodus/bitcoin-lib/lib/utils/get-tx-sequence'
|
|
11
|
+
import { toXOnly, toAsyncSigner, eccFactory, getOrdinalsUtxos } from '@exodus/bitcoin-api'
|
|
12
|
+
|
|
13
|
+
const encoder = new TextEncoder()
|
|
14
|
+
export function createInscriptionScript({ xOnlyPublicKey, inscription }) {
|
|
15
|
+
assert(xOnlyPublicKey instanceof Buffer, `xOnlyPublicKey must be a Buffer`)
|
|
16
|
+
assert(inscription, `inscription is required`)
|
|
17
|
+
assert(inscription.content instanceof Buffer, `inscription.content must be a Buffer`)
|
|
18
|
+
assert(inscription.contentType instanceof Buffer, `inscription.content must be a Buffer`)
|
|
19
|
+
const protocolId = Buffer.from(encoder.encode('ord'))
|
|
20
|
+
return [
|
|
21
|
+
xOnlyPublicKey,
|
|
22
|
+
bitcoinjsLib.opcodes.OP_CHECKSIG,
|
|
23
|
+
bitcoinjsLib.opcodes.OP_0,
|
|
24
|
+
bitcoinjsLib.opcodes.OP_IF,
|
|
25
|
+
protocolId,
|
|
26
|
+
1,
|
|
27
|
+
1, // double 1 to avoid hitting asMinimalOP
|
|
28
|
+
inscription.contentType,
|
|
29
|
+
bitcoinjsLib.opcodes.OP_0,
|
|
30
|
+
inscription.content,
|
|
31
|
+
bitcoinjsLib.opcodes.OP_ENDIF,
|
|
32
|
+
]
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const inscriptionsServiceFactory = ({ network, ordinalChainIndex }) => {
|
|
36
|
+
const ecc = eccFactory()
|
|
37
|
+
assert(network, 'network is required')
|
|
38
|
+
assert(typeof ordinalChainIndex === 'number', 'ordinalChainIndex must be a number')
|
|
39
|
+
|
|
40
|
+
let ECPair
|
|
41
|
+
|
|
42
|
+
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 }
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function createBrc20Inscription({
|
|
49
|
+
op,
|
|
50
|
+
tick,
|
|
51
|
+
amt,
|
|
52
|
+
max,
|
|
53
|
+
lim,
|
|
54
|
+
dec,
|
|
55
|
+
postage = DEFAULT_BRC20_POSTAGE,
|
|
56
|
+
}) {
|
|
57
|
+
let body
|
|
58
|
+
|
|
59
|
+
assert(['deploy', 'mint', 'transfer'].includes(op), `invalid brc20 op ${op}`)
|
|
60
|
+
assert(typeof tick === 'string', 'tick is required')
|
|
61
|
+
|
|
62
|
+
if (['mint', 'transfer'].includes(op)) {
|
|
63
|
+
assert(typeof amt === 'string', 'amt is required')
|
|
64
|
+
body = { p: 'brc-20', op, tick, amt }
|
|
65
|
+
} else {
|
|
66
|
+
assert(typeof max === 'string', 'max is required')
|
|
67
|
+
assert(typeof lim === 'string', 'lim is required')
|
|
68
|
+
body = { p: 'brc-20', op, tick, max, lim, dec }
|
|
69
|
+
}
|
|
70
|
+
|
|
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 }
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function createCommitTxData({ publicKey, inscription }) {
|
|
77
|
+
assert(publicKey, 'encodePublic is required')
|
|
78
|
+
assert(inscription, 'inscription is required')
|
|
79
|
+
const xOnlyPublicKey = toXOnly(publicKey)
|
|
80
|
+
const script = createInscriptionScript({ xOnlyPublicKey, inscription })
|
|
81
|
+
|
|
82
|
+
const outputScript = bitcoinjsLib.script.compile(script)
|
|
83
|
+
|
|
84
|
+
const scriptTree = {
|
|
85
|
+
output: outputScript,
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const scriptTaproot = bitcoinjsLib.payments.p2tr({
|
|
89
|
+
internalPubkey: xOnlyPublicKey,
|
|
90
|
+
scriptTree,
|
|
91
|
+
redeem: scriptTree,
|
|
92
|
+
network,
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
const tapleaf = scriptTaproot.hash.toString('hex')
|
|
96
|
+
|
|
97
|
+
const revealAddress = scriptTaproot.address
|
|
98
|
+
const tpubkey = scriptTaproot.pubkey.toString('hex')
|
|
99
|
+
const cblock = scriptTaproot.witness?.[scriptTaproot.witness.length - 1].toString('hex')
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
script,
|
|
103
|
+
tapleaf,
|
|
104
|
+
tpubkey,
|
|
105
|
+
cblock,
|
|
106
|
+
revealAddress,
|
|
107
|
+
scriptTaproot,
|
|
108
|
+
outputScript,
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
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
|
+
|
|
121
|
+
const tapLeafScript = {
|
|
122
|
+
leafVersion: scriptTaproot.redeemVersion, // 192 0xc0
|
|
123
|
+
script: outputScript,
|
|
124
|
+
controlBlock: Buffer.from(cblock, 'hex'),
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
ECPair = ECPairFactory(ecc)
|
|
128
|
+
const keypair = ECPair.fromPrivateKey(privateKey, { network })
|
|
129
|
+
const psbt = new bitcoinjsLib.Psbt({ network })
|
|
130
|
+
psbt.addInput({
|
|
131
|
+
hash: commitTxResult.txId,
|
|
132
|
+
index: commitTxResult.sendUtxoIndex,
|
|
133
|
+
witnessUtxo: { value: commitTxResult.sendAmount, script: scriptTaproot.output },
|
|
134
|
+
tapLeafScript: [tapLeafScript],
|
|
135
|
+
sequence: getTxSequence(false),
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
psbt.addOutput({
|
|
139
|
+
value: amount, // generally 1000 for nfts, 549 for brc20
|
|
140
|
+
address: toAddress.toString(),
|
|
141
|
+
})
|
|
142
|
+
|
|
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)
|
|
151
|
+
.concat(tapLeafScript.controlBlock)
|
|
152
|
+
|
|
153
|
+
return {
|
|
154
|
+
finalScriptWitness: witnessStackToScriptWitness(witness),
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
psbt.finalizeInput(0, customFinalizer)
|
|
159
|
+
|
|
160
|
+
const tx = psbt.extractTransaction()
|
|
161
|
+
|
|
162
|
+
const rawTx = tx.toBuffer().toString('hex')
|
|
163
|
+
const txId = tx.getId()
|
|
164
|
+
|
|
165
|
+
const virtualSize = tx.virtualSize()
|
|
166
|
+
|
|
167
|
+
const vout = 0
|
|
168
|
+
const script = tx.outs[vout].script.toString('hex')
|
|
169
|
+
const inscriptionId = `${txId}i0`
|
|
170
|
+
|
|
171
|
+
return {
|
|
172
|
+
txId,
|
|
173
|
+
rawTx,
|
|
174
|
+
inscriptionId,
|
|
175
|
+
signature,
|
|
176
|
+
virtualSize,
|
|
177
|
+
vout,
|
|
178
|
+
script,
|
|
179
|
+
value: amount,
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const broadcastInscription = async ({
|
|
184
|
+
asset,
|
|
185
|
+
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
|
|
186
|
+
receiverWalletAccount, // Optional: send the inscription to a different portfolio
|
|
187
|
+
receiverAddress, // Optional: send the inscription to a third party wallet
|
|
188
|
+
assetClientInterface,
|
|
189
|
+
inscription,
|
|
190
|
+
privateKey = randomBytes(32),
|
|
191
|
+
customFee,
|
|
192
|
+
}) => {
|
|
193
|
+
assert(asset, 'asset is required')
|
|
194
|
+
assert(walletAccount, 'walletAccount is required')
|
|
195
|
+
assert(assetClientInterface, 'assetClientInterface is required')
|
|
196
|
+
assert(inscription, 'inscription is required')
|
|
197
|
+
|
|
198
|
+
const assetName = asset.name
|
|
199
|
+
|
|
200
|
+
const resolvedReceiverWalletAccount = receiverWalletAccount || walletAccount
|
|
201
|
+
|
|
202
|
+
const toAddress =
|
|
203
|
+
receiverAddress ||
|
|
204
|
+
(await assetClientInterface.getAddress({
|
|
205
|
+
assetName,
|
|
206
|
+
walletAccount: resolvedReceiverWalletAccount,
|
|
207
|
+
purpose: 86,
|
|
208
|
+
chainIndex: ordinalChainIndex,
|
|
209
|
+
addressIndex: 0,
|
|
210
|
+
}))
|
|
211
|
+
|
|
212
|
+
const postage = inscription.postage
|
|
213
|
+
|
|
214
|
+
const publicKey = secp256k1.pointFromScalar(privateKey, false)
|
|
215
|
+
const commitTxData = createCommitTxData({ publicKey, inscription })
|
|
216
|
+
|
|
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
|
+
const feeData = await assetClientInterface.getFeeConfig({ assetName })
|
|
231
|
+
|
|
232
|
+
const feePerKB = customFee || feeData.feePerKB
|
|
233
|
+
|
|
234
|
+
const minersFee = Math.ceil((feePerKB.toBaseNumber() * txSize) / 1000)
|
|
235
|
+
|
|
236
|
+
const commitAmount = minersFee + postage
|
|
237
|
+
|
|
238
|
+
const commitTxResult = await asset.api.sendTx(
|
|
239
|
+
{
|
|
240
|
+
asset,
|
|
241
|
+
walletAccount,
|
|
242
|
+
address: commitTxData.revealAddress,
|
|
243
|
+
amount: asset.currency.baseUnit(commitAmount),
|
|
244
|
+
options: { isRbfAllowed: false, customFee },
|
|
245
|
+
},
|
|
246
|
+
{ assetClientInterface }
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
const revealTx = await createRevealTx({
|
|
250
|
+
commitTxData,
|
|
251
|
+
amount: postage,
|
|
252
|
+
commitTxResult,
|
|
253
|
+
toAddress,
|
|
254
|
+
privateKey,
|
|
255
|
+
})
|
|
256
|
+
|
|
257
|
+
if (!receiverAddress && resolvedReceiverWalletAccount) {
|
|
258
|
+
const accountState = await assetClientInterface.getAccountState({
|
|
259
|
+
assetName,
|
|
260
|
+
walletAccount: resolvedReceiverWalletAccount,
|
|
261
|
+
})
|
|
262
|
+
// only add the utxo
|
|
263
|
+
const ordinalsUtxos = getOrdinalsUtxos({ accountState, asset }).addUtxo({
|
|
264
|
+
txId: revealTx.txId,
|
|
265
|
+
address: toAddress,
|
|
266
|
+
vout: revealTx.vout,
|
|
267
|
+
script: revealTx.script,
|
|
268
|
+
value: asset.currency.baseUnit(revealTx.value),
|
|
269
|
+
confirmations: 0,
|
|
270
|
+
inscriptionId: revealTx.inscriptionId,
|
|
271
|
+
rbfEnabled: false,
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
await assetClientInterface.updateAccountState({
|
|
275
|
+
assetName,
|
|
276
|
+
walletAccount: resolvedReceiverWalletAccount,
|
|
277
|
+
newData: { ordinalsUtxos },
|
|
278
|
+
})
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
await asset.api.broadcastTx(revealTx.rawTx)
|
|
282
|
+
return { ...revealTx, ...commitTxData }
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
return {
|
|
286
|
+
createTextInscription,
|
|
287
|
+
createBrc20Inscription,
|
|
288
|
+
createCommitTxData,
|
|
289
|
+
createRevealTx,
|
|
290
|
+
broadcastInscription,
|
|
291
|
+
}
|
|
292
|
+
}
|
package/src/ordinals/nfts.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getOrdinalsUtxos, getUtxos, partitionUtxos } from '@exodus/bitcoin-api'
|
|
1
|
+
import { getOrdinalsUtxos, getUtxos, partitionUtxos, getOrdinalAddress } from '@exodus/bitcoin-api'
|
|
2
2
|
import { UtxoCollection } from '@exodus/models'
|
|
3
3
|
import { isEmpty, pickBy } from 'lodash'
|
|
4
4
|
import { getOwnProperty } from '@exodus/basic-utils'
|
|
@@ -33,10 +33,17 @@ export const updateAccountStateUtxos = async ({
|
|
|
33
33
|
}
|
|
34
34
|
)
|
|
35
35
|
|
|
36
|
+
const ordinalAddress = await getOrdinalAddress({
|
|
37
|
+
asset,
|
|
38
|
+
assetClientInterface: aci,
|
|
39
|
+
walletAccount,
|
|
40
|
+
ordinalChainIndex,
|
|
41
|
+
})
|
|
42
|
+
|
|
36
43
|
if (!allUpdatedUtxos.equals(allStoredUtxos) || !isEmpty(moreNewData)) {
|
|
37
44
|
const { utxos, ordinalsUtxos } = partitionUtxos({
|
|
38
45
|
allUtxos: allUpdatedUtxos,
|
|
39
|
-
|
|
46
|
+
ordinalAddress,
|
|
40
47
|
})
|
|
41
48
|
const utxosData = pickBy(
|
|
42
49
|
{
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
// Ordinal postage is the amount of sats left in an inscription for future transferring fee
|
|
2
|
+
// Wallet may use some postage when transferring an ordinal utxos or consume the fee from another regular utxos
|
|
3
|
+
// In general, wallets don't spend the postage.
|
|
4
|
+
export const DEFAULT_BRC20_POSTAGE = 546
|
|
5
|
+
|
|
6
|
+
export const DEFAULT_ORDINAL_POSTAGE = 10000
|
|
7
|
+
|
|
1
8
|
// What's the taproot chain index to use as the default ordinal address (as in purpose 86, m/2/0).
|
|
2
9
|
// Note that it's recommended for exodus wallets to not use 0 as it can hold regular btc (as in purpose 86, m/0/0).
|
|
3
10
|
// If a user syncs an ordinal Wallet (BE) with a non-ordinal Wallet (old Desktop), the non-ordinal wallet may burn ordinals as regular BTC
|
|
@@ -41,11 +41,13 @@ export const reloadBrc20TokenBalances = async ({
|
|
|
41
41
|
walletAccount,
|
|
42
42
|
asset,
|
|
43
43
|
ordinalChainIndex,
|
|
44
|
+
tickInscriptionIdIndexer,
|
|
44
45
|
}) => {
|
|
45
46
|
assert(brc20Client, 'brc20Client is required')
|
|
46
47
|
assert(assetClientInterface, 'assetClientInterface is required')
|
|
47
48
|
assert(asset, 'asset is required')
|
|
48
49
|
assert(walletAccount, 'walletAccount is required')
|
|
50
|
+
assert(tickInscriptionIdIndexer, 'tickInscriptionIdIndexer is required')
|
|
49
51
|
assert(typeof ordinalChainIndex === 'number', 'ordinalChainIndex must be a number')
|
|
50
52
|
const addresses = await getInscriptionAddresses({
|
|
51
53
|
assetClientInterface,
|
|
@@ -63,25 +65,31 @@ export const reloadBrc20TokenBalances = async ({
|
|
|
63
65
|
return map
|
|
64
66
|
}, new Map())
|
|
65
67
|
|
|
66
|
-
const
|
|
68
|
+
const unknownDeployInscriptionIds = new Set()
|
|
67
69
|
const knownInscriptionIds = new Set()
|
|
68
70
|
|
|
69
71
|
const brc20History = (
|
|
70
72
|
await Promise.all(
|
|
71
73
|
addresses.map(async (address) => {
|
|
72
74
|
const tokensHistoryList = await brc20Client.getTokenTransactionHistory({ owner: address })
|
|
73
|
-
return
|
|
74
|
-
.
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
75
|
+
return (
|
|
76
|
+
await Promise.all(
|
|
77
|
+
tokensHistoryList.map(async (token) => {
|
|
78
|
+
const deployInscriptionId = await tickInscriptionIdIndexer.getDeployInscriptionId(
|
|
79
|
+
token.brc20Tick
|
|
80
|
+
)
|
|
81
|
+
knownInscriptionIds.add(deployInscriptionId)
|
|
82
|
+
knownInscriptionIds.add(token.inscriptionId.toLowerCase())
|
|
83
|
+
const assetName = assetByAssetId.get(deployInscriptionId)?.name
|
|
84
|
+
if (assetName) {
|
|
85
|
+
return { ...token, address, assetName, deployInscriptionId }
|
|
86
|
+
} else {
|
|
87
|
+
unknownDeployInscriptionIds.add(deployInscriptionId)
|
|
88
|
+
return undefined
|
|
89
|
+
}
|
|
90
|
+
})
|
|
91
|
+
)
|
|
92
|
+
).filter(Boolean)
|
|
85
93
|
})
|
|
86
94
|
)
|
|
87
95
|
).flat()
|
|
@@ -149,6 +157,6 @@ export const reloadBrc20TokenBalances = async ({
|
|
|
149
157
|
})
|
|
150
158
|
|
|
151
159
|
return {
|
|
152
|
-
|
|
160
|
+
unknownDeployInscriptionIds: [...unknownDeployInscriptionIds],
|
|
153
161
|
}
|
|
154
162
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import assert from 'minimalistic-assert'
|
|
2
|
+
|
|
3
|
+
export const tickInscriptionIdIndexerFactory = ({ brc20Client }) => {
|
|
4
|
+
const deployInscriptionIdToBrc20Tick = new Map()
|
|
5
|
+
const brc20TickToDeployInscriptionId = new Map()
|
|
6
|
+
|
|
7
|
+
const save = ({ deployInscriptionId, brc20Tick }) => {
|
|
8
|
+
deployInscriptionIdToBrc20Tick.set(deployInscriptionId, brc20Tick)
|
|
9
|
+
brc20TickToDeployInscriptionId.set(brc20Tick, deployInscriptionId)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const loadDeployInscriptionId = async (brc20Tick) => {
|
|
13
|
+
const response = await brc20Client.getTickerInfo({ tick: brc20Tick })
|
|
14
|
+
const deployInscriptionId = response[0].inscriptionId
|
|
15
|
+
save({ deployInscriptionId, brc20Tick })
|
|
16
|
+
return deployInscriptionId
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const loadBrc20Tick = async (deployInscriptionId) => {
|
|
20
|
+
const response = await brc20Client.getTickerInfoByDeployInscriptionId({ deployInscriptionId })
|
|
21
|
+
const brc20Tick = response[0].brc20Tick
|
|
22
|
+
save({ deployInscriptionId, brc20Tick })
|
|
23
|
+
return brc20Tick
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const getDeployInscriptionId = async (brc20Tick) => {
|
|
27
|
+
return brc20TickToDeployInscriptionId.get(brc20Tick) || loadDeployInscriptionId(brc20Tick)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const getBrc20Tick = async (deployInscriptionId) => {
|
|
31
|
+
return (
|
|
32
|
+
deployInscriptionIdToBrc20Tick.get(deployInscriptionId) || loadBrc20Tick(deployInscriptionId)
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const getBrc20TickFromAsset = async (asset) => {
|
|
37
|
+
assert(asset.assetType === 'BRC20_TOKEN', `Asset ${asset.name} is not a brc20 asset`)
|
|
38
|
+
const deployInscriptionId = asset.assetId
|
|
39
|
+
const brc20Tick = asset.brc20Tick // this should always be true once CTR is patched
|
|
40
|
+
if (brc20Tick) {
|
|
41
|
+
save({ deployInscriptionId, brc20Tick })
|
|
42
|
+
return brc20Tick
|
|
43
|
+
}
|
|
44
|
+
return getBrc20Tick(deployInscriptionId)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return { getBrc20TickFromAsset, getDeployInscriptionId, getBrc20Tick }
|
|
48
|
+
}
|