@teleportdao/bitcoin 2.0.4 → 2.0.7
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/dist/bitcoin-interface-ordinal.d.ts +108 -108
- package/dist/bitcoin-interface-ordinal.js +140 -140
- package/dist/bitcoin-interface-teleswap.d.ts +101 -101
- package/dist/bitcoin-interface-teleswap.d.ts.map +1 -1
- package/dist/bitcoin-interface-teleswap.js +176 -165
- package/dist/bitcoin-interface-teleswap.js.map +1 -1
- package/dist/bitcoin-interface-utils.d.ts +20 -20
- package/dist/bitcoin-interface-utils.js +45 -45
- package/dist/bitcoin-interface-wallet.d.ts +28 -28
- package/dist/bitcoin-interface-wallet.js +125 -125
- package/dist/bitcoin-interface.d.ts +66 -66
- package/dist/bitcoin-interface.js +119 -119
- package/dist/bitcoin-utils.d.ts +96 -96
- package/dist/bitcoin-utils.js +514 -514
- package/dist/bitcoin-wallet-base.d.ts +111 -111
- package/dist/bitcoin-wallet-base.js +258 -258
- package/dist/helper/brc20-helper.d.ts +42 -42
- package/dist/helper/brc20-helper.js +127 -127
- package/dist/helper/index.d.ts +3 -3
- package/dist/helper/index.js +29 -29
- package/dist/helper/ordinal-helper.d.ts +12 -12
- package/dist/helper/ordinal-helper.js +129 -129
- package/dist/helper/teleswap-helper.d.ts +95 -95
- package/dist/helper/teleswap-helper.js +186 -186
- package/dist/index.d.ts +12 -12
- package/dist/index.js +41 -41
- package/dist/ordinal-wallet.d.ts +495 -495
- package/dist/ordinal-wallet.js +386 -386
- package/dist/sign/index.d.ts +1 -1
- package/dist/sign/index.js +8 -8
- package/dist/sign/sign-transaction.d.ts +12 -12
- package/dist/sign/sign-transaction.js +82 -82
- package/dist/teleswap-wallet.d.ts +45 -45
- package/dist/teleswap-wallet.js +68 -68
- package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +9 -9
- package/dist/transaction-builder/bitcoin-transaction-builder.js +54 -54
- package/dist/transaction-builder/index.d.ts +3 -3
- package/dist/transaction-builder/index.js +19 -19
- package/dist/transaction-builder/ordinal-transaction-builder.d.ts +63 -63
- package/dist/transaction-builder/ordinal-transaction-builder.js +125 -125
- package/dist/transaction-builder/transaction-builder.d.ts +223 -223
- package/dist/transaction-builder/transaction-builder.js +447 -447
- package/dist/type.d.ts +61 -61
- package/dist/type.js +2 -2
- package/dist/utils/networks.d.ts +5 -5
- package/dist/utils/networks.js +53 -53
- package/dist/utils/tools.d.ts +18 -18
- package/dist/utils/tools.js +74 -74
- package/package.json +4 -4
- package/src/bitcoin-interface-ordinal.ts +185 -185
- package/src/bitcoin-interface-teleswap.ts +251 -237
- package/src/bitcoin-interface-utils.ts +60 -60
- package/src/bitcoin-interface-wallet.ts +114 -114
- package/src/bitcoin-interface.ts +156 -156
- package/src/bitcoin-utils.ts +591 -591
- package/src/bitcoin-wallet-base.ts +344 -344
- package/src/helper/brc20-helper.ts +179 -179
- package/src/helper/ordinal-helper.ts +118 -118
- package/src/index.ts +15 -15
- package/src/ordinal-wallet.ts +659 -659
- package/src/sign/index.ts +1 -1
- package/src/sign/sign-transaction.ts +108 -108
- package/src/teleswap-wallet.ts +133 -133
- package/src/transaction-builder/bitcoin-transaction-builder.ts +26 -26
- package/src/transaction-builder/index.ts +3 -3
- package/src/transaction-builder/ordinal-transaction-builder.ts +139 -139
- package/src/transaction-builder/transaction-builder.ts +690 -690
- package/src/type.ts +74 -74
- package/src/utils/networks.ts +33 -33
- package/src/utils/tools.ts +92 -92
- package/tsconfig.json +9 -9
- package/webpack.config.js +16 -16
- package/.tmp/block-parser.ts +0 -58
- package/.tmp/check.ts +0 -101
- package/.tmp/ordinal-helper.ts +0 -133
- package/.tmp/ordinal.ts +0 -25
- package/.tmp/psbt/sign-transaction.ts +0 -121
- package/.tmp/rbf.ts +0 -45
|
@@ -1,179 +1,179 @@
|
|
|
1
|
-
import BigNumber from "bignumber.js"
|
|
2
|
-
import { brc20 } from "@teleportdao/configs"
|
|
3
|
-
|
|
4
|
-
export type WrapOpReturn = {
|
|
5
|
-
chainId: number
|
|
6
|
-
appId: number
|
|
7
|
-
brc20TokenId: number
|
|
8
|
-
recipientAddress: string
|
|
9
|
-
inputAmount: string
|
|
10
|
-
outputAmount?: string
|
|
11
|
-
outputToken?: string
|
|
12
|
-
thirdPartyId?: number
|
|
13
|
-
}
|
|
14
|
-
export type WrapOpReturnAndType = WrapOpReturn & {
|
|
15
|
-
requestType: "Wrap" | "WrapAndSwap"
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const brc20WrapRequests = brc20.requestAppId
|
|
19
|
-
|
|
20
|
-
export function generateBrc2OpReturn({
|
|
21
|
-
chainId,
|
|
22
|
-
appId,
|
|
23
|
-
brc20TokenId,
|
|
24
|
-
inputAmount,
|
|
25
|
-
recipientAddress,
|
|
26
|
-
thirdPartyId = 0,
|
|
27
|
-
isExchange = false,
|
|
28
|
-
outputToken,
|
|
29
|
-
outputAmount,
|
|
30
|
-
}: WrapOpReturn & {
|
|
31
|
-
isExchange?: boolean
|
|
32
|
-
}) {
|
|
33
|
-
let data = ""
|
|
34
|
-
if (chainId.toString(16).length > 4) throw new Error("chainId should be less than 2 bytes")
|
|
35
|
-
data += chainId.toString(16).padStart(4, "0") // 2 bytes
|
|
36
|
-
data += appId.toString(16).padStart(2, "0") // 1 bytes
|
|
37
|
-
data += brc20TokenId.toString(16).padStart(4, "0") // 2 bytes
|
|
38
|
-
data += BigNumber(inputAmount).toString(16).padStart(26, "0") // 13 bytes
|
|
39
|
-
data += recipientAddress.replace("0x", "").toLowerCase().padStart(40, "0") // 20 bytes
|
|
40
|
-
data += BigNumber(thirdPartyId).toString(16).padStart(2, "0") // 1 bytes
|
|
41
|
-
if (isExchange) {
|
|
42
|
-
if (!outputAmount || !outputToken) {
|
|
43
|
-
throw new Error("outputAmount and outputToken are required for exchange")
|
|
44
|
-
}
|
|
45
|
-
data += outputToken.replace("0x", "").toLowerCase().padStart(40, "0") // 20 bytes
|
|
46
|
-
data += BigNumber(outputAmount).toString(16).padStart(26, "0") // 13 bytes
|
|
47
|
-
}
|
|
48
|
-
return data
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function parseBrc20OpReturn(data: string) {
|
|
52
|
-
let parsedData: Partial<WrapOpReturnAndType> = {}
|
|
53
|
-
parsedData.chainId = Number(`0x${data.slice(0, 4)}`) // 2 bytes
|
|
54
|
-
parsedData.appId = Number(`0x${data.slice(4, 6)}`) // 1 bytes
|
|
55
|
-
parsedData.brc20TokenId = Number(`0x${data.slice(6, 10)}`) // 2 bytes
|
|
56
|
-
parsedData.inputAmount = new BigNumber(`0x${data.slice(10, 36)}`).toFixed(0) // 11 bytes
|
|
57
|
-
parsedData.recipientAddress = `0x${data.slice(36, 76)}` // 20 bytes
|
|
58
|
-
parsedData.thirdPartyId = Number(`0x${data.slice(76, 78)}`) // 20 bytes
|
|
59
|
-
if (data.length === 78) {
|
|
60
|
-
parsedData.requestType = "Wrap"
|
|
61
|
-
return {
|
|
62
|
-
status: true,
|
|
63
|
-
data: parsedData as WrapOpReturnAndType,
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
parsedData.outputToken = `0x${data.slice(78, 118)}` // 20 bytes
|
|
67
|
-
parsedData.outputAmount = new BigNumber(`0x${data.slice(118, 144)}`).toFixed(0) // 20 bytes
|
|
68
|
-
if (data.length === 144) {
|
|
69
|
-
parsedData.requestType = "WrapAndSwap"
|
|
70
|
-
return {
|
|
71
|
-
status: true,
|
|
72
|
-
data: parsedData as WrapOpReturnAndType,
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return {
|
|
77
|
-
status: false,
|
|
78
|
-
message: `invalid OP_RETURN data for requestType: 'wrap or exchange'. invalid data length : ${data.length} - valid length : 144 , 78`,
|
|
79
|
-
code: "INVALID_OP_RETURN",
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function parseBrc20RawRequest(opReturnData: string): {
|
|
84
|
-
status: boolean
|
|
85
|
-
data?: WrapOpReturnAndType
|
|
86
|
-
message?: string
|
|
87
|
-
code?: string
|
|
88
|
-
} {
|
|
89
|
-
let data = opReturnData.slice(2, 4) === "4c" ? opReturnData.slice(6) : opReturnData.slice(4)
|
|
90
|
-
// eslint-disable-next-line no-unused-vars
|
|
91
|
-
let appIdHex = data.slice(4, 6) // 2 bytes
|
|
92
|
-
if (!appIdHex) {
|
|
93
|
-
return {
|
|
94
|
-
status: false,
|
|
95
|
-
message: `invalid OP_RETURN data : ${data}`,
|
|
96
|
-
code: "INVALID_APP_ID",
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
let appId = Number(`0x${appIdHex}`) // 2 bytes
|
|
101
|
-
|
|
102
|
-
// get type base on appId
|
|
103
|
-
let requestType = Object.keys(brc20WrapRequests).find((key) =>
|
|
104
|
-
brc20WrapRequests[key as keyof typeof brc20WrapRequests].appIds.includes(appId),
|
|
105
|
-
)
|
|
106
|
-
|
|
107
|
-
switch (requestType) {
|
|
108
|
-
case "Wrap":
|
|
109
|
-
case "WrapAndSwap":
|
|
110
|
-
return parseBrc20OpReturn(data)
|
|
111
|
-
default:
|
|
112
|
-
return {
|
|
113
|
-
status: false,
|
|
114
|
-
message: `invalid appId : ${appId}`,
|
|
115
|
-
code: "INVALID_OP_RETURN",
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export function checkAndParseBrc20Request(
|
|
121
|
-
vouts: {
|
|
122
|
-
address?: string
|
|
123
|
-
script: string
|
|
124
|
-
value: number
|
|
125
|
-
}[],
|
|
126
|
-
address: string,
|
|
127
|
-
): {
|
|
128
|
-
status: boolean
|
|
129
|
-
data?: WrapOpReturnAndType
|
|
130
|
-
dataOutputIndex?: number
|
|
131
|
-
lockerFeeValue?: number
|
|
132
|
-
lockerFeeOutputIndex?: number
|
|
133
|
-
brc20OutputIndex?: number
|
|
134
|
-
brc20OutputValue?: number
|
|
135
|
-
message?: string
|
|
136
|
-
code?: string
|
|
137
|
-
} {
|
|
138
|
-
let requestOutputIndex = vouts.findIndex((vout_) => vout_.script.startsWith("6a"))
|
|
139
|
-
if (requestOutputIndex >= 0) {
|
|
140
|
-
let opReturnData = vouts[requestOutputIndex]?.script
|
|
141
|
-
if (!opReturnData) {
|
|
142
|
-
return {
|
|
143
|
-
status: false,
|
|
144
|
-
message: "invalid OP_RETURN",
|
|
145
|
-
code: "INVALID_OP_RETURN",
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
let brc20Transfer = vouts[0]
|
|
150
|
-
if (brc20Transfer.address !== address || brc20Transfer.value < 546) {
|
|
151
|
-
return {
|
|
152
|
-
status: false,
|
|
153
|
-
message: "no brc20 transfer output",
|
|
154
|
-
code: "NO_BRC20_TRANSFER",
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
let dataResponse = parseBrc20RawRequest(opReturnData)
|
|
159
|
-
if (dataResponse.status) {
|
|
160
|
-
let lockerFeeOutputIndex = vouts.findIndex((vout_, i) => vout_.address === address && +i > 0)
|
|
161
|
-
let lockerFeeValue = lockerFeeOutputIndex >= 0 ? vouts[lockerFeeOutputIndex].value : 0
|
|
162
|
-
return {
|
|
163
|
-
status: true,
|
|
164
|
-
data: dataResponse.data!,
|
|
165
|
-
dataOutputIndex: requestOutputIndex,
|
|
166
|
-
lockerFeeValue,
|
|
167
|
-
lockerFeeOutputIndex,
|
|
168
|
-
brc20OutputIndex: 0,
|
|
169
|
-
brc20OutputValue: brc20Transfer.value,
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
return dataResponse
|
|
173
|
-
}
|
|
174
|
-
return {
|
|
175
|
-
status: false,
|
|
176
|
-
message: "transaction outputs should contain OP_RETURN",
|
|
177
|
-
code: "NO_OP_RETURN",
|
|
178
|
-
}
|
|
179
|
-
}
|
|
1
|
+
import BigNumber from "bignumber.js"
|
|
2
|
+
import { brc20 } from "@teleportdao/configs"
|
|
3
|
+
|
|
4
|
+
export type WrapOpReturn = {
|
|
5
|
+
chainId: number
|
|
6
|
+
appId: number
|
|
7
|
+
brc20TokenId: number
|
|
8
|
+
recipientAddress: string
|
|
9
|
+
inputAmount: string
|
|
10
|
+
outputAmount?: string
|
|
11
|
+
outputToken?: string
|
|
12
|
+
thirdPartyId?: number
|
|
13
|
+
}
|
|
14
|
+
export type WrapOpReturnAndType = WrapOpReturn & {
|
|
15
|
+
requestType: "Wrap" | "WrapAndSwap"
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const brc20WrapRequests = brc20.requestAppId
|
|
19
|
+
|
|
20
|
+
export function generateBrc2OpReturn({
|
|
21
|
+
chainId,
|
|
22
|
+
appId,
|
|
23
|
+
brc20TokenId,
|
|
24
|
+
inputAmount,
|
|
25
|
+
recipientAddress,
|
|
26
|
+
thirdPartyId = 0,
|
|
27
|
+
isExchange = false,
|
|
28
|
+
outputToken,
|
|
29
|
+
outputAmount,
|
|
30
|
+
}: WrapOpReturn & {
|
|
31
|
+
isExchange?: boolean
|
|
32
|
+
}) {
|
|
33
|
+
let data = ""
|
|
34
|
+
if (chainId.toString(16).length > 4) throw new Error("chainId should be less than 2 bytes")
|
|
35
|
+
data += chainId.toString(16).padStart(4, "0") // 2 bytes
|
|
36
|
+
data += appId.toString(16).padStart(2, "0") // 1 bytes
|
|
37
|
+
data += brc20TokenId.toString(16).padStart(4, "0") // 2 bytes
|
|
38
|
+
data += BigNumber(inputAmount).toString(16).padStart(26, "0") // 13 bytes
|
|
39
|
+
data += recipientAddress.replace("0x", "").toLowerCase().padStart(40, "0") // 20 bytes
|
|
40
|
+
data += BigNumber(thirdPartyId).toString(16).padStart(2, "0") // 1 bytes
|
|
41
|
+
if (isExchange) {
|
|
42
|
+
if (!outputAmount || !outputToken) {
|
|
43
|
+
throw new Error("outputAmount and outputToken are required for exchange")
|
|
44
|
+
}
|
|
45
|
+
data += outputToken.replace("0x", "").toLowerCase().padStart(40, "0") // 20 bytes
|
|
46
|
+
data += BigNumber(outputAmount).toString(16).padStart(26, "0") // 13 bytes
|
|
47
|
+
}
|
|
48
|
+
return data
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function parseBrc20OpReturn(data: string) {
|
|
52
|
+
let parsedData: Partial<WrapOpReturnAndType> = {}
|
|
53
|
+
parsedData.chainId = Number(`0x${data.slice(0, 4)}`) // 2 bytes
|
|
54
|
+
parsedData.appId = Number(`0x${data.slice(4, 6)}`) // 1 bytes
|
|
55
|
+
parsedData.brc20TokenId = Number(`0x${data.slice(6, 10)}`) // 2 bytes
|
|
56
|
+
parsedData.inputAmount = new BigNumber(`0x${data.slice(10, 36)}`).toFixed(0) // 11 bytes
|
|
57
|
+
parsedData.recipientAddress = `0x${data.slice(36, 76)}` // 20 bytes
|
|
58
|
+
parsedData.thirdPartyId = Number(`0x${data.slice(76, 78)}`) // 20 bytes
|
|
59
|
+
if (data.length === 78) {
|
|
60
|
+
parsedData.requestType = "Wrap"
|
|
61
|
+
return {
|
|
62
|
+
status: true,
|
|
63
|
+
data: parsedData as WrapOpReturnAndType,
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
parsedData.outputToken = `0x${data.slice(78, 118)}` // 20 bytes
|
|
67
|
+
parsedData.outputAmount = new BigNumber(`0x${data.slice(118, 144)}`).toFixed(0) // 20 bytes
|
|
68
|
+
if (data.length === 144) {
|
|
69
|
+
parsedData.requestType = "WrapAndSwap"
|
|
70
|
+
return {
|
|
71
|
+
status: true,
|
|
72
|
+
data: parsedData as WrapOpReturnAndType,
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
status: false,
|
|
78
|
+
message: `invalid OP_RETURN data for requestType: 'wrap or exchange'. invalid data length : ${data.length} - valid length : 144 , 78`,
|
|
79
|
+
code: "INVALID_OP_RETURN",
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function parseBrc20RawRequest(opReturnData: string): {
|
|
84
|
+
status: boolean
|
|
85
|
+
data?: WrapOpReturnAndType
|
|
86
|
+
message?: string
|
|
87
|
+
code?: string
|
|
88
|
+
} {
|
|
89
|
+
let data = opReturnData.slice(2, 4) === "4c" ? opReturnData.slice(6) : opReturnData.slice(4)
|
|
90
|
+
// eslint-disable-next-line no-unused-vars
|
|
91
|
+
let appIdHex = data.slice(4, 6) // 2 bytes
|
|
92
|
+
if (!appIdHex) {
|
|
93
|
+
return {
|
|
94
|
+
status: false,
|
|
95
|
+
message: `invalid OP_RETURN data : ${data}`,
|
|
96
|
+
code: "INVALID_APP_ID",
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
let appId = Number(`0x${appIdHex}`) // 2 bytes
|
|
101
|
+
|
|
102
|
+
// get type base on appId
|
|
103
|
+
let requestType = Object.keys(brc20WrapRequests).find((key) =>
|
|
104
|
+
brc20WrapRequests[key as keyof typeof brc20WrapRequests].appIds.includes(appId),
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
switch (requestType) {
|
|
108
|
+
case "Wrap":
|
|
109
|
+
case "WrapAndSwap":
|
|
110
|
+
return parseBrc20OpReturn(data)
|
|
111
|
+
default:
|
|
112
|
+
return {
|
|
113
|
+
status: false,
|
|
114
|
+
message: `invalid appId : ${appId}`,
|
|
115
|
+
code: "INVALID_OP_RETURN",
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function checkAndParseBrc20Request(
|
|
121
|
+
vouts: {
|
|
122
|
+
address?: string
|
|
123
|
+
script: string
|
|
124
|
+
value: number
|
|
125
|
+
}[],
|
|
126
|
+
address: string,
|
|
127
|
+
): {
|
|
128
|
+
status: boolean
|
|
129
|
+
data?: WrapOpReturnAndType
|
|
130
|
+
dataOutputIndex?: number
|
|
131
|
+
lockerFeeValue?: number
|
|
132
|
+
lockerFeeOutputIndex?: number
|
|
133
|
+
brc20OutputIndex?: number
|
|
134
|
+
brc20OutputValue?: number
|
|
135
|
+
message?: string
|
|
136
|
+
code?: string
|
|
137
|
+
} {
|
|
138
|
+
let requestOutputIndex = vouts.findIndex((vout_) => vout_.script.startsWith("6a"))
|
|
139
|
+
if (requestOutputIndex >= 0) {
|
|
140
|
+
let opReturnData = vouts[requestOutputIndex]?.script
|
|
141
|
+
if (!opReturnData) {
|
|
142
|
+
return {
|
|
143
|
+
status: false,
|
|
144
|
+
message: "invalid OP_RETURN",
|
|
145
|
+
code: "INVALID_OP_RETURN",
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
let brc20Transfer = vouts[0]
|
|
150
|
+
if (brc20Transfer.address !== address || brc20Transfer.value < 546) {
|
|
151
|
+
return {
|
|
152
|
+
status: false,
|
|
153
|
+
message: "no brc20 transfer output",
|
|
154
|
+
code: "NO_BRC20_TRANSFER",
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
let dataResponse = parseBrc20RawRequest(opReturnData)
|
|
159
|
+
if (dataResponse.status) {
|
|
160
|
+
let lockerFeeOutputIndex = vouts.findIndex((vout_, i) => vout_.address === address && +i > 0)
|
|
161
|
+
let lockerFeeValue = lockerFeeOutputIndex >= 0 ? vouts[lockerFeeOutputIndex].value : 0
|
|
162
|
+
return {
|
|
163
|
+
status: true,
|
|
164
|
+
data: dataResponse.data!,
|
|
165
|
+
dataOutputIndex: requestOutputIndex,
|
|
166
|
+
lockerFeeValue,
|
|
167
|
+
lockerFeeOutputIndex,
|
|
168
|
+
brc20OutputIndex: 0,
|
|
169
|
+
brc20OutputValue: brc20Transfer.value,
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return dataResponse
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
status: false,
|
|
176
|
+
message: "transaction outputs should contain OP_RETURN",
|
|
177
|
+
code: "NO_OP_RETURN",
|
|
178
|
+
}
|
|
179
|
+
}
|
|
@@ -1,118 +1,118 @@
|
|
|
1
|
-
import * as bitcoin from "bitcoinjs-lib"
|
|
2
|
-
import ecc from "@bitcoinerlab/secp256k1"
|
|
3
|
-
|
|
4
|
-
bitcoin.initEccLib(ecc)
|
|
5
|
-
|
|
6
|
-
// eslint-disable-next-line consistent-return
|
|
7
|
-
export function createAddressObjectByScriptNoType(
|
|
8
|
-
script: Buffer,
|
|
9
|
-
network: bitcoin.Network = bitcoin.networks.bitcoin,
|
|
10
|
-
) {
|
|
11
|
-
let addressObject: bitcoin.payments.Payment
|
|
12
|
-
|
|
13
|
-
try {
|
|
14
|
-
addressObject = bitcoin.payments.p2pkh({
|
|
15
|
-
output: script,
|
|
16
|
-
network,
|
|
17
|
-
})
|
|
18
|
-
if (addressObject.address) {
|
|
19
|
-
return {
|
|
20
|
-
addressObject,
|
|
21
|
-
addressType: "p2pkh",
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
} catch (err) {
|
|
25
|
-
/* empty */
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
try {
|
|
29
|
-
addressObject = bitcoin.payments.p2wpkh({
|
|
30
|
-
output: script,
|
|
31
|
-
network,
|
|
32
|
-
})
|
|
33
|
-
if (addressObject.address) {
|
|
34
|
-
return {
|
|
35
|
-
addressObject,
|
|
36
|
-
addressType: "p2wpkh",
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
} catch (err) {
|
|
40
|
-
/* empty */
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
try {
|
|
44
|
-
addressObject = bitcoin.payments.p2sh({
|
|
45
|
-
output: script,
|
|
46
|
-
network,
|
|
47
|
-
})
|
|
48
|
-
if (addressObject.address) {
|
|
49
|
-
return {
|
|
50
|
-
addressObject,
|
|
51
|
-
addressType: "p2sh",
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
} catch (err) {
|
|
55
|
-
/* empty */
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
try {
|
|
59
|
-
addressObject = bitcoin.payments.p2wsh({
|
|
60
|
-
output: script,
|
|
61
|
-
network,
|
|
62
|
-
})
|
|
63
|
-
if (addressObject.address) {
|
|
64
|
-
return {
|
|
65
|
-
addressObject,
|
|
66
|
-
addressType: "p2wsh",
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
} catch (err) {
|
|
70
|
-
/* empty */
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
try {
|
|
74
|
-
addressObject = bitcoin.payments.p2tr({
|
|
75
|
-
output: script,
|
|
76
|
-
network,
|
|
77
|
-
})
|
|
78
|
-
if (addressObject.address) {
|
|
79
|
-
return {
|
|
80
|
-
addressObject,
|
|
81
|
-
addressType: "p2tr",
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
} catch (err) {
|
|
85
|
-
/* empty */
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export function splitBuffer(buffer: Buffer, partLength: number) {
|
|
90
|
-
const parts: Buffer[] = []
|
|
91
|
-
for (let i = 0; i < buffer.length; i += partLength) {
|
|
92
|
-
const part = buffer.subarray(i, i + partLength)
|
|
93
|
-
parts.push(part)
|
|
94
|
-
}
|
|
95
|
-
return parts
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export function getOrdinalScript(
|
|
99
|
-
file: { buffer: Buffer; type: string },
|
|
100
|
-
internalPublicKeyHex: string,
|
|
101
|
-
) {
|
|
102
|
-
let bufferParts = splitBuffer(file.buffer, 520)
|
|
103
|
-
const splittedFileHex = bufferParts.map((part) => part.toString("hex")).join(" ")
|
|
104
|
-
const fileTypeHex = Buffer.from(file.type!, "utf8").toString("hex")
|
|
105
|
-
const ORDINAL_ORD_HEX = Buffer.from("ord", "utf8").toString("hex")
|
|
106
|
-
const script = `${internalPublicKeyHex} OP_CHECKSIG OP_FALSE OP_IF ${ORDINAL_ORD_HEX} 00000000000000000000 ${fileTypeHex} OP_0 ${splittedFileHex} OP_ENDIF`
|
|
107
|
-
|
|
108
|
-
// we add 00000000000000000000 so we can modify INCORRECT OPCODE
|
|
109
|
-
const leafScript = Buffer.from(
|
|
110
|
-
bitcoin.script.fromASM(script).toString("hex").replace("0a00000000000000000000", "0101"),
|
|
111
|
-
"hex",
|
|
112
|
-
)
|
|
113
|
-
return leafScript
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export function toXOnly(pubKey: Buffer) {
|
|
117
|
-
return pubKey.length === 32 ? pubKey : pubKey.subarray(1, 33)
|
|
118
|
-
}
|
|
1
|
+
import * as bitcoin from "bitcoinjs-lib"
|
|
2
|
+
import ecc from "@bitcoinerlab/secp256k1"
|
|
3
|
+
|
|
4
|
+
bitcoin.initEccLib(ecc)
|
|
5
|
+
|
|
6
|
+
// eslint-disable-next-line consistent-return
|
|
7
|
+
export function createAddressObjectByScriptNoType(
|
|
8
|
+
script: Buffer,
|
|
9
|
+
network: bitcoin.Network = bitcoin.networks.bitcoin,
|
|
10
|
+
) {
|
|
11
|
+
let addressObject: bitcoin.payments.Payment
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
addressObject = bitcoin.payments.p2pkh({
|
|
15
|
+
output: script,
|
|
16
|
+
network,
|
|
17
|
+
})
|
|
18
|
+
if (addressObject.address) {
|
|
19
|
+
return {
|
|
20
|
+
addressObject,
|
|
21
|
+
addressType: "p2pkh",
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
} catch (err) {
|
|
25
|
+
/* empty */
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
addressObject = bitcoin.payments.p2wpkh({
|
|
30
|
+
output: script,
|
|
31
|
+
network,
|
|
32
|
+
})
|
|
33
|
+
if (addressObject.address) {
|
|
34
|
+
return {
|
|
35
|
+
addressObject,
|
|
36
|
+
addressType: "p2wpkh",
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
} catch (err) {
|
|
40
|
+
/* empty */
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
addressObject = bitcoin.payments.p2sh({
|
|
45
|
+
output: script,
|
|
46
|
+
network,
|
|
47
|
+
})
|
|
48
|
+
if (addressObject.address) {
|
|
49
|
+
return {
|
|
50
|
+
addressObject,
|
|
51
|
+
addressType: "p2sh",
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
} catch (err) {
|
|
55
|
+
/* empty */
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
addressObject = bitcoin.payments.p2wsh({
|
|
60
|
+
output: script,
|
|
61
|
+
network,
|
|
62
|
+
})
|
|
63
|
+
if (addressObject.address) {
|
|
64
|
+
return {
|
|
65
|
+
addressObject,
|
|
66
|
+
addressType: "p2wsh",
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
} catch (err) {
|
|
70
|
+
/* empty */
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
addressObject = bitcoin.payments.p2tr({
|
|
75
|
+
output: script,
|
|
76
|
+
network,
|
|
77
|
+
})
|
|
78
|
+
if (addressObject.address) {
|
|
79
|
+
return {
|
|
80
|
+
addressObject,
|
|
81
|
+
addressType: "p2tr",
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
} catch (err) {
|
|
85
|
+
/* empty */
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function splitBuffer(buffer: Buffer, partLength: number) {
|
|
90
|
+
const parts: Buffer[] = []
|
|
91
|
+
for (let i = 0; i < buffer.length; i += partLength) {
|
|
92
|
+
const part = buffer.subarray(i, i + partLength)
|
|
93
|
+
parts.push(part)
|
|
94
|
+
}
|
|
95
|
+
return parts
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function getOrdinalScript(
|
|
99
|
+
file: { buffer: Buffer; type: string },
|
|
100
|
+
internalPublicKeyHex: string,
|
|
101
|
+
) {
|
|
102
|
+
let bufferParts = splitBuffer(file.buffer, 520)
|
|
103
|
+
const splittedFileHex = bufferParts.map((part) => part.toString("hex")).join(" ")
|
|
104
|
+
const fileTypeHex = Buffer.from(file.type!, "utf8").toString("hex")
|
|
105
|
+
const ORDINAL_ORD_HEX = Buffer.from("ord", "utf8").toString("hex")
|
|
106
|
+
const script = `${internalPublicKeyHex} OP_CHECKSIG OP_FALSE OP_IF ${ORDINAL_ORD_HEX} 00000000000000000000 ${fileTypeHex} OP_0 ${splittedFileHex} OP_ENDIF`
|
|
107
|
+
|
|
108
|
+
// we add 00000000000000000000 so we can modify INCORRECT OPCODE
|
|
109
|
+
const leafScript = Buffer.from(
|
|
110
|
+
bitcoin.script.fromASM(script).toString("hex").replace("0a00000000000000000000", "0101"),
|
|
111
|
+
"hex",
|
|
112
|
+
)
|
|
113
|
+
return leafScript
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function toXOnly(pubKey: Buffer) {
|
|
117
|
+
return pubKey.length === 32 ? pubKey : pubKey.subarray(1, 33)
|
|
118
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
export * as bitcoinUtils from "./bitcoin-utils"
|
|
2
|
-
export * from "./helper"
|
|
3
|
-
export * from "./type"
|
|
4
|
-
//
|
|
5
|
-
export * from "./teleswap-wallet"
|
|
6
|
-
export * from "./ordinal-wallet"
|
|
7
|
-
export * from "./bitcoin-wallet-base"
|
|
8
|
-
//
|
|
9
|
-
export * from "./bitcoin-interface-utils"
|
|
10
|
-
export * from "./bitcoin-interface"
|
|
11
|
-
export * from "./bitcoin-interface-teleswap"
|
|
12
|
-
export * from "./bitcoin-interface-ordinal"
|
|
13
|
-
//
|
|
14
|
-
export * from "./transaction-builder"
|
|
15
|
-
export * from "./sign"
|
|
1
|
+
export * as bitcoinUtils from "./bitcoin-utils"
|
|
2
|
+
export * from "./helper"
|
|
3
|
+
export * from "./type"
|
|
4
|
+
//
|
|
5
|
+
export * from "./teleswap-wallet"
|
|
6
|
+
export * from "./ordinal-wallet"
|
|
7
|
+
export * from "./bitcoin-wallet-base"
|
|
8
|
+
//
|
|
9
|
+
export * from "./bitcoin-interface-utils"
|
|
10
|
+
export * from "./bitcoin-interface"
|
|
11
|
+
export * from "./bitcoin-interface-teleswap"
|
|
12
|
+
export * from "./bitcoin-interface-ordinal"
|
|
13
|
+
//
|
|
14
|
+
export * from "./transaction-builder"
|
|
15
|
+
export * from "./sign"
|