@bitpay-labs/crypto-wallet-core 11.10.9 → 11.10.12
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 +6 -6
- package/src/transactions/bch/index.ts +12 -12
- package/src/transactions/btc/index.ts +140 -51
- package/src/transactions/doge/index.ts +12 -12
- package/src/transactions/eth/index.ts +9 -0
- package/src/transactions/ltc/index.ts +12 -12
- package/src/transactions/sol/index.ts +14 -0
- package/src/transactions/xrp/index.ts +9 -0
- package/src/utils/index.ts +9 -0
- package/test/transactions.test.ts +215 -2
- package/test/utils.test.ts +39 -0
- package/ts_build/src/transactions/bch/index.d.ts +11 -8
- package/ts_build/src/transactions/bch/index.d.ts.map +1 -1
- package/ts_build/src/transactions/bch/index.js +5 -11
- package/ts_build/src/transactions/bch/index.js.map +1 -1
- package/ts_build/src/transactions/btc/index.d.ts +66 -47
- package/ts_build/src/transactions/btc/index.d.ts.map +1 -1
- package/ts_build/src/transactions/btc/index.js +46 -34
- package/ts_build/src/transactions/btc/index.js.map +1 -1
- package/ts_build/src/transactions/doge/index.d.ts +11 -8
- package/ts_build/src/transactions/doge/index.d.ts.map +1 -1
- package/ts_build/src/transactions/doge/index.js +5 -11
- package/ts_build/src/transactions/doge/index.js.map +1 -1
- package/ts_build/src/transactions/eth/index.d.ts +3 -0
- package/ts_build/src/transactions/eth/index.d.ts.map +1 -1
- package/ts_build/src/transactions/eth/index.js +4 -0
- package/ts_build/src/transactions/eth/index.js.map +1 -1
- package/ts_build/src/transactions/ltc/index.d.ts +11 -8
- package/ts_build/src/transactions/ltc/index.d.ts.map +1 -1
- package/ts_build/src/transactions/ltc/index.js +5 -11
- package/ts_build/src/transactions/ltc/index.js.map +1 -1
- package/ts_build/src/transactions/sol/index.d.ts +3 -0
- package/ts_build/src/transactions/sol/index.d.ts.map +1 -1
- package/ts_build/src/transactions/sol/index.js +5 -0
- package/ts_build/src/transactions/sol/index.js.map +1 -1
- package/ts_build/src/transactions/xrp/index.d.ts +4 -1
- package/ts_build/src/transactions/xrp/index.d.ts.map +1 -1
- package/ts_build/src/transactions/xrp/index.js +4 -0
- package/ts_build/src/transactions/xrp/index.js.map +1 -1
- package/ts_build/src/utils/index.d.ts +1 -0
- package/ts_build/src/utils/index.d.ts.map +1 -1
- package/ts_build/src/utils/index.js +11 -0
- package/ts_build/src/utils/index.js.map +1 -1
- package/ts_build/test/transactions.test.js +198 -0
- package/ts_build/test/transactions.test.js.map +1 -1
- package/ts_build/test/utils.test.js +33 -0
- package/ts_build/test/utils.test.js.map +1 -1
|
@@ -6,7 +6,13 @@ import bitcoreLibDoge from '@bitpay-labs/bitcore-lib-doge';
|
|
|
6
6
|
import bitcoreLibLtc from '@bitpay-labs/bitcore-lib-ltc';
|
|
7
7
|
import { Constants, Transactions } from '../src';
|
|
8
8
|
|
|
9
|
-
describe('Transaction', function() {
|
|
9
|
+
describe('Transaction', function () {
|
|
10
|
+
const libs = {
|
|
11
|
+
BTC: bitcoreLib,
|
|
12
|
+
BCH: bitcoreLibCash,
|
|
13
|
+
DOGE: bitcoreLibDoge,
|
|
14
|
+
LTC: bitcoreLibLtc
|
|
15
|
+
};
|
|
10
16
|
describe('create', () => {
|
|
11
17
|
it('should create a BTC tx', () => {
|
|
12
18
|
const recipients = [{ address: 'mpNpzMoprLnSBu8CWDunNCYeJq3Mzdk59V', amount: 1e8 }];
|
|
@@ -45,6 +51,171 @@ describe('Transaction', function() {
|
|
|
45
51
|
expect(signed).to.eq(expected);
|
|
46
52
|
});
|
|
47
53
|
|
|
54
|
+
describe('every utxo type: bitcore-node, UnspentOutput, and UnspentOutput.toObject', () => {
|
|
55
|
+
const keys = [{
|
|
56
|
+
address: 'mnfnJx2xWWptYmBzck3rdE851Dtu9GaZ3F',
|
|
57
|
+
privKey: 'cSFjiifSbZ2hU4jTFwE993LCe2rkZGULCTGWTDWXzHvuXRKxpnc1'
|
|
58
|
+
}];
|
|
59
|
+
|
|
60
|
+
const bitcoreNodeUtxos =
|
|
61
|
+
[
|
|
62
|
+
{
|
|
63
|
+
mintTxid: '643ec66d6c4cad4cbdb8ed2166b8078975e0af9bb7ff7e30d394f43b0d9f18ab',
|
|
64
|
+
mintIndex: 1,
|
|
65
|
+
mintHeight: 100,
|
|
66
|
+
value: 90_000,
|
|
67
|
+
script: '76a9144e744a19a009a9dd43a23a7c12045c83e82ac9d288ac',
|
|
68
|
+
address: 'mnfnJx2xWWptYmBzck3rdE851Dtu9GaZ3F',
|
|
69
|
+
sequenceNumber: 4294967294
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
mintTxid: '643ec66d6c4cad4cbdb8ed2166b8078975e0af9bb7ff7e30d394f43b0d9f18ab',
|
|
73
|
+
mintIndex: 0,
|
|
74
|
+
mintHeight: 100,
|
|
75
|
+
value: 30_000,
|
|
76
|
+
script: '76a9144e744a19a009a9dd43a23a7c12045c83e82ac9d288ac',
|
|
77
|
+
address: 'mnfnJx2xWWptYmBzck3rdE851Dtu9GaZ3F',
|
|
78
|
+
sequenceNumber: 4294967294
|
|
79
|
+
}
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
const unspentOutputToObjectUtxos = [
|
|
83
|
+
{
|
|
84
|
+
txid: '643ec66d6c4cad4cbdb8ed2166b8078975e0af9bb7ff7e30d394f43b0d9f18ab',
|
|
85
|
+
outputIndex: 1,
|
|
86
|
+
amount: 0.0009,
|
|
87
|
+
scriptPubKey: '76a9144e744a19a009a9dd43a23a7c12045c83e82ac9d288ac'
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
txid: '643ec66d6c4cad4cbdb8ed2166b8078975e0af9bb7ff7e30d394f43b0d9f18ab',
|
|
91
|
+
vout: 0,
|
|
92
|
+
amount: 0.0003,
|
|
93
|
+
scriptPubKey: '76a9144e744a19a009a9dd43a23a7c12045c83e82ac9d288ac'
|
|
94
|
+
}
|
|
95
|
+
];
|
|
96
|
+
|
|
97
|
+
const recipients = [{ address: 'moVnNJpHHfssYJEnMTS5xXyGV8RhRQNRz5', amount: 100_000 }];
|
|
98
|
+
for (const chain of ['BTC', 'BCH', 'DOGE', 'LTC']) {
|
|
99
|
+
const unspentOutputUtxos = [
|
|
100
|
+
new libs[chain].Transaction.UnspentOutput({
|
|
101
|
+
txId: '643ec66d6c4cad4cbdb8ed2166b8078975e0af9bb7ff7e30d394f43b0d9f18ab',
|
|
102
|
+
outputIndex: 1,
|
|
103
|
+
satoshis: 90_000,
|
|
104
|
+
script: '76a9144e744a19a009a9dd43a23a7c12045c83e82ac9d288ac'
|
|
105
|
+
}),
|
|
106
|
+
new libs[chain].Transaction.UnspentOutput({
|
|
107
|
+
txId: '643ec66d6c4cad4cbdb8ed2166b8078975e0af9bb7ff7e30d394f43b0d9f18ab',
|
|
108
|
+
outputIndex: 0,
|
|
109
|
+
satoshis: 30_000,
|
|
110
|
+
script: '76a9144e744a19a009a9dd43a23a7c12045c83e82ac9d288ac'
|
|
111
|
+
})
|
|
112
|
+
];
|
|
113
|
+
const utxoSet = [
|
|
114
|
+
bitcoreNodeUtxos,
|
|
115
|
+
unspentOutputUtxos,
|
|
116
|
+
unspentOutputToObjectUtxos
|
|
117
|
+
];
|
|
118
|
+
let tx: string;
|
|
119
|
+
const lib = libs[chain];
|
|
120
|
+
it(`should create a tx with every utxo type for ${chain}`, () => {
|
|
121
|
+
const txs = utxoSet.map(utxos => Transactions.create({
|
|
122
|
+
chain,
|
|
123
|
+
recipients,
|
|
124
|
+
utxos
|
|
125
|
+
}));
|
|
126
|
+
|
|
127
|
+
tx = txs[0];
|
|
128
|
+
for (const _tx of txs.slice(1)) {
|
|
129
|
+
expect(tx, 'all transactions should be the same regardless of the utxo format').to.equal(_tx);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
let expectedTx: string;
|
|
133
|
+
if (chain === 'DOGE') {
|
|
134
|
+
expectedTx = '0100000002ab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e640100000000ffffffffab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e640000000000ffffffff01a0860100000000001976a91457884dcfe2ab46d3354a42d97333c95e5b80cf0188ac00000000';
|
|
135
|
+
} else {
|
|
136
|
+
expectedTx = '0200000002ab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e640100000000ffffffffab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e640000000000ffffffff01a0860100000000001976a91457884dcfe2ab46d3354a42d97333c95e5b80cf0188ac00000000';
|
|
137
|
+
}
|
|
138
|
+
expect(tx).to.equal(expectedTx);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it(`should sign a tx with every utxo type ${chain}`, () => {
|
|
142
|
+
const signedTxs = utxoSet.map(utxos => Transactions.sign({
|
|
143
|
+
chain,
|
|
144
|
+
tx,
|
|
145
|
+
utxos,
|
|
146
|
+
keys,
|
|
147
|
+
sigtype: lib.crypto.Signature.SIGHASH_ALL
|
|
148
|
+
}));
|
|
149
|
+
const signedTx = signedTxs[0];
|
|
150
|
+
for (const _signedTx of signedTxs.slice(1)) {
|
|
151
|
+
expect(signedTx, 'signed transactions should all be the same regardless of utxo type').to.equal(_signedTx);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
let expectedTx: string;
|
|
155
|
+
if (chain === 'DOGE') {
|
|
156
|
+
expectedTx = '0100000002ab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e64010000006a473044022015ceee1da23792e26cd1215c8327a35978c8b0494de9e69bab6d31ab3d0b56ad02205f9299162591672c6cde3d92d5819a11e1ad9f353a25d7a3ce494877ef27468901210321f2f13aed42db7257b64f77d574071a6e81e460ab3693eefb7482c12d1ff697ffffffffab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e64000000006b4830450221009f0b5dd0b4c0bd9bcf804e71020705eb9bda99a31cb2a4d123c364bdd394adeb022016939a51fb03c9efea4b7d53a7b90d7a3667fff71cd8365e366c81b1fdd0d3e001210321f2f13aed42db7257b64f77d574071a6e81e460ab3693eefb7482c12d1ff697ffffffff01a0860100000000001976a91457884dcfe2ab46d3354a42d97333c95e5b80cf0188ac00000000';
|
|
157
|
+
} else {
|
|
158
|
+
expectedTx = '0200000002ab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e64010000006b483045022100c74ab4fe359e8efc73fc28dce989e28da47d3b45eb06ee305dfc0e675da8700802201076a067337a3c6b90fad2077a55154aa150460cb842bc86373026eb071a7ac901210321f2f13aed42db7257b64f77d574071a6e81e460ab3693eefb7482c12d1ff697ffffffffab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e64000000006b483045022100ed22907cc96b5367ef469225f7f718efa8fb4eb2dc5fbf3909777c5f024cf5e902206480082ad9b413a8a76128ab147287da30d907cfb1a159be814392fa1138012801210321f2f13aed42db7257b64f77d574071a6e81e460ab3693eefb7482c12d1ff697ffffffff01a0860100000000001976a91457884dcfe2ab46d3354a42d97333c95e5b80cf0188ac00000000';
|
|
159
|
+
}
|
|
160
|
+
expect(signedTx).to.equal(expectedTx);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it(`should create valid sighashes for all utxo types ${chain}`, () => {
|
|
164
|
+
const bitcoreTx = lib.Transaction()
|
|
165
|
+
.from(unspentOutputUtxos)
|
|
166
|
+
.to(recipients[0].address, recipients[0].amount);
|
|
167
|
+
const signedTx = lib.Transaction()
|
|
168
|
+
.from(unspentOutputUtxos)
|
|
169
|
+
.to(recipients[0].address, recipients[0].amount);
|
|
170
|
+
|
|
171
|
+
for (let index = 0; index < unspentOutputUtxos.length; index++) {
|
|
172
|
+
const sighashes: string[] = [];
|
|
173
|
+
for (const utxos of utxoSet) {
|
|
174
|
+
sighashes.push(Transactions.getSighash({
|
|
175
|
+
chain,
|
|
176
|
+
tx,
|
|
177
|
+
utxos,
|
|
178
|
+
index,
|
|
179
|
+
sigtype: lib.crypto.Signature.SIGHASH_ALL
|
|
180
|
+
}));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// getSighash should work without utxos if a complete lib transaction is provided
|
|
184
|
+
sighashes.push(Transactions.getSighash({
|
|
185
|
+
chain,
|
|
186
|
+
tx: bitcoreTx,
|
|
187
|
+
index,
|
|
188
|
+
sigtype: lib.crypto.Signature.SIGHASH_ALL
|
|
189
|
+
}));
|
|
190
|
+
|
|
191
|
+
const sighash = sighashes[0];
|
|
192
|
+
for (const hash of sighashes) {
|
|
193
|
+
expect(sighash).to.equal(hash);
|
|
194
|
+
}
|
|
195
|
+
expect(sighash.length).to.equal(64);
|
|
196
|
+
const privateKey = new lib.PrivateKey(keys[0].privKey);
|
|
197
|
+
const publicKey = privateKey.toPublicKey();
|
|
198
|
+
const signature = lib.crypto.ECDSA.sign(Buffer.from(sighash, 'hex'), privateKey);
|
|
199
|
+
signature.pubKey = publicKey;
|
|
200
|
+
|
|
201
|
+
Transactions.applySignature({
|
|
202
|
+
chain,
|
|
203
|
+
tx: signedTx,
|
|
204
|
+
signature,
|
|
205
|
+
index
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
if (chain === 'DOGE') {
|
|
209
|
+
const serializedSignedTx = '0100000002ab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e64010000006a473044022015ceee1da23792e26cd1215c8327a35978c8b0494de9e69bab6d31ab3d0b56ad02205f9299162591672c6cde3d92d5819a11e1ad9f353a25d7a3ce494877ef27468901210321f2f13aed42db7257b64f77d574071a6e81e460ab3693eefb7482c12d1ff697ffffffffab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e64000000006b4830450221009f0b5dd0b4c0bd9bcf804e71020705eb9bda99a31cb2a4d123c364bdd394adeb022016939a51fb03c9efea4b7d53a7b90d7a3667fff71cd8365e366c81b1fdd0d3e001210321f2f13aed42db7257b64f77d574071a6e81e460ab3693eefb7482c12d1ff697ffffffff01a0860100000000001976a91457884dcfe2ab46d3354a42d97333c95e5b80cf0188ac00000000';
|
|
210
|
+
expect(signedTx.serialize({ disableSmallFees: true, disableDustOutputs: true })).to.equal(serializedSignedTx);
|
|
211
|
+
} else {
|
|
212
|
+
const serializedSignedTx = '0200000002ab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e64010000006b483045022100c74ab4fe359e8efc73fc28dce989e28da47d3b45eb06ee305dfc0e675da8700802201076a067337a3c6b90fad2077a55154aa150460cb842bc86373026eb071a7ac901210321f2f13aed42db7257b64f77d574071a6e81e460ab3693eefb7482c12d1ff697ffffffffab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e64000000006b483045022100ed22907cc96b5367ef469225f7f718efa8fb4eb2dc5fbf3909777c5f024cf5e902206480082ad9b413a8a76128ab147287da30d907cfb1a159be814392fa1138012801210321f2f13aed42db7257b64f77d574071a6e81e460ab3693eefb7482c12d1ff697ffffffff01a0860100000000001976a91457884dcfe2ab46d3354a42d97333c95e5b80cf0188ac00000000';
|
|
213
|
+
expect(signedTx.serialize()).to.equal(serializedSignedTx);
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
|
|
48
219
|
it('should sign a BTC opreturn tx', () => {
|
|
49
220
|
const tx =
|
|
50
221
|
'0200000001ab189f0d3bf494d3307effb79bafe0758907b86621edb8bd4cad4c6c6dc63e640100000000ffffffff0200000000000000000b6a096a07696f6e3a61626340420f00000000001976a91457884dcfe2ab46d3354a42d97333c95e5b80cf0188ac00000000';
|
|
@@ -873,6 +1044,48 @@ describe('Transaction', function() {
|
|
|
873
1044
|
|
|
874
1045
|
});
|
|
875
1046
|
|
|
1047
|
+
describe('getMutableFields', () => {
|
|
1048
|
+
it('recovers the SOL recent blockhash only (not blockHeight)', () => {
|
|
1049
|
+
const blockHash = 'GtV1Hb3FvP3HURHAsj8mGwEqCumvP3pv3i6CVCzYNj3d';
|
|
1050
|
+
const raw = Transactions.create({
|
|
1051
|
+
chain: 'SOL',
|
|
1052
|
+
category: 'transfer',
|
|
1053
|
+
from: '8WyoNvKsmfdG6zrbzNBVN8DETyLra3ond61saU9C52YR',
|
|
1054
|
+
recipients: [{ address: 'F7FknkRckx4yvA3Gexnx1H3nwPxndMxVt58BwAzEQhcY', amount: 3896000000000000 }],
|
|
1055
|
+
blockHash,
|
|
1056
|
+
blockHeight: 531575
|
|
1057
|
+
});
|
|
1058
|
+
const fields = (Transactions.get({ chain: 'SOL' }) as any).getMutableFields(raw);
|
|
1059
|
+
expect(fields).to.deep.equal({ blockHash });
|
|
1060
|
+
});
|
|
1061
|
+
|
|
1062
|
+
it('recovers the EVM nonce', () => {
|
|
1063
|
+
const raw = Transactions.create({
|
|
1064
|
+
chain: 'ETH',
|
|
1065
|
+
network: 'livenet',
|
|
1066
|
+
recipients: [{ address: '0x37d7B3bBD88EFdE6a93cF74D2F5b0385D3E3B08A', amount: 1000 }],
|
|
1067
|
+
nonce: 7,
|
|
1068
|
+
gasLimit: 21000,
|
|
1069
|
+
gasPrice: 20000000000,
|
|
1070
|
+
data: '0x'
|
|
1071
|
+
});
|
|
1072
|
+
const fields = (Transactions.get({ chain: 'ETH' }) as any).getMutableFields(raw);
|
|
1073
|
+
expect(fields).to.deep.equal({ nonce: 7 });
|
|
1074
|
+
});
|
|
1075
|
+
|
|
1076
|
+
it('recovers the XRP sequence (nonce)', () => {
|
|
1077
|
+
const raw = Transactions.create({
|
|
1078
|
+
chain: 'XRP',
|
|
1079
|
+
recipients: [{ address: 'rEqj9WKSH7wEkPvWf6b4gCi26Y3F7HbKUF', amount: '123456' }],
|
|
1080
|
+
from: 'rEqj9WKSH7wEkPvWf6b4gCi26Y3F7HbKUF',
|
|
1081
|
+
fee: 12,
|
|
1082
|
+
nonce: 4
|
|
1083
|
+
});
|
|
1084
|
+
const fields = (Transactions.get({ chain: 'XRP' }) as any).getMutableFields(raw);
|
|
1085
|
+
expect(fields).to.deep.equal({ nonce: 4 });
|
|
1086
|
+
});
|
|
1087
|
+
});
|
|
1088
|
+
|
|
876
1089
|
describe('sign', () => {
|
|
877
1090
|
it('should sign an ETH tx', () => {
|
|
878
1091
|
const signedTx = Transactions.sign({
|
|
@@ -1887,4 +2100,4 @@ describe('Transaction', function() {
|
|
|
1887
2100
|
});
|
|
1888
2101
|
|
|
1889
2102
|
});
|
|
1890
|
-
});
|
|
2103
|
+
});
|
package/test/utils.test.ts
CHANGED
|
@@ -404,4 +404,43 @@ describe('Utils', function() {
|
|
|
404
404
|
expect(() => utils.normalizeXrpFlag('tfPartialPayment')).to.throw(Error).with.property('message', 'Invalid XRP flag: tfPartialPayment. Flag is not in enum AccountSetTfFlags');
|
|
405
405
|
});
|
|
406
406
|
});
|
|
407
|
+
|
|
408
|
+
describe('tryParse', function() {
|
|
409
|
+
it('should parse valid JSON', function() {
|
|
410
|
+
const json = '{"key":"value"}';
|
|
411
|
+
const result = utils.tryParse(json);
|
|
412
|
+
expect(result).to.deep.equal({ key: 'value' });
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
it('should parse JSON primitives and arrays', function() {
|
|
416
|
+
expect(utils.tryParse('0')).to.equal(0);
|
|
417
|
+
expect(utils.tryParse('false')).to.equal(false);
|
|
418
|
+
expect(utils.tryParse('null', 'fallback')).to.equal(null);
|
|
419
|
+
expect(utils.tryParse('[1,2]')).to.deep.equal([1, 2]);
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
it('should return fallback for invalid JSON', function() {
|
|
423
|
+
const json = '{"key": "value"';
|
|
424
|
+
const fallback = { fallback: true };
|
|
425
|
+
const result = utils.tryParse(json, fallback);
|
|
426
|
+
expect(result).to.deep.equal(fallback);
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
it('should return undefined for invalid JSON without a fallback', function() {
|
|
430
|
+
expect(utils.tryParse('{invalid')).to.equal(undefined);
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
it('should return the object itself if already parsed', function() {
|
|
434
|
+
const obj = { key: 'value' };
|
|
435
|
+
const result = utils.tryParse(obj);
|
|
436
|
+
expect(result).to.equal(obj);
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
it('should return non-string inputs unchanged even when a fallback is provided', function() {
|
|
440
|
+
expect(utils.tryParse(0, 'fallback')).to.equal(0);
|
|
441
|
+
expect(utils.tryParse(false, 'fallback')).to.equal(false);
|
|
442
|
+
expect(utils.tryParse(null, 'fallback')).to.equal(null);
|
|
443
|
+
expect(utils.tryParse(undefined, 'fallback')).to.equal(undefined);
|
|
444
|
+
});
|
|
445
|
+
});
|
|
407
446
|
});
|
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import { BTCTxProvider } from '../btc';
|
|
1
|
+
import { BTCTxProvider, EveryUtxoType } from '../btc';
|
|
2
2
|
export declare class BCHTxProvider extends BTCTxProvider {
|
|
3
3
|
lib: any;
|
|
4
|
-
create(
|
|
5
|
-
recipients:
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
create(params: {
|
|
5
|
+
recipients: Array<{
|
|
6
|
+
address: string;
|
|
7
|
+
amount: number | string;
|
|
8
|
+
}>;
|
|
9
|
+
utxos?: EveryUtxoType[];
|
|
10
|
+
change?: string;
|
|
11
|
+
fee?: number | string;
|
|
12
|
+
isSweep?: boolean;
|
|
13
|
+
}): string;
|
|
11
14
|
}
|
|
12
15
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/transactions/bch/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/transactions/bch/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAEtD,qBAAa,aAAc,SAAQ,aAAa;IAC9C,GAAG,MAAkB;IACrB,MAAM,CAAC,MAAM,EAAE;QACb,UAAU,EAAE,KAAK,CAAC;YAAE,OAAO,EAAE,MAAM,CAAC;YAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAA;SAAE,CAAC,CAAC;QAChE,KAAK,CAAC,EAAE,aAAa,EAAE,CAAC;QACxB,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,GAAG,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;QACtB,OAAO,CAAC,EAAE,OAAO,CAAC;KACnB,GAAG,MAAM;CAaX"}
|
|
@@ -8,22 +8,16 @@ const bitcore_lib_cash_1 = __importDefault(require("@bitpay-labs/bitcore-lib-cas
|
|
|
8
8
|
const btc_1 = require("../btc");
|
|
9
9
|
class BCHTxProvider extends btc_1.BTCTxProvider {
|
|
10
10
|
lib = bitcore_lib_cash_1.default;
|
|
11
|
-
create(
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
amount: utxo.value / 1e8,
|
|
16
|
-
txid: utxo.mintTxid,
|
|
17
|
-
outputIndex: utxo.mintIndex
|
|
18
|
-
});
|
|
19
|
-
return new this.lib.Transaction.UnspentOutput(btcUtxo);
|
|
20
|
-
});
|
|
11
|
+
create(params) {
|
|
12
|
+
const { recipients, utxos = [], change, fee = 20000, isSweep } = params;
|
|
13
|
+
const filteredUtxos = isSweep ? utxos : this.selectCoins(recipients, utxos, Number(fee));
|
|
14
|
+
const btcUtxos = filteredUtxos.map(utxo => this.standardizeUtxo(utxo));
|
|
21
15
|
const tx = new this.lib.Transaction().from(btcUtxos).feePerByte(Number(fee) + 2);
|
|
22
16
|
if (change) {
|
|
23
17
|
tx.change(change);
|
|
24
18
|
}
|
|
25
19
|
for (const recipient of recipients) {
|
|
26
|
-
tx.to(recipient.address,
|
|
20
|
+
tx.to(recipient.address, Number(recipient.amount));
|
|
27
21
|
}
|
|
28
22
|
return tx.uncheckedSerialize();
|
|
29
23
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/transactions/bch/index.ts"],"names":[],"mappings":";;;;;;AAAA,qFAA2D;AAC3D,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/transactions/bch/index.ts"],"names":[],"mappings":";;;;;;AAAA,qFAA2D;AAC3D,gCAAsD;AAEtD,MAAa,aAAc,SAAQ,mBAAa;IAC9C,GAAG,GAAG,0BAAc,CAAC;IACrB,MAAM,CAAC,MAMN;QACC,MAAM,EAAE,UAAU,EAAE,KAAK,GAAG,EAAE,EAAE,MAAM,EAAE,GAAG,GAAG,KAAK,EAAE,OAAO,EAAE,GAAG,MAAM,CAAC;QACxE,MAAM,aAAa,GAAG,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,UAAU,EAAE,KAAK,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;QACzF,MAAM,QAAQ,GAAG,aAAa,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC;QACvE,MAAM,EAAE,GAAG,IAAI,IAAI,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;QACjF,IAAI,MAAM,EAAE,CAAC;YACX,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACpB,CAAC;QACD,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,CAAC;YACnC,EAAE,CAAC,EAAE,CAAC,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC;QACrD,CAAC;QACD,OAAO,EAAE,CAAC,kBAAkB,EAAE,CAAC;IACjC,CAAC;CACF;AArBD,sCAqBC"}
|
|
@@ -9,68 +9,64 @@ interface TssSig {
|
|
|
9
9
|
export declare class BTCTxProvider {
|
|
10
10
|
lib: any;
|
|
11
11
|
selectCoins(recipients: Array<{
|
|
12
|
-
amount: number;
|
|
13
|
-
}>, utxos:
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
change: any;
|
|
30
|
-
feeRate: any;
|
|
31
|
-
fee: any;
|
|
32
|
-
isSweep: any;
|
|
33
|
-
replaceByFee: any;
|
|
34
|
-
lockUntilDate: any;
|
|
35
|
-
lockUntilBlock: any;
|
|
36
|
-
}): any;
|
|
12
|
+
amount: number | string;
|
|
13
|
+
}>, utxos: EveryUtxoType[], fee?: number): EveryUtxoType[];
|
|
14
|
+
standardizeUtxo(utxo: EveryUtxoType): UtxoType;
|
|
15
|
+
create(params: {
|
|
16
|
+
recipients: Array<{
|
|
17
|
+
address: string;
|
|
18
|
+
amount: number | string;
|
|
19
|
+
}>;
|
|
20
|
+
utxos?: EveryUtxoType[];
|
|
21
|
+
change?: string;
|
|
22
|
+
feeRate?: number | string;
|
|
23
|
+
fee?: number | string;
|
|
24
|
+
isSweep?: boolean;
|
|
25
|
+
replaceByFee?: boolean;
|
|
26
|
+
lockUntilDate?: number;
|
|
27
|
+
lockUntilBlock?: number;
|
|
28
|
+
}): string;
|
|
37
29
|
getSignature(): void;
|
|
38
30
|
transformSignatureObject(params: {
|
|
39
31
|
obj: any;
|
|
40
32
|
sigtype?: number;
|
|
41
|
-
}):
|
|
33
|
+
}): string;
|
|
42
34
|
applySignature(params: {
|
|
43
35
|
tx: BitcoreLib.Transaction;
|
|
44
36
|
signature: SignatureType;
|
|
45
37
|
index: number;
|
|
46
38
|
sigtype?: number;
|
|
47
|
-
}):
|
|
39
|
+
}): BitcoreLib.Transaction;
|
|
48
40
|
getHash(params: {
|
|
49
|
-
tx:
|
|
50
|
-
}):
|
|
41
|
+
tx: TransactionType;
|
|
42
|
+
}): string;
|
|
51
43
|
sign(params: {
|
|
52
|
-
tx:
|
|
53
|
-
keys:
|
|
54
|
-
utxos:
|
|
44
|
+
tx: TransactionType;
|
|
45
|
+
keys: Key[];
|
|
46
|
+
utxos: EveryUtxoType[];
|
|
47
|
+
sigtype?: number;
|
|
55
48
|
pubkeys?: any[];
|
|
56
49
|
threshold?: number;
|
|
57
|
-
opts
|
|
58
|
-
}):
|
|
59
|
-
getRelatedUtxos(
|
|
60
|
-
outputs:
|
|
61
|
-
utxos:
|
|
62
|
-
}):
|
|
63
|
-
getOutputsFromTx(
|
|
64
|
-
tx:
|
|
65
|
-
}):
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
50
|
+
opts?: any;
|
|
51
|
+
}): string;
|
|
52
|
+
getRelatedUtxos(params: {
|
|
53
|
+
outputs: BitcoreLib.Transaction.Input[];
|
|
54
|
+
utxos: UtxoType[];
|
|
55
|
+
}): UtxoType[];
|
|
56
|
+
getOutputsFromTx(params: {
|
|
57
|
+
tx: BitcoreLib.Transaction;
|
|
58
|
+
}): Array<{
|
|
59
|
+
address: string | BitcoreLib.Script;
|
|
60
|
+
satoshis: number;
|
|
61
|
+
}>;
|
|
62
|
+
getSigningAddresses(params: {
|
|
63
|
+
tx: TransactionType;
|
|
64
|
+
utxos: EveryUtxoType[];
|
|
65
|
+
}): (string | undefined)[];
|
|
70
66
|
getSighash(params: {
|
|
71
|
-
tx:
|
|
67
|
+
tx: TransactionType;
|
|
72
68
|
index: number;
|
|
73
|
-
utxos?:
|
|
69
|
+
utxos?: EveryUtxoType[];
|
|
74
70
|
pubKey?: string | BitcoreLib.PublicKey | BitcoreLib.HDPublicKey;
|
|
75
71
|
path?: string;
|
|
76
72
|
sigtype?: number;
|
|
@@ -80,5 +76,28 @@ export declare class BTCTxProvider {
|
|
|
80
76
|
}): string;
|
|
81
77
|
}
|
|
82
78
|
type SignatureType = BitcoreLib.Transaction.Signature | BitcoreLib.crypto.Signature | TssSig;
|
|
79
|
+
type TransactionType = BitcoreLib.Transaction | string | Buffer | object;
|
|
80
|
+
export type UtxoType = {
|
|
81
|
+
txId: string;
|
|
82
|
+
outputIndex: number;
|
|
83
|
+
satoshis: number;
|
|
84
|
+
script?: string;
|
|
85
|
+
address?: string;
|
|
86
|
+
};
|
|
87
|
+
export type EveryUtxoType = Partial<{
|
|
88
|
+
txId: string;
|
|
89
|
+
outputIndex: number | string;
|
|
90
|
+
satoshis: number | string;
|
|
91
|
+
script: string | BitcoreLib.Script;
|
|
92
|
+
address: string | BitcoreLib.Address;
|
|
93
|
+
mintTxid: string;
|
|
94
|
+
mintIndex: number;
|
|
95
|
+
mintHeight: number;
|
|
96
|
+
value: number;
|
|
97
|
+
txid: string;
|
|
98
|
+
amount: number;
|
|
99
|
+
vout: number;
|
|
100
|
+
scriptPubKey: string;
|
|
101
|
+
}>;
|
|
83
102
|
export {};
|
|
84
103
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/transactions/btc/index.ts"],"names":[],"mappings":"AACA,OAAO,UAAU,MAAM,0BAA0B,CAAC;AAClD,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,wBAAwB,CAAC;AAIlD,UAAU,MAAM;IACd,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,qBAAa,aAAa;IACxB,GAAG,MAAc;IAEjB,WAAW,CACT,UAAU,EAAE,KAAK,CAAC;QAAE,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/transactions/btc/index.ts"],"names":[],"mappings":"AACA,OAAO,UAAU,MAAM,0BAA0B,CAAC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{
|
|
14
|
-
utxos
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
if (utxos.length > 0 && utxos.every(utxo => utxo.mintHeight != undefined)) {
|
|
15
|
+
utxos = utxos.sort(function (a, b) {
|
|
16
|
+
return a.mintHeight - b.mintHeight;
|
|
17
|
+
});
|
|
18
|
+
}
|
|
17
19
|
let index = 0;
|
|
18
20
|
let utxoSum = 0;
|
|
19
21
|
const recepientSum = recipients.reduce((sum, cur) => sum + Number(cur.amount), fee || 0);
|
|
20
22
|
while (utxoSum < recepientSum) {
|
|
23
|
+
(0, assert_1.default)(index < utxos.length, 'insufficient funds');
|
|
21
24
|
const utxo = utxos[index];
|
|
22
|
-
utxoSum += Number(utxo.value);
|
|
25
|
+
utxoSum += Number(utxo.satoshis ?? utxo.value ?? this.lib.Unit.fromBTC(utxo.amount ?? 0).toSatoshis());
|
|
23
26
|
index += 1;
|
|
24
27
|
}
|
|
25
28
|
const filteredUtxos = utxos.slice(0, index);
|
|
26
29
|
return filteredUtxos;
|
|
27
30
|
}
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
31
|
+
standardizeUtxo(utxo) {
|
|
32
|
+
const outputIndex = utxo.outputIndex ?? utxo.mintIndex ?? utxo.vout;
|
|
33
|
+
const satoshis = utxo.satoshis ?? utxo.value ?? (utxo.amount != undefined ? this.lib.Unit.fromBTC(utxo.amount ?? 0).toSatoshis() : undefined);
|
|
34
|
+
const txId = utxo.txId ?? utxo.mintTxid ?? utxo.txid;
|
|
35
|
+
const script = utxo.script ?? utxo.scriptPubKey;
|
|
36
|
+
$.checkArgument(outputIndex != undefined, 'output index required (outputIndex|mintIndex|vout)');
|
|
37
|
+
$.checkArgument(satoshis != undefined, 'satoshis required (satoshis|value|amount)');
|
|
38
|
+
$.checkArgument(txId, 'txid required (txid|txId|mintTxid)');
|
|
39
|
+
$.checkArgument(script || utxo.address, 'script or address required (script|scriptPubKey|address)');
|
|
40
|
+
return {
|
|
41
|
+
satoshis: Number(satoshis),
|
|
42
|
+
txId,
|
|
43
|
+
outputIndex: Number(outputIndex),
|
|
44
|
+
script: script != undefined ? new this.lib.Script(script).toHex() : undefined,
|
|
45
|
+
address: utxo.address != undefined ? new this.lib.Address(utxo.address).toString() : undefined
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
create(params) {
|
|
49
|
+
const { recipients, utxos = [], change, feeRate, fee, isSweep, replaceByFee, lockUntilDate, lockUntilBlock } = params;
|
|
50
|
+
const filteredUtxos = isSweep ? utxos : this.selectCoins(recipients, utxos, Number(fee));
|
|
51
|
+
const btcUtxos = filteredUtxos.map(utxo => this.standardizeUtxo(utxo));
|
|
38
52
|
const tx = new this.lib.Transaction().from(btcUtxos);
|
|
39
53
|
if (fee) {
|
|
40
54
|
tx.fee(fee);
|
|
@@ -46,7 +60,7 @@ class BTCTxProvider {
|
|
|
46
60
|
tx.change(change);
|
|
47
61
|
}
|
|
48
62
|
for (const recipient of recipients) {
|
|
49
|
-
tx.to(recipient.address,
|
|
63
|
+
tx.to(recipient.address, Number(recipient.amount));
|
|
50
64
|
}
|
|
51
65
|
if (replaceByFee && typeof tx.enableRBF === 'function') {
|
|
52
66
|
tx.enableRBF();
|
|
@@ -114,45 +128,42 @@ class BTCTxProvider {
|
|
|
114
128
|
return bitcoreTx.hash;
|
|
115
129
|
}
|
|
116
130
|
sign(params) {
|
|
117
|
-
const { tx, keys, pubkeys, threshold, opts } = params;
|
|
131
|
+
const { tx, keys, pubkeys, sigtype, threshold, opts } = params;
|
|
132
|
+
$.checkArgument(keys.every(k => !!k.privKey), 'Keys must contain private key data for signing');
|
|
118
133
|
const utxos = params.utxos || [];
|
|
119
134
|
const bitcoreTx = new this.lib.Transaction(tx);
|
|
135
|
+
const btcUtxos = utxos.map(utxo => this.standardizeUtxo(utxo));
|
|
120
136
|
const applicableUtxos = this.getRelatedUtxos({
|
|
121
137
|
outputs: bitcoreTx.inputs,
|
|
122
|
-
utxos
|
|
138
|
+
utxos: btcUtxos
|
|
123
139
|
});
|
|
124
|
-
bitcoreTx.associateInputs(applicableUtxos, pubkeys, threshold, opts);
|
|
140
|
+
bitcoreTx.associateInputs(applicableUtxos.map(utxo => new this.lib.Transaction.UnspentOutput(utxo)), pubkeys, threshold, opts);
|
|
125
141
|
const uniqePrivKeys = Object.values(keys.reduce((map, key) => {
|
|
126
142
|
const pk = new this.lib.PrivateKey(key.privKey);
|
|
127
143
|
map[pk.publicKey.toString()] = pk;
|
|
128
144
|
return map;
|
|
129
145
|
}, {}));
|
|
130
|
-
const signedTx = bitcoreTx.sign(uniqePrivKeys).toString();
|
|
146
|
+
const signedTx = bitcoreTx.sign(uniqePrivKeys, sigtype).toString();
|
|
131
147
|
return signedTx;
|
|
132
148
|
}
|
|
133
|
-
getRelatedUtxos(
|
|
149
|
+
getRelatedUtxos(params) {
|
|
150
|
+
const { outputs, utxos } = params;
|
|
134
151
|
const txids = outputs.map(output => output.toObject().prevTxId);
|
|
135
|
-
|
|
136
|
-
return applicableUtxos.map(utxo => {
|
|
137
|
-
const btcUtxo = Object.assign({}, utxo, {
|
|
138
|
-
amount: utxo.value / Math.pow(10, 8),
|
|
139
|
-
txid: utxo.mintTxid,
|
|
140
|
-
outputIndex: utxo.mintIndex
|
|
141
|
-
});
|
|
142
|
-
return new this.lib.Transaction.UnspentOutput(btcUtxo);
|
|
143
|
-
});
|
|
152
|
+
return utxos.filter(utxo => txids.includes(utxo.txId));
|
|
144
153
|
}
|
|
145
|
-
getOutputsFromTx(
|
|
146
|
-
return tx.outputs.map(({ script, satoshis }) => {
|
|
154
|
+
getOutputsFromTx(params) {
|
|
155
|
+
return params.tx.outputs.map(({ script, satoshis }) => {
|
|
147
156
|
const address = script;
|
|
148
157
|
return { address, satoshis };
|
|
149
158
|
});
|
|
150
159
|
}
|
|
151
|
-
getSigningAddresses(
|
|
160
|
+
getSigningAddresses(params) {
|
|
161
|
+
const { tx, utxos } = params;
|
|
152
162
|
const bitcoreTx = new this.lib.Transaction(tx);
|
|
163
|
+
const btcUtxos = utxos.map(utxo => this.standardizeUtxo(utxo));
|
|
153
164
|
const applicableUtxos = this.getRelatedUtxos({
|
|
154
165
|
outputs: bitcoreTx.inputs,
|
|
155
|
-
utxos
|
|
166
|
+
utxos: btcUtxos
|
|
156
167
|
});
|
|
157
168
|
return applicableUtxos.map(utxo => utxo.address);
|
|
158
169
|
}
|
|
@@ -163,7 +174,8 @@ class BTCTxProvider {
|
|
|
163
174
|
tx = new this.lib.Transaction(tx);
|
|
164
175
|
}
|
|
165
176
|
if (utxos) {
|
|
166
|
-
|
|
177
|
+
const btcUtxos = utxos.map(utxo => this.standardizeUtxo(utxo));
|
|
178
|
+
tx.associateInputs(btcUtxos.map(utxo => new this.lib.Transaction.UnspentOutput(utxo)), pubKeys, threshold, opts);
|
|
167
179
|
}
|
|
168
180
|
$.checkState(tx.inputs[index].output instanceof this.lib.Transaction.Output, 'Input must have all utxo info');
|
|
169
181
|
pubKey = pubKey?.toString();
|