@teleportdao/bitcoin 1.7.20 → 1.7.21
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/.tmp/rbf.ts +24 -27
- package/dist/helper/teleswap-helper.js +1 -1
- package/dist/helper/teleswap-helper.js.map +1 -1
- package/package.json +3 -3
- package/src/bitcoin-interface-ordinal.ts +181 -181
- package/src/bitcoin-interface-teleswap.ts +252 -252
- package/src/bitcoin-interface-utils.ts +60 -60
- package/src/bitcoin-interface.ts +241 -241
- package/src/bitcoin-utils.ts +591 -591
- package/src/bitcoin-wallet-base.ts +310 -310
- package/src/helper/brc20-helper.ts +181 -181
- package/src/helper/ordinal-helper.ts +118 -118
- package/src/helper/teleswap-helper.ts +1 -1
- package/src/index.ts +15 -15
- package/src/ordinal-wallet.ts +738 -738
- package/src/sign/index.ts +1 -1
- package/src/sign/sign-transaction.ts +108 -108
- package/src/teleswap-wallet.ts +155 -155
- package/src/transaction-builder/bitcoin-transaction-builder.ts +44 -44
- package/src/transaction-builder/index.ts +3 -3
- package/src/transaction-builder/ordinal-transaction-builder.ts +147 -147
- package/src/transaction-builder/transaction-builder.ts +706 -706
- package/src/type.ts +48 -48
- package/src/utils/networks.ts +33 -33
- package/src/utils/tools.ts +90 -90
- package/tsconfig.json +9 -9
- package/webpack.config.js +16 -16
- package/dist/bitcoin-base.d.ts +0 -93
- package/dist/bitcoin-base.d.ts.map +0 -1
- package/dist/bitcoin-base.js +0 -236
- package/dist/bitcoin-base.js.map +0 -1
- package/dist/helper/burn-request-helper.d.ts +0 -7
- package/dist/helper/burn-request-helper.d.ts.map +0 -1
- package/dist/helper/burn-request-helper.js +0 -26
- package/dist/helper/burn-request-helper.js.map +0 -1
- package/dist/helper/teleport-request-helper.d.ts +0 -47
- package/dist/helper/teleport-request-helper.d.ts.map +0 -1
- package/dist/helper/teleport-request-helper.js +0 -146
- package/dist/helper/teleport-request-helper.js.map +0 -1
- package/dist/teleport-dao-payments.d.ts +0 -76
- package/dist/teleport-dao-payments.d.ts.map +0 -1
- package/dist/teleport-dao-payments.js +0 -217
- package/dist/teleport-dao-payments.js.map +0 -1
package/src/sign/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { default as BaseBitcoinSigner } from "./sign-transaction"
|
|
1
|
+
export { default as BaseBitcoinSigner } from "./sign-transaction"
|
|
@@ -1,108 +1,108 @@
|
|
|
1
|
-
import { Psbt, crypto, Network } from "bitcoinjs-lib"
|
|
2
|
-
// import BIP32Factory from "bip32"
|
|
3
|
-
import ecc from "@bitcoinerlab/secp256k1"
|
|
4
|
-
import ECPairFactory from "ecpair"
|
|
5
|
-
|
|
6
|
-
const ECPair = ECPairFactory(ecc)
|
|
7
|
-
|
|
8
|
-
function tapTweakHash(pubKey: Buffer, h?: Buffer) {
|
|
9
|
-
return crypto.taggedHash("TapTweak", Buffer.concat(h ? [pubKey, h] : [pubKey]))
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function tweakSigner(
|
|
13
|
-
privateKey: Buffer,
|
|
14
|
-
network: Network,
|
|
15
|
-
opts = {} as {
|
|
16
|
-
[key: string]: Buffer
|
|
17
|
-
},
|
|
18
|
-
) {
|
|
19
|
-
let newPrv = privateKey
|
|
20
|
-
let keyPair = ECPair.fromPrivateKey(privateKey, {
|
|
21
|
-
network,
|
|
22
|
-
compressed: true,
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
if (!keyPair.privateKey) throw new Error("private key not exist")
|
|
26
|
-
|
|
27
|
-
if (keyPair.publicKey.toString("hex").startsWith("03")) {
|
|
28
|
-
newPrv = ecc.privateNegate(keyPair.privateKey) as Buffer
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const tweakedPrivateKey = ecc.privateAdd(
|
|
32
|
-
newPrv,
|
|
33
|
-
tapTweakHash(Buffer.from(keyPair.publicKey.toString("hex").slice(2), "hex"), opts?.tweakHash),
|
|
34
|
-
)
|
|
35
|
-
if (!tweakedPrivateKey) {
|
|
36
|
-
throw new Error("Invalid tweaked private key!")
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return ECPair.fromPrivateKey(Buffer.from(tweakedPrivateKey), {
|
|
40
|
-
network,
|
|
41
|
-
})
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
class BitcoinLikeSignTransaction {
|
|
45
|
-
network: Network
|
|
46
|
-
|
|
47
|
-
constructor(network: Network) {
|
|
48
|
-
this.network = network
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
async signPsbt(
|
|
52
|
-
unsignedPsbt: {
|
|
53
|
-
unsignedTransaction: string
|
|
54
|
-
inputsToSign?: number[]
|
|
55
|
-
},
|
|
56
|
-
privateKey: Buffer,
|
|
57
|
-
sighashTypes?: any[],
|
|
58
|
-
usingTweakSignerIfNeeded = true,
|
|
59
|
-
) {
|
|
60
|
-
const { network } = this
|
|
61
|
-
const keyPair = ECPair.fromPrivateKey(privateKey, {
|
|
62
|
-
network,
|
|
63
|
-
compressed: true,
|
|
64
|
-
})
|
|
65
|
-
const psbt = Psbt.fromBase64(unsignedPsbt.unsignedTransaction, {
|
|
66
|
-
network,
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
let numberOfInputs = psbt.inputCount
|
|
70
|
-
|
|
71
|
-
for (let i = 0; i < numberOfInputs; i += 1) {
|
|
72
|
-
if (unsignedPsbt.inputsToSign && !unsignedPsbt.inputsToSign.includes(i)) {
|
|
73
|
-
// eslint-disable-next-line no-continue
|
|
74
|
-
continue
|
|
75
|
-
}
|
|
76
|
-
let type = psbt.getInputType(i)
|
|
77
|
-
|
|
78
|
-
if (usingTweakSignerIfNeeded && type === "nonstandard") {
|
|
79
|
-
console.log("using tweak signer")
|
|
80
|
-
let a = tweakSigner(privateKey, this.network)
|
|
81
|
-
await psbt.signInputAsync(i, a, sighashTypes)
|
|
82
|
-
} else {
|
|
83
|
-
await psbt.signInputAsync(i, keyPair, sighashTypes)
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// psbt.signAllInputs(keyPair)
|
|
88
|
-
const partialSigendPsbt = psbt.toBase64()
|
|
89
|
-
return partialSigendPsbt
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
finalizePsbts(psbtsBase64: string[] = []) {
|
|
93
|
-
const finals = psbtsBase64.map((psbtBase64) =>
|
|
94
|
-
Psbt.fromBase64(psbtBase64, { network: this.network }),
|
|
95
|
-
)
|
|
96
|
-
const psbt =
|
|
97
|
-
finals.length === 1 ? finals[0] : new Psbt({ network: this.network }).combine(...finals)
|
|
98
|
-
|
|
99
|
-
psbt.finalizeAllInputs()
|
|
100
|
-
|
|
101
|
-
let finalizeTx = psbt.extractTransaction()
|
|
102
|
-
// const rawTx = finalizeTx.toBuffer()
|
|
103
|
-
// const hex = rawTx.toString("hex")
|
|
104
|
-
return finalizeTx.toHex()
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export default BitcoinLikeSignTransaction
|
|
1
|
+
import { Psbt, crypto, Network } from "bitcoinjs-lib"
|
|
2
|
+
// import BIP32Factory from "bip32"
|
|
3
|
+
import ecc from "@bitcoinerlab/secp256k1"
|
|
4
|
+
import ECPairFactory from "ecpair"
|
|
5
|
+
|
|
6
|
+
const ECPair = ECPairFactory(ecc)
|
|
7
|
+
|
|
8
|
+
function tapTweakHash(pubKey: Buffer, h?: Buffer) {
|
|
9
|
+
return crypto.taggedHash("TapTweak", Buffer.concat(h ? [pubKey, h] : [pubKey]))
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function tweakSigner(
|
|
13
|
+
privateKey: Buffer,
|
|
14
|
+
network: Network,
|
|
15
|
+
opts = {} as {
|
|
16
|
+
[key: string]: Buffer
|
|
17
|
+
},
|
|
18
|
+
) {
|
|
19
|
+
let newPrv = privateKey
|
|
20
|
+
let keyPair = ECPair.fromPrivateKey(privateKey, {
|
|
21
|
+
network,
|
|
22
|
+
compressed: true,
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
if (!keyPair.privateKey) throw new Error("private key not exist")
|
|
26
|
+
|
|
27
|
+
if (keyPair.publicKey.toString("hex").startsWith("03")) {
|
|
28
|
+
newPrv = ecc.privateNegate(keyPair.privateKey) as Buffer
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const tweakedPrivateKey = ecc.privateAdd(
|
|
32
|
+
newPrv,
|
|
33
|
+
tapTweakHash(Buffer.from(keyPair.publicKey.toString("hex").slice(2), "hex"), opts?.tweakHash),
|
|
34
|
+
)
|
|
35
|
+
if (!tweakedPrivateKey) {
|
|
36
|
+
throw new Error("Invalid tweaked private key!")
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return ECPair.fromPrivateKey(Buffer.from(tweakedPrivateKey), {
|
|
40
|
+
network,
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
class BitcoinLikeSignTransaction {
|
|
45
|
+
network: Network
|
|
46
|
+
|
|
47
|
+
constructor(network: Network) {
|
|
48
|
+
this.network = network
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async signPsbt(
|
|
52
|
+
unsignedPsbt: {
|
|
53
|
+
unsignedTransaction: string
|
|
54
|
+
inputsToSign?: number[]
|
|
55
|
+
},
|
|
56
|
+
privateKey: Buffer,
|
|
57
|
+
sighashTypes?: any[],
|
|
58
|
+
usingTweakSignerIfNeeded = true,
|
|
59
|
+
) {
|
|
60
|
+
const { network } = this
|
|
61
|
+
const keyPair = ECPair.fromPrivateKey(privateKey, {
|
|
62
|
+
network,
|
|
63
|
+
compressed: true,
|
|
64
|
+
})
|
|
65
|
+
const psbt = Psbt.fromBase64(unsignedPsbt.unsignedTransaction, {
|
|
66
|
+
network,
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
let numberOfInputs = psbt.inputCount
|
|
70
|
+
|
|
71
|
+
for (let i = 0; i < numberOfInputs; i += 1) {
|
|
72
|
+
if (unsignedPsbt.inputsToSign && !unsignedPsbt.inputsToSign.includes(i)) {
|
|
73
|
+
// eslint-disable-next-line no-continue
|
|
74
|
+
continue
|
|
75
|
+
}
|
|
76
|
+
let type = psbt.getInputType(i)
|
|
77
|
+
|
|
78
|
+
if (usingTweakSignerIfNeeded && type === "nonstandard") {
|
|
79
|
+
console.log("using tweak signer")
|
|
80
|
+
let a = tweakSigner(privateKey, this.network)
|
|
81
|
+
await psbt.signInputAsync(i, a, sighashTypes)
|
|
82
|
+
} else {
|
|
83
|
+
await psbt.signInputAsync(i, keyPair, sighashTypes)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// psbt.signAllInputs(keyPair)
|
|
88
|
+
const partialSigendPsbt = psbt.toBase64()
|
|
89
|
+
return partialSigendPsbt
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
finalizePsbts(psbtsBase64: string[] = []) {
|
|
93
|
+
const finals = psbtsBase64.map((psbtBase64) =>
|
|
94
|
+
Psbt.fromBase64(psbtBase64, { network: this.network }),
|
|
95
|
+
)
|
|
96
|
+
const psbt =
|
|
97
|
+
finals.length === 1 ? finals[0] : new Psbt({ network: this.network }).combine(...finals)
|
|
98
|
+
|
|
99
|
+
psbt.finalizeAllInputs()
|
|
100
|
+
|
|
101
|
+
let finalizeTx = psbt.extractTransaction()
|
|
102
|
+
// const rawTx = finalizeTx.toBuffer()
|
|
103
|
+
// const hex = rawTx.toString("hex")
|
|
104
|
+
return finalizeTx.toHex()
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export default BitcoinLikeSignTransaction
|
package/src/teleswap-wallet.ts
CHANGED
|
@@ -1,155 +1,155 @@
|
|
|
1
|
-
import { teleswap } from "@teleportdao/configs"
|
|
2
|
-
import type {
|
|
3
|
-
ExtendedUtxo,
|
|
4
|
-
SignerInfo,
|
|
5
|
-
TargetAddress,
|
|
6
|
-
} from "./transaction-builder/transaction-builder"
|
|
7
|
-
import { BitcoinBaseWallet } from "./bitcoin-wallet-base"
|
|
8
|
-
import { generateWrapOpReturn } from "./helper/teleswap-helper"
|
|
9
|
-
|
|
10
|
-
export type TransferRequest = {
|
|
11
|
-
changeAddress?: string
|
|
12
|
-
lockerAddress: string
|
|
13
|
-
amount: number
|
|
14
|
-
fullAmount?: boolean
|
|
15
|
-
//-----------
|
|
16
|
-
chainId: number
|
|
17
|
-
appId: number
|
|
18
|
-
recipientAddress: string // 20 bytes
|
|
19
|
-
percentageFee: number // 2 bytes in satoshi
|
|
20
|
-
speed?: number | boolean // 1 byte
|
|
21
|
-
isExchange?: boolean
|
|
22
|
-
exchangeTokenAddress?: string // 20 bytes
|
|
23
|
-
outputAmount?: number // 28 bytes
|
|
24
|
-
deadline?: number // 4 bytes
|
|
25
|
-
isFixedToken?: boolean // 1 byte
|
|
26
|
-
feeSpeed?: "normal" | "fast" | "slow"
|
|
27
|
-
staticFeeRate?: number
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export class TeleswapWallet extends BitcoinBaseWallet {
|
|
31
|
-
// payment
|
|
32
|
-
async sendToMultipleAddress(
|
|
33
|
-
receivers: TargetAddress[],
|
|
34
|
-
feeSpeed: "normal" | "fast" | "slow" = "normal",
|
|
35
|
-
) {
|
|
36
|
-
if (!this.signerInfo || !this.privateKey) {
|
|
37
|
-
throw new Error("account not initialized")
|
|
38
|
-
}
|
|
39
|
-
let extendedUtxo = await this.getExtendedUtxo(this.signerInfo)
|
|
40
|
-
let feeRate = await this.transactionBuilder._getFeeRate(feeSpeed)
|
|
41
|
-
let unsignedTx = await this.transactionBuilder.processUnsignedTransaction({
|
|
42
|
-
extendedUtxo,
|
|
43
|
-
targets: receivers,
|
|
44
|
-
changeAddress: this.currentAccount,
|
|
45
|
-
feeRate,
|
|
46
|
-
fullAmount: false,
|
|
47
|
-
})
|
|
48
|
-
let signedPsbt = await this.signer.signPsbt(unsignedTx, this.privateKey)
|
|
49
|
-
let signedTx = this.signer.finalizePsbts([signedPsbt])
|
|
50
|
-
let txId = await this.transactionBuilder.sendTx(signedTx)
|
|
51
|
-
return txId
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// send
|
|
55
|
-
|
|
56
|
-
async wrapUnsigned(
|
|
57
|
-
recipientAddress: string,
|
|
58
|
-
amount: string,
|
|
59
|
-
networkFee: string,
|
|
60
|
-
signer: SignerInfo,
|
|
61
|
-
lockerAddress: string,
|
|
62
|
-
exchange?: {
|
|
63
|
-
outputToken: string
|
|
64
|
-
outputAmount: string
|
|
65
|
-
bridgePercentageFee?: number
|
|
66
|
-
},
|
|
67
|
-
{
|
|
68
|
-
chainId = 137, // 80001
|
|
69
|
-
appId = exchange
|
|
70
|
-
? teleswap.requestAppId.WrapAndSwap.default
|
|
71
|
-
: teleswap.requestAppId.Wrap.default,
|
|
72
|
-
} = {},
|
|
73
|
-
fullAmount = false,
|
|
74
|
-
staticExtendedUtxo?: ExtendedUtxo[],
|
|
75
|
-
staticFeeRate?: number,
|
|
76
|
-
changeAddress?: string,
|
|
77
|
-
) {
|
|
78
|
-
const dataHex = generateWrapOpReturn({
|
|
79
|
-
chainId,
|
|
80
|
-
appId,
|
|
81
|
-
recipientAddress,
|
|
82
|
-
networkFee,
|
|
83
|
-
speed: false,
|
|
84
|
-
isExchange: !!exchange,
|
|
85
|
-
outputAmount: exchange?.outputAmount,
|
|
86
|
-
outputToken: exchange?.outputToken,
|
|
87
|
-
bridgePercentageFee: exchange?.bridgePercentageFee,
|
|
88
|
-
})
|
|
89
|
-
let extendedUtxo = staticExtendedUtxo || (await this.getExtendedUtxo(signer))
|
|
90
|
-
let feeRate = staticFeeRate || (await this.btcInterface.getFeeRate("normal"))
|
|
91
|
-
|
|
92
|
-
const targets = fullAmount
|
|
93
|
-
? [this.transactionBuilder.getOpReturnTarget(dataHex)]
|
|
94
|
-
: [
|
|
95
|
-
{
|
|
96
|
-
address: lockerAddress,
|
|
97
|
-
value: +amount,
|
|
98
|
-
},
|
|
99
|
-
this.transactionBuilder.getOpReturnTarget(dataHex),
|
|
100
|
-
]
|
|
101
|
-
|
|
102
|
-
const unsignedTx = this.transactionBuilder.processUnsignedTransaction({
|
|
103
|
-
extendedUtxo,
|
|
104
|
-
feeRate,
|
|
105
|
-
targets,
|
|
106
|
-
changeAddress: changeAddress || signer.address,
|
|
107
|
-
fullAmount,
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
return unsignedTx
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
async wrap(
|
|
114
|
-
recipientAddress: string,
|
|
115
|
-
amount: string,
|
|
116
|
-
networkFee: string,
|
|
117
|
-
lockerAddress: string,
|
|
118
|
-
exchange?: {
|
|
119
|
-
outputToken: string
|
|
120
|
-
outputAmount: string
|
|
121
|
-
bridgePercentageFee?: number
|
|
122
|
-
},
|
|
123
|
-
{
|
|
124
|
-
chainId = 137, // 80001
|
|
125
|
-
appId = exchange ? 1 : 0,
|
|
126
|
-
} = {},
|
|
127
|
-
fullAmount = false,
|
|
128
|
-
staticExtendedUtxo?: ExtendedUtxo[],
|
|
129
|
-
staticFeeRate?: number,
|
|
130
|
-
changeAddress?: string,
|
|
131
|
-
) {
|
|
132
|
-
if (!this.signerInfo || !this.privateKey) {
|
|
133
|
-
throw new Error("account not initialized")
|
|
134
|
-
}
|
|
135
|
-
let unsignedTransaction = await this.wrapUnsigned(
|
|
136
|
-
recipientAddress,
|
|
137
|
-
amount,
|
|
138
|
-
networkFee,
|
|
139
|
-
this.signerInfo,
|
|
140
|
-
lockerAddress,
|
|
141
|
-
exchange,
|
|
142
|
-
{
|
|
143
|
-
chainId,
|
|
144
|
-
appId,
|
|
145
|
-
},
|
|
146
|
-
fullAmount,
|
|
147
|
-
staticExtendedUtxo,
|
|
148
|
-
staticFeeRate,
|
|
149
|
-
changeAddress,
|
|
150
|
-
)
|
|
151
|
-
let signedPsbt = await this.signer.signPsbt(unsignedTransaction, this.privateKey)
|
|
152
|
-
return this.sendSignedPsbt(signedPsbt)
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
export default TeleswapWallet
|
|
1
|
+
import { teleswap } from "@teleportdao/configs"
|
|
2
|
+
import type {
|
|
3
|
+
ExtendedUtxo,
|
|
4
|
+
SignerInfo,
|
|
5
|
+
TargetAddress,
|
|
6
|
+
} from "./transaction-builder/transaction-builder"
|
|
7
|
+
import { BitcoinBaseWallet } from "./bitcoin-wallet-base"
|
|
8
|
+
import { generateWrapOpReturn } from "./helper/teleswap-helper"
|
|
9
|
+
|
|
10
|
+
export type TransferRequest = {
|
|
11
|
+
changeAddress?: string
|
|
12
|
+
lockerAddress: string
|
|
13
|
+
amount: number
|
|
14
|
+
fullAmount?: boolean
|
|
15
|
+
//-----------
|
|
16
|
+
chainId: number
|
|
17
|
+
appId: number
|
|
18
|
+
recipientAddress: string // 20 bytes
|
|
19
|
+
percentageFee: number // 2 bytes in satoshi
|
|
20
|
+
speed?: number | boolean // 1 byte
|
|
21
|
+
isExchange?: boolean
|
|
22
|
+
exchangeTokenAddress?: string // 20 bytes
|
|
23
|
+
outputAmount?: number // 28 bytes
|
|
24
|
+
deadline?: number // 4 bytes
|
|
25
|
+
isFixedToken?: boolean // 1 byte
|
|
26
|
+
feeSpeed?: "normal" | "fast" | "slow"
|
|
27
|
+
staticFeeRate?: number
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class TeleswapWallet extends BitcoinBaseWallet {
|
|
31
|
+
// payment
|
|
32
|
+
async sendToMultipleAddress(
|
|
33
|
+
receivers: TargetAddress[],
|
|
34
|
+
feeSpeed: "normal" | "fast" | "slow" = "normal",
|
|
35
|
+
) {
|
|
36
|
+
if (!this.signerInfo || !this.privateKey) {
|
|
37
|
+
throw new Error("account not initialized")
|
|
38
|
+
}
|
|
39
|
+
let extendedUtxo = await this.getExtendedUtxo(this.signerInfo)
|
|
40
|
+
let feeRate = await this.transactionBuilder._getFeeRate(feeSpeed)
|
|
41
|
+
let unsignedTx = await this.transactionBuilder.processUnsignedTransaction({
|
|
42
|
+
extendedUtxo,
|
|
43
|
+
targets: receivers,
|
|
44
|
+
changeAddress: this.currentAccount,
|
|
45
|
+
feeRate,
|
|
46
|
+
fullAmount: false,
|
|
47
|
+
})
|
|
48
|
+
let signedPsbt = await this.signer.signPsbt(unsignedTx, this.privateKey)
|
|
49
|
+
let signedTx = this.signer.finalizePsbts([signedPsbt])
|
|
50
|
+
let txId = await this.transactionBuilder.sendTx(signedTx)
|
|
51
|
+
return txId
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// send
|
|
55
|
+
|
|
56
|
+
async wrapUnsigned(
|
|
57
|
+
recipientAddress: string,
|
|
58
|
+
amount: string,
|
|
59
|
+
networkFee: string,
|
|
60
|
+
signer: SignerInfo,
|
|
61
|
+
lockerAddress: string,
|
|
62
|
+
exchange?: {
|
|
63
|
+
outputToken: string
|
|
64
|
+
outputAmount: string
|
|
65
|
+
bridgePercentageFee?: number
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
chainId = 137, // 80001
|
|
69
|
+
appId = exchange
|
|
70
|
+
? teleswap.requestAppId.WrapAndSwap.default
|
|
71
|
+
: teleswap.requestAppId.Wrap.default,
|
|
72
|
+
} = {},
|
|
73
|
+
fullAmount = false,
|
|
74
|
+
staticExtendedUtxo?: ExtendedUtxo[],
|
|
75
|
+
staticFeeRate?: number,
|
|
76
|
+
changeAddress?: string,
|
|
77
|
+
) {
|
|
78
|
+
const dataHex = generateWrapOpReturn({
|
|
79
|
+
chainId,
|
|
80
|
+
appId,
|
|
81
|
+
recipientAddress,
|
|
82
|
+
networkFee,
|
|
83
|
+
speed: false,
|
|
84
|
+
isExchange: !!exchange,
|
|
85
|
+
outputAmount: exchange?.outputAmount,
|
|
86
|
+
outputToken: exchange?.outputToken,
|
|
87
|
+
bridgePercentageFee: exchange?.bridgePercentageFee,
|
|
88
|
+
})
|
|
89
|
+
let extendedUtxo = staticExtendedUtxo || (await this.getExtendedUtxo(signer))
|
|
90
|
+
let feeRate = staticFeeRate || (await this.btcInterface.getFeeRate("normal"))
|
|
91
|
+
|
|
92
|
+
const targets = fullAmount
|
|
93
|
+
? [this.transactionBuilder.getOpReturnTarget(dataHex)]
|
|
94
|
+
: [
|
|
95
|
+
{
|
|
96
|
+
address: lockerAddress,
|
|
97
|
+
value: +amount,
|
|
98
|
+
},
|
|
99
|
+
this.transactionBuilder.getOpReturnTarget(dataHex),
|
|
100
|
+
]
|
|
101
|
+
|
|
102
|
+
const unsignedTx = this.transactionBuilder.processUnsignedTransaction({
|
|
103
|
+
extendedUtxo,
|
|
104
|
+
feeRate,
|
|
105
|
+
targets,
|
|
106
|
+
changeAddress: changeAddress || signer.address,
|
|
107
|
+
fullAmount,
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
return unsignedTx
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async wrap(
|
|
114
|
+
recipientAddress: string,
|
|
115
|
+
amount: string,
|
|
116
|
+
networkFee: string,
|
|
117
|
+
lockerAddress: string,
|
|
118
|
+
exchange?: {
|
|
119
|
+
outputToken: string
|
|
120
|
+
outputAmount: string
|
|
121
|
+
bridgePercentageFee?: number
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
chainId = 137, // 80001
|
|
125
|
+
appId = exchange ? 1 : 0,
|
|
126
|
+
} = {},
|
|
127
|
+
fullAmount = false,
|
|
128
|
+
staticExtendedUtxo?: ExtendedUtxo[],
|
|
129
|
+
staticFeeRate?: number,
|
|
130
|
+
changeAddress?: string,
|
|
131
|
+
) {
|
|
132
|
+
if (!this.signerInfo || !this.privateKey) {
|
|
133
|
+
throw new Error("account not initialized")
|
|
134
|
+
}
|
|
135
|
+
let unsignedTransaction = await this.wrapUnsigned(
|
|
136
|
+
recipientAddress,
|
|
137
|
+
amount,
|
|
138
|
+
networkFee,
|
|
139
|
+
this.signerInfo,
|
|
140
|
+
lockerAddress,
|
|
141
|
+
exchange,
|
|
142
|
+
{
|
|
143
|
+
chainId,
|
|
144
|
+
appId,
|
|
145
|
+
},
|
|
146
|
+
fullAmount,
|
|
147
|
+
staticExtendedUtxo,
|
|
148
|
+
staticFeeRate,
|
|
149
|
+
changeAddress,
|
|
150
|
+
)
|
|
151
|
+
let signedPsbt = await this.signer.signPsbt(unsignedTransaction, this.privateKey)
|
|
152
|
+
return this.sendSignedPsbt(signedPsbt)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
export default TeleswapWallet
|
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
import * as bitcoin from "bitcoinjs-lib"
|
|
2
|
-
import { BaseTransactionBuilder } from "./transaction-builder"
|
|
3
|
-
import { BitcoinInterface } from "../bitcoin-interface"
|
|
4
|
-
import { BitcoinConnectionInfo } from "../type"
|
|
5
|
-
|
|
6
|
-
export class BitcoinTransactionBuilder extends BaseTransactionBuilder {
|
|
7
|
-
btcInterface: BitcoinInterface
|
|
8
|
-
constructor(
|
|
9
|
-
connectionInfo: BitcoinConnectionInfo,
|
|
10
|
-
networkName: string,
|
|
11
|
-
network = bitcoin.networks.bitcoin,
|
|
12
|
-
) {
|
|
13
|
-
super({
|
|
14
|
-
network,
|
|
15
|
-
testnet: networkName?.includes("_testnet"),
|
|
16
|
-
dustLimit: 1000,
|
|
17
|
-
})
|
|
18
|
-
this.btcInterface = new BitcoinInterface(connectionInfo, networkName)
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
async _getUtxo(userAddress: string) {
|
|
22
|
-
let utxos = await this.btcInterface.getUtxo(userAddress)
|
|
23
|
-
return utxos.map((tx) => ({
|
|
24
|
-
hash: tx.txId as string,
|
|
25
|
-
value: +tx.value,
|
|
26
|
-
index: +tx.index,
|
|
27
|
-
}))
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
async _getFeeRate(speed: "normal" | "slow" | "fast" = "normal") {
|
|
31
|
-
return this.btcInterface.getFeeRate(speed)
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
async _getTransactionHex(transactionId: string): Promise<string> {
|
|
35
|
-
return this.btcInterface.getRawTransaction(transactionId)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
async sendTx(txHex: string): Promise<string> {
|
|
39
|
-
let txId = await (
|
|
40
|
-
this.btcInterface.rpcProvider || this.btcInterface.apiProvider
|
|
41
|
-
).sendRawTransaction(txHex)
|
|
42
|
-
return txId
|
|
43
|
-
}
|
|
44
|
-
}
|
|
1
|
+
import * as bitcoin from "bitcoinjs-lib"
|
|
2
|
+
import { BaseTransactionBuilder } from "./transaction-builder"
|
|
3
|
+
import { BitcoinInterface } from "../bitcoin-interface"
|
|
4
|
+
import { BitcoinConnectionInfo } from "../type"
|
|
5
|
+
|
|
6
|
+
export class BitcoinTransactionBuilder extends BaseTransactionBuilder {
|
|
7
|
+
btcInterface: BitcoinInterface
|
|
8
|
+
constructor(
|
|
9
|
+
connectionInfo: BitcoinConnectionInfo,
|
|
10
|
+
networkName: string,
|
|
11
|
+
network = bitcoin.networks.bitcoin,
|
|
12
|
+
) {
|
|
13
|
+
super({
|
|
14
|
+
network,
|
|
15
|
+
testnet: networkName?.includes("_testnet"),
|
|
16
|
+
dustLimit: 1000,
|
|
17
|
+
})
|
|
18
|
+
this.btcInterface = new BitcoinInterface(connectionInfo, networkName)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async _getUtxo(userAddress: string) {
|
|
22
|
+
let utxos = await this.btcInterface.getUtxo(userAddress)
|
|
23
|
+
return utxos.map((tx) => ({
|
|
24
|
+
hash: tx.txId as string,
|
|
25
|
+
value: +tx.value,
|
|
26
|
+
index: +tx.index,
|
|
27
|
+
}))
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async _getFeeRate(speed: "normal" | "slow" | "fast" = "normal") {
|
|
31
|
+
return this.btcInterface.getFeeRate(speed)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async _getTransactionHex(transactionId: string): Promise<string> {
|
|
35
|
+
return this.btcInterface.getRawTransaction(transactionId)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async sendTx(txHex: string): Promise<string> {
|
|
39
|
+
let txId = await (
|
|
40
|
+
this.btcInterface.rpcProvider || this.btcInterface.apiProvider
|
|
41
|
+
).sendRawTransaction(txHex)
|
|
42
|
+
return txId
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from "./bitcoin-transaction-builder"
|
|
2
|
-
export * from "./ordinal-transaction-builder"
|
|
3
|
-
export * from "./transaction-builder"
|
|
1
|
+
export * from "./bitcoin-transaction-builder"
|
|
2
|
+
export * from "./ordinal-transaction-builder"
|
|
3
|
+
export * from "./transaction-builder"
|