@rsksmart/powpeg-sdk 0.0.1 → 1.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.
- package/LICENSE +21 -0
- package/README.md +5 -0
- package/SECURITY.md +140 -0
- package/dist/index.d.mts +269 -0
- package/dist/index.d.ts +269 -0
- package/dist/index.js +812 -0
- package/dist/index.mjs +769 -0
- package/package.json +25 -1
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { Psbt, networks as networks$1 } from 'bitcoinjs-lib';
|
|
2
|
+
import { ethers } from '@rsksmart/bridges-core-sdk';
|
|
3
|
+
import TrezorConnect from '@trezor/connect-web';
|
|
4
|
+
import { CreateTransactionArg } from '@ledgerhq/hw-app-btc/lib/createTransaction';
|
|
5
|
+
|
|
6
|
+
type FeeLevel = 'slow' | 'average' | 'fast';
|
|
7
|
+
interface BitcoinSigner {
|
|
8
|
+
getNonChangeAddresses(bundleSize: number): Promise<string[]>;
|
|
9
|
+
getChangeAddresses(bundleSize: number): Promise<string[]>;
|
|
10
|
+
signTransaction(psbt: Psbt, inputs?: Utxo[], transactions?: string[]): Promise<string>;
|
|
11
|
+
}
|
|
12
|
+
interface BitcoinDataSource {
|
|
13
|
+
getFeeRate(level: FeeLevel): Promise<number>;
|
|
14
|
+
getTxHex(txId: string): Promise<string>;
|
|
15
|
+
getOutputs(address: string): Promise<Utxo[]>;
|
|
16
|
+
broadcast(hexTx: string): Promise<string>;
|
|
17
|
+
getAddressDetails(address: string): Promise<AddressWithDetails>;
|
|
18
|
+
}
|
|
19
|
+
interface AddressWithDetails {
|
|
20
|
+
address: string;
|
|
21
|
+
balance: number;
|
|
22
|
+
txCount: number;
|
|
23
|
+
}
|
|
24
|
+
interface Utxo {
|
|
25
|
+
address: string;
|
|
26
|
+
txid: string;
|
|
27
|
+
amount: bigint;
|
|
28
|
+
vout: number;
|
|
29
|
+
}
|
|
30
|
+
interface PegoutFeeEstimation {
|
|
31
|
+
bitcoinFee: bigint;
|
|
32
|
+
rootstockFee: bigint;
|
|
33
|
+
}
|
|
34
|
+
declare enum TxType {
|
|
35
|
+
PEGIN = "PEGIN",
|
|
36
|
+
PEGOUT = "PEGOUT"
|
|
37
|
+
}
|
|
38
|
+
declare enum PegoutStatuses {
|
|
39
|
+
RECEIVED = "RECEIVED",
|
|
40
|
+
REJECTED = "REJECTED",
|
|
41
|
+
WAITING_FOR_CONFIRMATION = "WAITING_FOR_CONFIRMATION",
|
|
42
|
+
WAITING_FOR_SIGNATURE = "WAITING_FOR_SIGNATURE",
|
|
43
|
+
SIGNED = "SIGNED",
|
|
44
|
+
NOT_FOUND = "NOT_FOUND",
|
|
45
|
+
PENDING = "PENDING",
|
|
46
|
+
NOT_PEGOUT_TX = "NOT_PEGOUT_TX",
|
|
47
|
+
RELEASE_BTC = "RELEASE_BTC"
|
|
48
|
+
}
|
|
49
|
+
declare enum PeginStatuses {
|
|
50
|
+
NOT_IN_BTC_YET = "NOT_IN_BTC_YET",
|
|
51
|
+
WAITING_CONFIRMATIONS = "WAITING_CONFIRMATIONS",
|
|
52
|
+
NOT_IN_RSK_YET = "NOT_IN_RSK_YET",
|
|
53
|
+
CONFIRMED = "CONFIRMED",
|
|
54
|
+
REJECTED_NO_REFUND = "REJECTED_NO_REFUND",
|
|
55
|
+
REJECTED_REFUND = "REJECTED_REFUND",
|
|
56
|
+
ERROR_NOT_A_PEGIN = "ERROR_NOT_A_PEGIN",
|
|
57
|
+
ERROR_BELOW_MIN = "ERROR_BELOW_MIN",
|
|
58
|
+
ERROR_UNEXPECTED = "ERROR_UNEXPECTED"
|
|
59
|
+
}
|
|
60
|
+
declare const RejectedPegoutReasons: {
|
|
61
|
+
readonly 1: "LOW_AMOUNT";
|
|
62
|
+
readonly 2: "CALLER_CONTRACT";
|
|
63
|
+
readonly 3: "FEE_ABOVE_VALUE";
|
|
64
|
+
};
|
|
65
|
+
type RejectedPegoutReason = (typeof RejectedPegoutReasons)[keyof typeof RejectedPegoutReasons];
|
|
66
|
+
interface PeginTxDetails {
|
|
67
|
+
btc: {
|
|
68
|
+
txId: string;
|
|
69
|
+
creationDate: string;
|
|
70
|
+
federationAddress: string;
|
|
71
|
+
amountTransferred: number;
|
|
72
|
+
fees: number;
|
|
73
|
+
refundAddress: string;
|
|
74
|
+
confirmations: number;
|
|
75
|
+
requiredConfirmation: number;
|
|
76
|
+
btcWTxId: string;
|
|
77
|
+
senderAddress: string;
|
|
78
|
+
};
|
|
79
|
+
rsk: {
|
|
80
|
+
recipientAddress: string;
|
|
81
|
+
};
|
|
82
|
+
status: PeginStatuses;
|
|
83
|
+
}
|
|
84
|
+
interface PegoutTxDetails {
|
|
85
|
+
originatingRskTxHash: string;
|
|
86
|
+
rskTxHash: string;
|
|
87
|
+
rskSenderAddress: string;
|
|
88
|
+
btcRecipientAddress: string;
|
|
89
|
+
valueRequestedInSatoshis: number;
|
|
90
|
+
valueInSatoshisToBeReceived: number;
|
|
91
|
+
feeInSatoshisToBePaid: number;
|
|
92
|
+
status: PegoutStatuses;
|
|
93
|
+
btcRawTransaction: string;
|
|
94
|
+
reason?: RejectedPegoutReason;
|
|
95
|
+
}
|
|
96
|
+
interface PegoutStatusData {
|
|
97
|
+
type: TxType.PEGOUT;
|
|
98
|
+
txDetails: PegoutTxDetails;
|
|
99
|
+
}
|
|
100
|
+
interface PeginStatusData {
|
|
101
|
+
type: TxType.PEGIN;
|
|
102
|
+
txDetails: PeginTxDetails;
|
|
103
|
+
}
|
|
104
|
+
type StatusData = PegoutStatusData | PeginStatusData;
|
|
105
|
+
interface UnsignedPegin {
|
|
106
|
+
psbt: Psbt;
|
|
107
|
+
inputs: Utxo[];
|
|
108
|
+
transactions: string[];
|
|
109
|
+
fee: number;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
declare const supportedAddressTypes: {
|
|
113
|
+
readonly 'NATIVE SEGWIT': {
|
|
114
|
+
readonly path: "84";
|
|
115
|
+
readonly format: "bech32";
|
|
116
|
+
};
|
|
117
|
+
readonly SEGWIT: {
|
|
118
|
+
readonly path: "49";
|
|
119
|
+
readonly format: "p2sh";
|
|
120
|
+
};
|
|
121
|
+
readonly LEGACY: {
|
|
122
|
+
readonly path: "44";
|
|
123
|
+
readonly format: "legacy";
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
declare const networks: {
|
|
127
|
+
readonly TEST: {
|
|
128
|
+
readonly currency: "bitcoin_testnet";
|
|
129
|
+
readonly coin: "1";
|
|
130
|
+
readonly lib: networks$1.Network;
|
|
131
|
+
readonly xpubVersion: 70617039;
|
|
132
|
+
};
|
|
133
|
+
readonly MAIN: {
|
|
134
|
+
readonly currency: "bitcoin";
|
|
135
|
+
readonly coin: "0";
|
|
136
|
+
readonly lib: networks$1.Network;
|
|
137
|
+
readonly xpubVersion: 76067358;
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
type AddressType = keyof typeof supportedAddressTypes;
|
|
141
|
+
type Network = keyof typeof networks;
|
|
142
|
+
|
|
143
|
+
declare class PowPegSDK {
|
|
144
|
+
private _bitcoinSigner;
|
|
145
|
+
private _bitcoinDataSource;
|
|
146
|
+
private network;
|
|
147
|
+
private maxBundleSize;
|
|
148
|
+
private burnDustValue;
|
|
149
|
+
private txHeaderSizeInBytes;
|
|
150
|
+
private txOutputSizeInBytes;
|
|
151
|
+
private txInputSizeInBytes;
|
|
152
|
+
private pegInOutputs;
|
|
153
|
+
private powpegRsktHeader;
|
|
154
|
+
private burnDustMaxValue;
|
|
155
|
+
private utxos;
|
|
156
|
+
private changeAddress?;
|
|
157
|
+
private minPeginAmount;
|
|
158
|
+
private peginFeeEstimationInputs;
|
|
159
|
+
private minPegoutAmount;
|
|
160
|
+
private bitcoinJsNetwork;
|
|
161
|
+
private bridge;
|
|
162
|
+
private api;
|
|
163
|
+
private rskProvider;
|
|
164
|
+
private publicNodes;
|
|
165
|
+
/**
|
|
166
|
+
* @param {BitcoinSigner | null} _bitcoinSigner - An instance of a class that implements the BitcoinSigner interface.
|
|
167
|
+
* @param {BitcoinDataSource | null} _bitcoinDataSource - An instance of a class that implements the BitcoinDataSource interface or null if you won't use peg-in operations.
|
|
168
|
+
* @param {Network} network - The network to use. Either 'MAIN' or 'TEST'.
|
|
169
|
+
* @param {string} rpcProviderUrl - URL of either your own Rootstock node, the Rootstock RPC API or a third-party node provider. If not provided, it will default to the Rootstock public node for the specified network.
|
|
170
|
+
* @param {string} apiUrl - The URL of the API to use. If not provided, it will default to the production 2WP API URL for the specified network and use it as BitcoinDataSource.
|
|
171
|
+
* @param {number} maxBundleSize - The maximum number of addresses to ask for while creating a peg-in transaction. Defaults to 10.
|
|
172
|
+
* @param {number} burnDustValue - The value in satoshis to consider as dust to burn. Defaults to 2000.
|
|
173
|
+
*/
|
|
174
|
+
constructor(_bitcoinSigner: BitcoinSigner | null, _bitcoinDataSource: BitcoinDataSource | null, network: Network, rpcProviderUrl?: string, apiUrl?: string, maxBundleSize?: number, burnDustValue?: number);
|
|
175
|
+
private get bitcoinSigner();
|
|
176
|
+
private set bitcoinSigner(value);
|
|
177
|
+
private get bitcoinDataSource();
|
|
178
|
+
private getUtxos;
|
|
179
|
+
private getAddressesWithDetails;
|
|
180
|
+
private groupAddressesByUsage;
|
|
181
|
+
private groupAddressesByBalance;
|
|
182
|
+
private initPegin;
|
|
183
|
+
private getRskOutput;
|
|
184
|
+
estimatePeginFee(amount: bigint, feeLevel?: FeeLevel): Promise<number>;
|
|
185
|
+
createPegin(amount: bigint, recipientAddress: string): Promise<Psbt>;
|
|
186
|
+
private selectInputs;
|
|
187
|
+
private validatePeginAmount;
|
|
188
|
+
private calculatePeginFee;
|
|
189
|
+
private calculateFeeAndSelectedInputs;
|
|
190
|
+
fundPegin(psbt: Psbt, feeLevel?: FeeLevel): Promise<{
|
|
191
|
+
psbt: Psbt;
|
|
192
|
+
inputs: Utxo[];
|
|
193
|
+
transactions: string[];
|
|
194
|
+
fee: number;
|
|
195
|
+
}>;
|
|
196
|
+
createAndFundPegin(amount: bigint, recipientAddress: string, signer: BitcoinSigner, feeLevel?: FeeLevel): Promise<UnsignedPegin>;
|
|
197
|
+
private signPegin;
|
|
198
|
+
signAndBroadcastPegin(psbt: Psbt, inputs?: Utxo[], transactions?: string[]): Promise<string>;
|
|
199
|
+
private validateMinimumPegoutAmount;
|
|
200
|
+
private createPegoutTransaction;
|
|
201
|
+
estimatePegoutFees(amount: string, fromAddress?: string): Promise<PegoutFeeEstimation>;
|
|
202
|
+
createPegout(amount: string, senderAccount: string): Promise<{
|
|
203
|
+
tx: {
|
|
204
|
+
from: string;
|
|
205
|
+
to: string;
|
|
206
|
+
value: string;
|
|
207
|
+
};
|
|
208
|
+
rootstockFee: bigint;
|
|
209
|
+
bitcoinFee: bigint;
|
|
210
|
+
}>;
|
|
211
|
+
signAndBroadcastPegout(tx: {
|
|
212
|
+
from: string;
|
|
213
|
+
to: string;
|
|
214
|
+
value: string;
|
|
215
|
+
}, signer: ethers.Signer): Promise<ethers.providers.TransactionReceipt | undefined>;
|
|
216
|
+
getTransactionStatus<T extends TxType>(txHash: string, txType: T): Promise<Extract<PegoutStatusData, {
|
|
217
|
+
type: T;
|
|
218
|
+
}> | Extract<PeginStatusData, {
|
|
219
|
+
type: T;
|
|
220
|
+
}>>;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
declare class TrezorSigner implements BitcoinSigner {
|
|
224
|
+
private readonly network;
|
|
225
|
+
private _addressType;
|
|
226
|
+
private readonly bitcoinTxVersion;
|
|
227
|
+
private readonly addresses;
|
|
228
|
+
private constructor();
|
|
229
|
+
static init(network?: Network, initOptions?: typeof TrezorConnect.init): Promise<TrezorSigner>;
|
|
230
|
+
static reinit(): Promise<TrezorSigner>;
|
|
231
|
+
set addressType(addressType: AddressType);
|
|
232
|
+
get addressType(): AddressType;
|
|
233
|
+
private getPathPurpose;
|
|
234
|
+
private getPathCoin;
|
|
235
|
+
private getAddresses;
|
|
236
|
+
getChangeAddresses(bundleSize: number): Promise<string[]>;
|
|
237
|
+
getNonChangeAddresses(bundleSize: number): Promise<string[]>;
|
|
238
|
+
private getScriptType;
|
|
239
|
+
private getInputs;
|
|
240
|
+
private getOutputs;
|
|
241
|
+
signTransaction(psbt: Psbt, utxos: Utxo[]): Promise<string>;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
declare class LedgerSigner implements BitcoinSigner {
|
|
245
|
+
private readonly connection;
|
|
246
|
+
private readonly transport;
|
|
247
|
+
private readonly network;
|
|
248
|
+
private _addressType;
|
|
249
|
+
private readonly addresses;
|
|
250
|
+
private readonly transportService;
|
|
251
|
+
private constructor();
|
|
252
|
+
static init(network?: Network): Promise<LedgerSigner>;
|
|
253
|
+
reinit(): Promise<LedgerSigner>;
|
|
254
|
+
set addressType(addressType: AddressType);
|
|
255
|
+
get addressType(): AddressType;
|
|
256
|
+
private getPathPurpose;
|
|
257
|
+
private getPathCoin;
|
|
258
|
+
private getAddressFormat;
|
|
259
|
+
private getXpubVersion;
|
|
260
|
+
private isSegwit;
|
|
261
|
+
getAddresses(bundleSize: number, change?: boolean): Promise<string[]>;
|
|
262
|
+
getChangeAddresses(bundleSize: number): Promise<string[]>;
|
|
263
|
+
getNonChangeAddresses(bundleSize: number): Promise<string[]>;
|
|
264
|
+
getInputs(inputs: Utxo[], transactions: string[]): CreateTransactionArg['inputs'];
|
|
265
|
+
getOutputScriptHex(psbt: Psbt): string;
|
|
266
|
+
signTransaction(psbt: Psbt, inputs: Utxo[], transactions: string[]): Promise<string>;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export { type AddressWithDetails, type BitcoinDataSource, type BitcoinSigner, type FeeLevel, LedgerSigner, type PeginStatusData, PeginStatuses, type PeginTxDetails, type PegoutFeeEstimation, type PegoutStatusData, PegoutStatuses, type PegoutTxDetails, PowPegSDK, type RejectedPegoutReason, RejectedPegoutReasons, type StatusData, TrezorSigner, TxType, type UnsignedPegin, type Utxo };
|