@yaswap/yacoin 2.0.0

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.
Files changed (50) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/dist/lib/chain/YacoinBaseChainProvider.d.ts +12 -0
  3. package/dist/lib/chain/YacoinBaseChainProvider.js +7 -0
  4. package/dist/lib/chain/YacoinBaseChainProvider.js.map +1 -0
  5. package/dist/lib/chain/esplora/YacoinEsploraApiProvider.d.ts +19 -0
  6. package/dist/lib/chain/esplora/YacoinEsploraApiProvider.js +135 -0
  7. package/dist/lib/chain/esplora/YacoinEsploraApiProvider.js.map +1 -0
  8. package/dist/lib/chain/esplora/YacoinEsploraBaseProvider.d.ts +20 -0
  9. package/dist/lib/chain/esplora/YacoinEsploraBaseProvider.js +138 -0
  10. package/dist/lib/chain/esplora/YacoinEsploraBaseProvider.js.map +1 -0
  11. package/dist/lib/chain/esplora/YacoinEsploraBatchBaseProvider.d.ts +12 -0
  12. package/dist/lib/chain/esplora/YacoinEsploraBatchBaseProvider.js +48 -0
  13. package/dist/lib/chain/esplora/YacoinEsploraBatchBaseProvider.js.map +1 -0
  14. package/dist/lib/chain/esplora/types.d.ts +80 -0
  15. package/dist/lib/chain/esplora/types.js +3 -0
  16. package/dist/lib/chain/esplora/types.js.map +1 -0
  17. package/dist/lib/fee/YacoinFeeApiProvider.d.ts +6 -0
  18. package/dist/lib/fee/YacoinFeeApiProvider.js +45 -0
  19. package/dist/lib/fee/YacoinFeeApiProvider.js.map +1 -0
  20. package/dist/lib/index.d.ts +12 -0
  21. package/dist/lib/index.js +49 -0
  22. package/dist/lib/index.js.map +1 -0
  23. package/dist/lib/networks.d.ts +7 -0
  24. package/dist/lib/networks.js +14 -0
  25. package/dist/lib/networks.js.map +1 -0
  26. package/dist/lib/swap/YacoinSwapBaseProvider.d.ts +48 -0
  27. package/dist/lib/swap/YacoinSwapBaseProvider.js +320 -0
  28. package/dist/lib/swap/YacoinSwapBaseProvider.js.map +1 -0
  29. package/dist/lib/swap/YacoinSwapEsploraProvider.d.ts +11 -0
  30. package/dist/lib/swap/YacoinSwapEsploraProvider.js +56 -0
  31. package/dist/lib/swap/YacoinSwapEsploraProvider.js.map +1 -0
  32. package/dist/lib/swap/types.d.ts +12 -0
  33. package/dist/lib/swap/types.js +4 -0
  34. package/dist/lib/swap/types.js.map +1 -0
  35. package/dist/lib/types.d.ts +84 -0
  36. package/dist/lib/types.js +40 -0
  37. package/dist/lib/types.js.map +1 -0
  38. package/dist/lib/utils.d.ts +30 -0
  39. package/dist/lib/utils.js +216 -0
  40. package/dist/lib/utils.js.map +1 -0
  41. package/dist/lib/wallet/IYacoinWallet.d.ts +11 -0
  42. package/dist/lib/wallet/IYacoinWallet.js +3 -0
  43. package/dist/lib/wallet/IYacoinWallet.js.map +1 -0
  44. package/dist/lib/wallet/YacoinBaseWallet.d.ts +68 -0
  45. package/dist/lib/wallet/YacoinBaseWallet.js +426 -0
  46. package/dist/lib/wallet/YacoinBaseWallet.js.map +1 -0
  47. package/dist/lib/wallet/YacoinHDWallet.d.ts +43 -0
  48. package/dist/lib/wallet/YacoinHDWallet.js +250 -0
  49. package/dist/lib/wallet/YacoinHDWallet.js.map +1 -0
  50. package/package.json +36 -0
@@ -0,0 +1,7 @@
1
+ import { YacoinNetwork } from './types';
2
+ declare const YacoinNetworks: {
3
+ yacoin: YacoinNetwork;
4
+ yacoin_testnet: YacoinNetwork;
5
+ yacoin_regtest: YacoinNetwork;
6
+ };
7
+ export { YacoinNetworks };
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.YacoinNetworks = void 0;
4
+ const yacoinjs_lib_1 = require("@yaswap/yacoinjs-lib");
5
+ const yacoin = Object.assign(Object.assign({ name: 'yacoin' }, yacoinjs_lib_1.networks.yacoin), { coinType: '0', isTestnet: false });
6
+ const yacoin_testnet = Object.assign(Object.assign({ name: 'yacoin_testnet' }, yacoinjs_lib_1.networks.testnet), { coinType: '0', isTestnet: true });
7
+ const yacoin_regtest = Object.assign(Object.assign({ name: 'yacoin_regtest' }, yacoinjs_lib_1.networks.regtest), { coinType: '1', isTestnet: true });
8
+ const YacoinNetworks = {
9
+ yacoin,
10
+ yacoin_testnet,
11
+ yacoin_regtest,
12
+ };
13
+ exports.YacoinNetworks = YacoinNetworks;
14
+ //# sourceMappingURL=networks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"networks.js","sourceRoot":"","sources":["../../lib/networks.ts"],"names":[],"mappings":";;;AAAA,uDAAgD;AAGhD,MAAM,MAAM,iCACR,IAAI,EAAE,QAAQ,IACX,uBAAQ,CAAC,MAAM,KAClB,QAAQ,EAAE,GAAG,EACb,SAAS,EAAE,KAAK,GACnB,CAAC;AAEF,MAAM,cAAc,iCAChB,IAAI,EAAE,gBAAgB,IACnB,uBAAQ,CAAC,OAAO,KACnB,QAAQ,EAAE,GAAG,EACb,SAAS,EAAE,IAAI,GAClB,CAAC;AAEF,MAAM,cAAc,iCAChB,IAAI,EAAE,gBAAgB,IACnB,uBAAQ,CAAC,OAAO,KACnB,QAAQ,EAAE,GAAG,EACb,SAAS,EAAE,IAAI,GAClB,CAAC;AAEF,MAAM,cAAc,GAAG;IACnB,MAAM;IACN,cAAc;IACd,cAAc;CACjB,CAAC;AAEO,wCAAc"}
@@ -0,0 +1,48 @@
1
+ /// <reference types="node" />
2
+ import { Swap } from '@yaswap/client';
3
+ import { SwapParams, Transaction } from '@yaswap/types';
4
+ import { payments } from '@yaswap/yacoinjs-lib';
5
+ import { YacoinBaseChainProvider } from '../chain/YacoinBaseChainProvider';
6
+ import { YacoinNetwork, Input, SwapMode, Transaction as YacoinTransaction } from '../types';
7
+ import { IYacoinWallet } from '../wallet/IYacoinWallet';
8
+ import { YacoinSwapProviderOptions, TransactionMatchesFunction } from './types';
9
+ export declare abstract class YacoinSwapBaseProvider extends Swap<YacoinBaseChainProvider, null, IYacoinWallet<YacoinBaseChainProvider>> {
10
+ protected _network: YacoinNetwork;
11
+ protected _mode: SwapMode;
12
+ constructor(options: YacoinSwapProviderOptions, walletProvider?: IYacoinWallet<YacoinBaseChainProvider>);
13
+ validateSwapParams(swapParams: SwapParams): void;
14
+ initiateSwap(swapParams: SwapParams, feePerByte: number): Promise<Transaction<any>>;
15
+ fundSwap(): Promise<null>;
16
+ findFundSwapTransaction(): Promise<null>;
17
+ claimSwap(swapParams: SwapParams, initiationTxHash: string, secret: string, feePerByte: number): Promise<Transaction<YacoinTransaction>>;
18
+ refundSwap(swapParams: SwapParams, initiationTxHash: string, feePerByte: number): Promise<Transaction<YacoinTransaction>>;
19
+ findInitiateSwapTransaction(swapParams: SwapParams, blockNumber?: number): Promise<Transaction<any>>;
20
+ getSwapSecret(claimTxHash: string, initTxHash: string): Promise<string>;
21
+ findClaimSwapTransaction(swapParams: SwapParams, initTxHash: string, blockNumber?: number): Promise<Transaction<any>>;
22
+ findRefundSwapTransaction(swapParams: SwapParams, initiationTxHash: string, blockNumber?: number): Promise<Transaction<any>>;
23
+ protected onWalletProviderUpdate(_wallet: IYacoinWallet<YacoinBaseChainProvider, any>): void;
24
+ protected getSwapOutput(swapParams: SwapParams): Buffer;
25
+ private getSwapInput;
26
+ protected getSwapPaymentVariants(swapOutput: Buffer): {
27
+ p2sh: payments.Payment;
28
+ };
29
+ private _redeemSwap;
30
+ private _redeemSwapOutput;
31
+ protected extractSwapParams(outputScript: string): {
32
+ recipientPublicKey: string;
33
+ refundPublicKey: string;
34
+ secretHash: string;
35
+ expiration: number;
36
+ };
37
+ /**
38
+ * Only to be used for situations where transaction is trusted. e.g to bump fee
39
+ * DO NOT USE THIS TO VERIFY THE REDEEM
40
+ */
41
+ private UNSAFE_isSwapRedeemTransaction;
42
+ canUpdateFee(): boolean;
43
+ updateTransactionFee(tx: Transaction<YacoinTransaction> | string, newFeePerByte: number): Promise<Transaction<any>>;
44
+ protected getInputScript(vin: Input): string[];
45
+ protected doesTransactionMatchRedeem(initiationTxHash: string, tx: Transaction<YacoinTransaction>, isRefund: boolean): boolean;
46
+ protected doesTransactionMatchInitiation(swapParams: SwapParams, transaction: Transaction<YacoinTransaction>): boolean;
47
+ protected abstract findSwapTransaction(swapParams: SwapParams, blockNumber: number, predicate: TransactionMatchesFunction): Promise<Transaction<YacoinTransaction>>;
48
+ }
@@ -0,0 +1,320 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.YacoinSwapBaseProvider = void 0;
13
+ const client_1 = require("@yaswap/client");
14
+ const types_1 = require("@yaswap/types");
15
+ const utils_1 = require("@yaswap/utils");
16
+ const yacoinjs_lib_1 = require("@yaswap/yacoinjs-lib");
17
+ const types_2 = require("../types");
18
+ const utils_2 = require("../utils");
19
+ class YacoinSwapBaseProvider extends client_1.Swap {
20
+ constructor(options, walletProvider) {
21
+ super(walletProvider);
22
+ const { network, mode = types_2.SwapMode.P2SH } = options;
23
+ const swapModes = Object.values(types_2.SwapMode);
24
+ if (!swapModes.includes(mode)) {
25
+ throw new Error(`Mode must be one of ${swapModes.join(',')}`);
26
+ }
27
+ this._network = network;
28
+ this._mode = mode;
29
+ }
30
+ validateSwapParams(swapParams) {
31
+ (0, utils_1.validateValue)(swapParams.value);
32
+ (0, utils_2.validateAddress)(swapParams.recipientAddress, this._network);
33
+ (0, utils_2.validateAddress)(swapParams.refundAddress, this._network);
34
+ (0, utils_1.validateSecretHash)(swapParams.secretHash);
35
+ (0, utils_1.validateExpiration)(swapParams.expiration);
36
+ }
37
+ initiateSwap(swapParams, feePerByte) {
38
+ return __awaiter(this, void 0, void 0, function* () {
39
+ this.validateSwapParams(swapParams);
40
+ const swapOutput = this.getSwapOutput(swapParams);
41
+ const address = this.getSwapPaymentVariants(swapOutput)[this._mode].address;
42
+ return this.walletProvider.sendTransaction({
43
+ to: address,
44
+ value: swapParams.value,
45
+ fee: feePerByte,
46
+ });
47
+ });
48
+ }
49
+ fundSwap() {
50
+ return __awaiter(this, void 0, void 0, function* () {
51
+ return null;
52
+ });
53
+ }
54
+ findFundSwapTransaction() {
55
+ return __awaiter(this, void 0, void 0, function* () {
56
+ return null;
57
+ });
58
+ }
59
+ claimSwap(swapParams, initiationTxHash, secret, feePerByte) {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ this.validateSwapParams(swapParams);
62
+ (0, utils_1.validateSecret)(secret);
63
+ (0, utils_1.validateSecretAndHash)(secret, swapParams.secretHash);
64
+ yield this.verifyInitiateSwapTransaction(swapParams, initiationTxHash);
65
+ return this._redeemSwap(swapParams, initiationTxHash, true, secret, feePerByte);
66
+ });
67
+ }
68
+ refundSwap(swapParams, initiationTxHash, feePerByte) {
69
+ return __awaiter(this, void 0, void 0, function* () {
70
+ this.validateSwapParams(swapParams);
71
+ yield this.verifyInitiateSwapTransaction(swapParams, initiationTxHash);
72
+ return this._redeemSwap(swapParams, initiationTxHash, false, undefined, feePerByte);
73
+ });
74
+ }
75
+ findInitiateSwapTransaction(swapParams, blockNumber) {
76
+ this.validateSwapParams(swapParams);
77
+ return this.findSwapTransaction(swapParams, blockNumber, (tx) => this.doesTransactionMatchInitiation(swapParams, tx));
78
+ }
79
+ getSwapSecret(claimTxHash, initTxHash) {
80
+ return __awaiter(this, void 0, void 0, function* () {
81
+ const claimSwapTransaction = yield this.walletProvider
82
+ .getChainProvider()
83
+ .getTransactionByHash(claimTxHash);
84
+ if (claimSwapTransaction) {
85
+ const swapInput = claimSwapTransaction._raw.vin.find((vin) => vin.txid === initTxHash);
86
+ if (!swapInput) {
87
+ throw new Error('Claim input missing');
88
+ }
89
+ const inputScript = this.getInputScript(swapInput);
90
+ const secret = inputScript[2];
91
+ return secret;
92
+ }
93
+ });
94
+ }
95
+ findClaimSwapTransaction(swapParams, initTxHash, blockNumber) {
96
+ return __awaiter(this, void 0, void 0, function* () {
97
+ this.validateSwapParams(swapParams);
98
+ const claimSwapTransaction = yield this.findSwapTransaction(swapParams, blockNumber, (tx) => this.doesTransactionMatchRedeem(initTxHash, tx, false));
99
+ if (claimSwapTransaction) {
100
+ const swapInput = claimSwapTransaction._raw.vin.find((vin) => vin.txid === initTxHash);
101
+ if (!swapInput) {
102
+ throw new Error('Claim input missing');
103
+ }
104
+ const inputScript = this.getInputScript(swapInput);
105
+ const secret = inputScript[2];
106
+ (0, utils_1.validateSecretAndHash)(secret, swapParams.secretHash);
107
+ return Object.assign(Object.assign({}, claimSwapTransaction), { secret, _raw: claimSwapTransaction });
108
+ }
109
+ });
110
+ }
111
+ findRefundSwapTransaction(swapParams, initiationTxHash, blockNumber) {
112
+ return __awaiter(this, void 0, void 0, function* () {
113
+ this.validateSwapParams(swapParams);
114
+ const refundSwapTransaction = yield this.findSwapTransaction(swapParams, blockNumber, (tx) => this.doesTransactionMatchRedeem(initiationTxHash, tx, true));
115
+ return refundSwapTransaction;
116
+ });
117
+ }
118
+ onWalletProviderUpdate(_wallet) {
119
+ // do nothing
120
+ }
121
+ getSwapOutput(swapParams) {
122
+ this.validateSwapParams(swapParams);
123
+ const secretHashBuff = Buffer.from(swapParams.secretHash, 'hex');
124
+ const recipientPubKeyHash = (0, utils_2.getPubKeyHash)(swapParams.recipientAddress.toString(), this._network);
125
+ const refundPubKeyHash = (0, utils_2.getPubKeyHash)(swapParams.refundAddress.toString(), this._network);
126
+ const OPS = yacoinjs_lib_1.script.OPS;
127
+ const script = yacoinjs_lib_1.script.compile([
128
+ OPS.OP_IF,
129
+ OPS.OP_SIZE,
130
+ yacoinjs_lib_1.script.number.encode(32),
131
+ OPS.OP_EQUALVERIFY,
132
+ OPS.OP_SHA256,
133
+ secretHashBuff,
134
+ OPS.OP_EQUALVERIFY,
135
+ OPS.OP_DUP,
136
+ OPS.OP_HASH160,
137
+ recipientPubKeyHash,
138
+ OPS.OP_ELSE,
139
+ yacoinjs_lib_1.script.number.encode(swapParams.expiration),
140
+ OPS.OP_CHECKLOCKTIMEVERIFY,
141
+ OPS.OP_DROP,
142
+ OPS.OP_DUP,
143
+ OPS.OP_HASH160,
144
+ refundPubKeyHash,
145
+ OPS.OP_ENDIF,
146
+ OPS.OP_EQUALVERIFY,
147
+ OPS.OP_CHECKSIG,
148
+ ]);
149
+ if (![97, 98].includes(Buffer.byteLength(script))) {
150
+ throw new Error('Invalid swap script');
151
+ }
152
+ return script;
153
+ }
154
+ getSwapInput(sig, pubKey, isClaim, secret) {
155
+ const OPS = yacoinjs_lib_1.script.OPS;
156
+ const redeem = isClaim ? OPS.OP_TRUE : OPS.OP_FALSE;
157
+ const secretParams = isClaim ? [Buffer.from(secret, 'hex')] : [];
158
+ return yacoinjs_lib_1.script.compile([sig, pubKey, ...secretParams, redeem]);
159
+ }
160
+ getSwapPaymentVariants(swapOutput) {
161
+ const p2sh = yacoinjs_lib_1.payments.p2sh({
162
+ redeem: { output: swapOutput, network: this._network },
163
+ network: this._network,
164
+ });
165
+ return {
166
+ [types_2.SwapMode.P2SH]: p2sh,
167
+ };
168
+ }
169
+ _redeemSwap(swapParams, initiationTxHash, isClaim, secret, feePerByte) {
170
+ return __awaiter(this, void 0, void 0, function* () {
171
+ const address = isClaim ? swapParams.recipientAddress : swapParams.refundAddress;
172
+ const swapOutput = this.getSwapOutput(swapParams);
173
+ return this._redeemSwapOutput(initiationTxHash, swapParams.value, address.toString(), swapOutput, swapParams.expiration, isClaim, secret, feePerByte);
174
+ });
175
+ }
176
+ _redeemSwapOutput(initiationTxHash, value, address, swapOutput, expiration, isClaim, secret, _feePerByte) {
177
+ return __awaiter(this, void 0, void 0, function* () {
178
+ const network = this._network;
179
+ const swapPaymentVariants = this.getSwapPaymentVariants(swapOutput);
180
+ const initiationTxRaw = yield this.walletProvider.getChainProvider().getProvider().getRawTransactionByHash(initiationTxHash);
181
+ const initiationTx = (0, utils_2.decodeRawTransaction)(initiationTxRaw, this._network);
182
+ let swapVout;
183
+ let paymentVariant;
184
+ for (const vout of initiationTx.vout) {
185
+ const paymentVariantEntry = Object.entries(swapPaymentVariants).find(([, payment]) => payment.output.toString('hex') === vout.scriptPubKey.hex);
186
+ const voutValue = new types_1.BigNumber(vout.value).times(1e6);
187
+ if (paymentVariantEntry && voutValue.eq(new types_1.BigNumber(value))) {
188
+ paymentVariant = paymentVariantEntry[1];
189
+ swapVout = vout;
190
+ }
191
+ }
192
+ if (!swapVout) {
193
+ throw new Error('Valid swap output not found');
194
+ }
195
+ const feePerByte = _feePerByte || (yield this.walletProvider.getChainProvider().getProvider().getFeePerByte());
196
+ // TODO: Implement proper fee calculation that counts bytes in inputs and outputs
197
+ const txfee = (0, utils_2.calculateFee)(1, 1, feePerByte);
198
+ const swapValue = new types_1.BigNumber(swapVout.value).times(1e6).toNumber();
199
+ if (swapValue - txfee < 0) {
200
+ throw new Error('Transaction amount does not cover fee.');
201
+ }
202
+ // BEGIN CHANGE
203
+ const hashType = yacoinjs_lib_1.Transaction.SIGHASH_ALL;
204
+ const redeemScript = paymentVariant.redeem.output;
205
+ var tx = new yacoinjs_lib_1.Transaction();
206
+ if (!isClaim) {
207
+ tx.locktime = expiration;
208
+ }
209
+ tx.addInput(Buffer.from(initiationTxHash, 'hex').reverse(), swapVout.n, 0);
210
+ tx.addOutput(yacoinjs_lib_1.address.toOutputScript(address, network), swapValue - txfee);
211
+ let signatureHash = tx.hashForSignature(0, redeemScript, hashType);
212
+ // Sign transaction
213
+ const walletAddress = yield this.walletProvider.getWalletAddress(address);
214
+ const signedSignatureHash = yield this.walletProvider.signTx(tx.toHex(), signatureHash.toString('hex'), walletAddress.derivationPath, txfee);
215
+ const swapInput = this.getSwapInput(yacoinjs_lib_1.script.signature.encode(Buffer.from(signedSignatureHash, 'hex'), hashType), Buffer.from(walletAddress.publicKey, 'hex'), isClaim, secret);
216
+ const redeemScriptSig = yacoinjs_lib_1.payments.p2sh({
217
+ network: network,
218
+ redeem: {
219
+ network: network,
220
+ output: redeemScript,
221
+ input: swapInput
222
+ }
223
+ }).input;
224
+ tx.setInputScript(0, redeemScriptSig);
225
+ // END CHANGE
226
+ const hex = tx.toHex();
227
+ yield this.walletProvider.getChainProvider().sendRawTransaction(`data=${hex}`);
228
+ return (0, utils_2.normalizeTransactionObject)((0, utils_2.decodeRawTransaction)(hex, this._network), txfee);
229
+ });
230
+ }
231
+ extractSwapParams(outputScript) {
232
+ const buffer = yacoinjs_lib_1.script.decompile(Buffer.from(outputScript, 'hex'));
233
+ if (buffer.length !== 20) {
234
+ throw new Error('Invalid swap output script');
235
+ }
236
+ const secretHash = buffer[5].reverse().toString('hex');
237
+ const recipientPublicKey = buffer[9].reverse().toString('hex');
238
+ const expiration = parseInt(buffer[11].reverse().toString('hex'), 16);
239
+ const refundPublicKey = buffer[16].reverse().toString('hex');
240
+ return { recipientPublicKey, refundPublicKey, secretHash, expiration };
241
+ }
242
+ /**
243
+ * Only to be used for situations where transaction is trusted. e.g to bump fee
244
+ * DO NOT USE THIS TO VERIFY THE REDEEM
245
+ */
246
+ UNSAFE_isSwapRedeemTransaction(transaction) {
247
+ return __awaiter(this, void 0, void 0, function* () {
248
+ // eslint-disable-line
249
+ if (transaction._raw.vin.length === 1 && transaction._raw.vout.length === 1) {
250
+ const swapInput = transaction._raw.vin[0];
251
+ const inputScript = this.getInputScript(swapInput);
252
+ const initiationTransaction = yield this.walletProvider
253
+ .getChainProvider()
254
+ .getTransactionByHash(transaction._raw.vin[0].txid);
255
+ const scriptType = initiationTransaction._raw.vout[transaction._raw.vin[0].vout].scriptPubKey.type;
256
+ if (['scripthash', 'witness_v0_scripthash'].includes(scriptType) && [4, 5].includes(inputScript.length))
257
+ return true;
258
+ }
259
+ return false;
260
+ });
261
+ }
262
+ canUpdateFee() {
263
+ return true;
264
+ }
265
+ updateTransactionFee(tx, newFeePerByte) {
266
+ return __awaiter(this, void 0, void 0, function* () {
267
+ const txHash = typeof tx === 'string' ? tx : tx.hash;
268
+ const transaction = yield this.walletProvider.getChainProvider().getTransactionByHash(txHash);
269
+ if (yield this.UNSAFE_isSwapRedeemTransaction(transaction)) {
270
+ const swapInput = transaction._raw.vin[0];
271
+ const inputScript = this.getInputScript(swapInput);
272
+ const initiationTxHash = swapInput.txid;
273
+ const initiationTx = yield this.walletProvider
274
+ .getChainProvider()
275
+ .getTransactionByHash(initiationTxHash);
276
+ const swapOutput = initiationTx._raw.vout[swapInput.vout];
277
+ const value = new types_1.BigNumber(swapOutput.value).times(1e6);
278
+ const address = transaction._raw.vout[0].scriptPubKey.addresses[0];
279
+ const isClaim = inputScript.length === 5;
280
+ const secret = isClaim ? inputScript[2] : undefined;
281
+ const outputScript = isClaim ? inputScript[4] : inputScript[3];
282
+ const { expiration } = this.extractSwapParams(outputScript);
283
+ return this._redeemSwapOutput(initiationTxHash, value, address, Buffer.from(outputScript, 'hex'), expiration, isClaim, secret, newFeePerByte);
284
+ }
285
+ return this.walletProvider.updateTransactionFee(tx, newFeePerByte);
286
+ });
287
+ }
288
+ getInputScript(vin) {
289
+ const inputScript = yacoinjs_lib_1.script
290
+ .decompile(Buffer.from(vin.scriptSig.hex, 'hex'))
291
+ .map((b) => (Buffer.isBuffer(b) ? b.toString('hex') : b));
292
+ return inputScript;
293
+ }
294
+ doesTransactionMatchRedeem(initiationTxHash, tx, isRefund) {
295
+ const swapInput = tx._raw.vin.find((vin) => vin.txid === initiationTxHash);
296
+ if (!swapInput)
297
+ return false;
298
+ const inputScript = this.getInputScript(swapInput);
299
+ if (!inputScript)
300
+ return false;
301
+ if (isRefund) {
302
+ if (inputScript.length !== 4)
303
+ return false; // 4 because there is 4 parameters: signature, pubkey, false, original redeemscript
304
+ }
305
+ else {
306
+ if (inputScript.length !== 5)
307
+ return false; // 5 because there is 5 parameters: signature, pubkey, secretHash, true, original redeemscript
308
+ }
309
+ return true;
310
+ }
311
+ doesTransactionMatchInitiation(swapParams, transaction) {
312
+ const swapOutput = this.getSwapOutput(swapParams);
313
+ const swapPaymentVariants = this.getSwapPaymentVariants(swapOutput);
314
+ const vout = transaction._raw.vout.find((vout) => Object.values(swapPaymentVariants).find((payment) => payment.output.toString('hex') === vout.scriptPubKey.hex &&
315
+ new types_1.BigNumber(vout.value).times(1e6).eq(new types_1.BigNumber(swapParams.value))));
316
+ return Boolean(vout);
317
+ }
318
+ }
319
+ exports.YacoinSwapBaseProvider = YacoinSwapBaseProvider;
320
+ //# sourceMappingURL=YacoinSwapBaseProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"YacoinSwapBaseProvider.js","sourceRoot":"","sources":["../../../lib/swap/YacoinSwapBaseProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAAsC;AACtC,yCAA4E;AAC5E,yCAA6H;AAC7H,uDAAmI;AAEnI,oCAA4F;AAC5F,oCAMkB;AAIlB,MAAsB,sBAAuB,SAAQ,aAA2E;IAI5H,YAAY,OAAkC,EAAE,cAAuD;QACnG,KAAK,CAAC,cAAc,CAAC,CAAC;QACtB,MAAM,EAAE,OAAO,EAAE,IAAI,GAAG,gBAAQ,CAAC,IAAI,EAAE,GAAG,OAAO,CAAC;QAClD,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,gBAAQ,CAAC,CAAC;QAC1C,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE;YAC3B,MAAM,IAAI,KAAK,CAAC,uBAAuB,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;SACjE;QACD,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;IACtB,CAAC;I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OAAO,KAAK,CAAC;QAC7B,MAAM,WAAW,GAAG,IAAI,CAAC,cAAc,CAAC,SAAS,CAAC,CAAC;QACnD,IAAI,CAAC,WAAW;YAAE,OAAO,KAAK,CAAC;QAC/B,IAAI,QAAQ,EAAE;YACV,IAAI,WAAW,CAAC,MAAM,KAAK,CAAC;gBAAE,OAAO,KAAK,CAAC,CAAC,mFAAmF;SAClI;aAAM;YACH,IAAI,WAAW,CAAC,MAAM,KAAK,CAAC;gBAAE,OAAO,KAAK,CAAC,CAAC,8FAA8F;SAC7I;QACD,OAAO,IAAI,CAAC;IAChB,CAAC;IAES,8BAA8B,CAAC,UAAsB,EAAE,WAA2C;QACxG,MAAM,UAAU,GAAG,IAAI,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC;QAClD,MAAM,mBAAmB,GAAG,IAAI,CAAC,sBAAsB,CAAC,UAAU,CAAC,CAAC;QACpE,MAAM,IAAI,GAAG,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAC7C,MAAM,CAAC,MAAM,CAAC,mBAAmB,CAAC,CAAC,IAAI,CACnC,CAAC,OAAO,EAAE,EAAE,CACR,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,IAAI,CAAC,YAAY,CAAC,GAAG;YACxD,IAAI,iBAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,IAAI,iBAAS,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAC/E,CACJ,CAAC;QACF,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC;IACzB,CAAC;CAOJ;AAvXD,wDAuXC"}
@@ -0,0 +1,11 @@
1
+ import { SwapParams, Transaction } from '@yaswap/types';
2
+ import { Transaction as YacoinTransaction } from '../types';
3
+ import { YacoinBaseWalletProvider } from '../wallet/YacoinBaseWallet';
4
+ import { YacoinSwapBaseProvider } from './YacoinSwapBaseProvider';
5
+ import { YacoinSwapProviderOptions, TransactionMatchesFunction } from './types';
6
+ export declare class YacoinSwapEsploraProvider extends YacoinSwapBaseProvider {
7
+ private _httpClient;
8
+ constructor(options: YacoinSwapProviderOptions, walletProvider?: YacoinBaseWalletProvider);
9
+ findSwapTransaction(swapParams: SwapParams, _blockNumber: number, predicate: TransactionMatchesFunction): Promise<Transaction<YacoinTransaction>>;
10
+ private findAddressTransaction;
11
+ }
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.YacoinSwapEsploraProvider = void 0;
13
+ const client_1 = require("@yaswap/client");
14
+ const errors_1 = require("@yaswap/errors");
15
+ const YacoinSwapBaseProvider_1 = require("./YacoinSwapBaseProvider");
16
+ class YacoinSwapEsploraProvider extends YacoinSwapBaseProvider_1.YacoinSwapBaseProvider {
17
+ constructor(options, walletProvider) {
18
+ super(options, walletProvider);
19
+ this._httpClient = new client_1.HttpClient({ baseURL: options.scraperUrl });
20
+ }
21
+ findSwapTransaction(swapParams, _blockNumber, predicate) {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ const swapOutput = this.getSwapOutput(swapParams);
24
+ const paymentVariants = this.getSwapPaymentVariants(swapOutput);
25
+ for (const paymentVariant of Object.values(paymentVariants)) {
26
+ const addressTransaction = this.findAddressTransaction(paymentVariant.address, predicate);
27
+ if (addressTransaction)
28
+ return addressTransaction;
29
+ }
30
+ });
31
+ }
32
+ findAddressTransaction(address, predicate) {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ // TODO: This does not go through pages as swap addresses have at most 2 transactions
35
+ // Investigate whether retrieving more transactions is required.
36
+ const addressInfo = yield this._httpClient.nodeGet(`/ext/getaddress/${address}`);
37
+ try {
38
+ for (const transaction of addressInfo.last_txs) {
39
+ const formattedTransaction = yield this.walletProvider
40
+ .getChainProvider()
41
+ .getProvider()
42
+ .getTransaction(transaction.addresses);
43
+ if (predicate(formattedTransaction)) {
44
+ return formattedTransaction;
45
+ }
46
+ }
47
+ }
48
+ catch (error) {
49
+ if (['TypeError'].includes(error.name))
50
+ throw new errors_1.PendingTxError(`Transaction not confirmed`);
51
+ }
52
+ });
53
+ }
54
+ }
55
+ exports.YacoinSwapEsploraProvider = YacoinSwapEsploraProvider;
56
+ //# sourceMappingURL=YacoinSwapEsploraProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"YacoinSwapEsploraProvider.js","sourceRoot":"","sources":["../../../lib/swap/YacoinSwapEsploraProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAA4C;AAC5C,2CAAgD;AAIhD,qEAAkE;AAGlE,MAAa,yBAA0B,SAAQ,+CAAsB;IAGjE,YAAY,OAAkC,EAAE,cAAyC;QACrF,KAAK,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC;QAC/B,IAAI,CAAC,WAAW,GAAG,IAAI,mBAAU,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC;IACvE,CAAC;IAEY,mBAAmB,CAAC,UAAsB,EAAE,YAAoB,EAAE,SAAqC;;YAChH,MAAM,UAAU,GAAW,IAAI,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC;YAC1D,MAAM,eAAe,GAAoB,IAAI,CAAC,sBAAsB,CAAC,UAAU,CAAC,CAAC;YACjF,KAAK,MAAM,cAAc,IAAI,MAAM,CAAC,MAAM,CAAC,eAAe,CAAC,EAAE;gBACzD,MAAM,kBAAkB,GAAG,IAAI,CAAC,sBAAsB,CAAC,cAAc,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;gBAC1F,IAAI,kBAAkB;oBAAE,OAAO,kBAAkB,CAAC;aACrD;QACL,CAAC;KAAA;IAEa,sBAAsB,CAAC,OAAe,EAAE,SAAqC;;YACvF,qFAAqF;YACrF,gEAAgE;YAChE,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,mBAAmB,OAAO,EAAE,CAAC,CAAC;YAEjF,IAAI;gBACA,KAAK,MAAM,WAAW,IAAI,WAAW,CAAC,QAAQ,EAAE;oBAC5C,MAAM,oBAAoB,GAAmC,MAAM,IAAI,CAAC,cAAc;yBACjF,gBAAgB,EAAE;yBAClB,WAAW,EAAE;yBACb,cAAc,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC;oBAC3C,IAAI,SAAS,CAAC,oBAAoB,CAAC,EAAE;wBACjC,OAAO,oBAAoB,CAAC;qBAC/B;iBACJ;aACJ;YAAC,OAAO,KAAK,EAAE;gBACZ,IAAI,CAAC,WAAW,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC;oBAAE,MAAM,IAAI,uBAAc,CAAC,2BAA2B,CAAC,CAAC;aACjG;QAEL,CAAC;KAAA;CACJ;AArCD,8DAqCC"}
@@ -0,0 +1,12 @@
1
+ import { Transaction } from '@yaswap/types';
2
+ import { payments } from '@yaswap/yacoinjs-lib';
3
+ import { YacoinNetwork, SwapMode, Transaction as YacoinTransaction } from '../types';
4
+ export interface YacoinSwapProviderOptions {
5
+ network: YacoinNetwork;
6
+ mode?: SwapMode;
7
+ scraperUrl?: string;
8
+ }
9
+ export declare type TransactionMatchesFunction = (tx: Transaction<YacoinTransaction>) => boolean;
10
+ export declare type PaymentVariants = {
11
+ [SwapMode.P2SH]?: payments.Payment;
12
+ };
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const types_1 = require("../types");
4
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../lib/swap/types.ts"],"names":[],"mappings":";;AAEA,oCAAqF"}
@@ -0,0 +1,84 @@
1
+ /// <reference types="node" />
2
+ import { Network } from '@yaswap/types';
3
+ import { Network as YacoinJsLibNetwork } from '@yaswap/yacoinjs-lib';
4
+ export * as YacoinEsploraTypes from './chain/esplora/types';
5
+ export * from './swap/types';
6
+ export interface YacoinNetwork extends Network, YacoinJsLibNetwork {
7
+ }
8
+ export interface YacoinNodeWalletOptions {
9
+ addressType?: AddressType;
10
+ network?: YacoinNetwork;
11
+ }
12
+ export interface YacoinWalletProviderOptions extends YacoinNodeWalletOptions {
13
+ baseDerivationPath: string;
14
+ }
15
+ export interface YacoinHDWalletProviderOptions extends YacoinWalletProviderOptions {
16
+ mnemonic: string;
17
+ }
18
+ export interface OutputTarget {
19
+ address?: string;
20
+ script?: Buffer;
21
+ value: number;
22
+ }
23
+ export interface ScriptPubKey {
24
+ asm: string;
25
+ hex: string;
26
+ reqSigs: number;
27
+ type: string;
28
+ addresses: string[];
29
+ }
30
+ export interface Output {
31
+ value: number;
32
+ n: number;
33
+ scriptPubKey: ScriptPubKey;
34
+ }
35
+ export interface Input {
36
+ txid: string;
37
+ vout: number;
38
+ scriptSig: {
39
+ asm: string;
40
+ hex: string;
41
+ };
42
+ sequence: number;
43
+ coinbase?: string;
44
+ }
45
+ export interface Transaction {
46
+ txid: string;
47
+ hash: string;
48
+ version: number;
49
+ time: number;
50
+ locktime: number;
51
+ size: number;
52
+ vsize: number;
53
+ weight: number;
54
+ vin: Input[];
55
+ vout: Output[];
56
+ confirmations?: number;
57
+ hex: string;
58
+ }
59
+ export interface UTXO {
60
+ txid: string;
61
+ vout: number;
62
+ value: number;
63
+ address: string;
64
+ derivationPath?: string;
65
+ }
66
+ export declare enum AddressType {
67
+ LEGACY = "legacy"
68
+ }
69
+ export declare enum SwapMode {
70
+ P2SH = "p2sh"
71
+ }
72
+ export declare type AddressTxCounts = {
73
+ [index: string]: number;
74
+ };
75
+ export interface PsbtInputTarget {
76
+ index: number;
77
+ derivationPath: string;
78
+ }
79
+ export interface P2SHInput {
80
+ inputTxHex: string;
81
+ index: number;
82
+ vout: any;
83
+ outputScript: Buffer;
84
+ }
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.SwapMode = exports.AddressType = exports.YacoinEsploraTypes = void 0;
30
+ exports.YacoinEsploraTypes = __importStar(require("./chain/esplora/types"));
31
+ __exportStar(require("./swap/types"), exports);
32
+ var AddressType;
33
+ (function (AddressType) {
34
+ AddressType["LEGACY"] = "legacy";
35
+ })(AddressType = exports.AddressType || (exports.AddressType = {}));
36
+ var SwapMode;
37
+ (function (SwapMode) {
38
+ SwapMode["P2SH"] = "p2sh";
39
+ })(SwapMode = exports.SwapMode || (exports.SwapMode = {}));
40
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../lib/types.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,4EAA4D;AAC5D,+CAA6B;AAsE7B,IAAY,WAEX;AAFD,WAAY,WAAW;IACnB,gCAAiB,CAAA;AACrB,CAAC,EAFW,WAAW,GAAX,mBAAW,KAAX,mBAAW,QAEtB;AAED,IAAY,QAEX;AAFD,WAAY,QAAQ;IAChB,yBAAa,CAAA;AACjB,CAAC,EAFW,QAAQ,GAAR,gBAAQ,KAAR,gBAAQ,QAEnB"}