@cryptorubic/web3 0.7.5-alpha.rpc-error.1 → 0.7.6
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 +3 -3
- package/src/lib/adapter/adapters/bitcoin-adapter.d.ts +3 -0
- package/src/lib/adapter/adapters/bitcoin-adapter.js +76 -2
- package/src/lib/adapter/adapters/evm-adapter.js +1 -3
- package/src/lib/adapter/adapters/models/bitcoin-user-address-info.d.ts +53 -0
- package/src/lib/adapter/adapters/models/bitcoin-user-address-info.js +2 -0
- package/src/lib/adapter/adapters/models/btc-wallet-provider.d.ts +2 -2
package/package.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cryptorubic/web3",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.6",
|
|
4
4
|
"dependencies": {
|
|
5
5
|
"tslib": "^2.3.0",
|
|
6
6
|
"bignumber.js": "9.1.2",
|
|
7
|
-
"@cryptorubic/core": "0.7.
|
|
7
|
+
"@cryptorubic/core": "0.7.6",
|
|
8
8
|
"viem": "^2.19.1",
|
|
9
9
|
"web3-utils": "^4.3.1",
|
|
10
10
|
"@ton/ton": "^15.1.0",
|
|
11
11
|
"@solana/web3.js": "1.95.3",
|
|
12
12
|
"@solflare-wallet/utl-sdk": "^1.4.0",
|
|
13
13
|
"@ethersproject/bignumber": "^5.7.0",
|
|
14
|
-
"@cryptorubic/tron-types": "0.7.
|
|
14
|
+
"@cryptorubic/tron-types": "0.7.6",
|
|
15
15
|
"bitcoin-address-validation": "^2.2.3",
|
|
16
16
|
"axios": "0.27.2",
|
|
17
17
|
"crc-32": "^1.2.2",
|
|
@@ -7,9 +7,12 @@ export declare class BitcoinAdapter extends AbstractAdapter<BtcWallet, BtcWallet
|
|
|
7
7
|
private readonly httpClient;
|
|
8
8
|
constructor(httpClient: HttpClient, logger?: ICustomLogger);
|
|
9
9
|
getBalance(userAddress: string): Promise<BigNumber>;
|
|
10
|
+
getPublicKey(userAddress: string): Promise<string | null>;
|
|
10
11
|
checkEnoughBalance(token: TokenAmount | PriceTokenAmount, walletAddress: string): Promise<boolean>;
|
|
11
12
|
callForTokensInfo(tokenAddresses: string[] | ReadonlyArray<string>): Promise<Token<BlockchainName>[]>;
|
|
12
13
|
read<T>(args: unknown): Promise<T>;
|
|
13
14
|
write(args: unknown): Promise<string>;
|
|
15
|
+
getPublicKeyFromWallet(): Promise<string>;
|
|
14
16
|
transfer(recipient: string, amount: string, fromAddress: string, memo?: string, options?: BasicTransactionOptions): Promise<string>;
|
|
17
|
+
sendPsbtTransaction(psbt: string, userAddress: string, inputIndexes: number[], options?: BasicTransactionOptions): Promise<string>;
|
|
15
18
|
}
|
|
@@ -14,6 +14,32 @@ class BitcoinAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
14
14
|
const response = await this.httpClient.get(url);
|
|
15
15
|
return new bignumber_js_1.default(response.final_balance);
|
|
16
16
|
}
|
|
17
|
+
async getPublicKey(userAddress) {
|
|
18
|
+
try {
|
|
19
|
+
const url = `https://api.blockcypher.com/v1/btc/main/addrs/${userAddress}/full`;
|
|
20
|
+
const response = await this.httpClient.get(url);
|
|
21
|
+
const txs = response.txs;
|
|
22
|
+
let publicKey = null;
|
|
23
|
+
for (const txData of txs) {
|
|
24
|
+
const userInputData = txData.inputs.find((inputData) => {
|
|
25
|
+
const isInputFromUserAddress = inputData.addresses.includes(userAddress);
|
|
26
|
+
const publicKey = inputData.witness?.[1];
|
|
27
|
+
return isInputFromUserAddress && publicKey;
|
|
28
|
+
});
|
|
29
|
+
if (userInputData) {
|
|
30
|
+
publicKey = userInputData.witness[1];
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (!publicKey) {
|
|
35
|
+
throw new Error();
|
|
36
|
+
}
|
|
37
|
+
return publicKey;
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
17
43
|
async checkEnoughBalance(token, walletAddress) {
|
|
18
44
|
const balance = await this.getBalance(walletAddress);
|
|
19
45
|
return balance.gte(token.tokenAmount);
|
|
@@ -27,6 +53,19 @@ class BitcoinAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
27
53
|
write(args) {
|
|
28
54
|
throw new Error('Not implemented');
|
|
29
55
|
}
|
|
56
|
+
async getPublicKeyFromWallet() {
|
|
57
|
+
const res = await this.wallet.request({
|
|
58
|
+
method: 'request_accounts_and_keys',
|
|
59
|
+
params: {
|
|
60
|
+
purposes: ['payment']
|
|
61
|
+
}
|
|
62
|
+
}, () => { });
|
|
63
|
+
if (res.error) {
|
|
64
|
+
console.error(res.error);
|
|
65
|
+
throw res.error;
|
|
66
|
+
}
|
|
67
|
+
return res.result[0].publicKey;
|
|
68
|
+
}
|
|
30
69
|
async transfer(recipient, amount, fromAddress, memo, options) {
|
|
31
70
|
const hashPromise = new Promise((resolve, reject) => {
|
|
32
71
|
this.wallet.request({
|
|
@@ -48,8 +87,9 @@ class BitcoinAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
48
87
|
reject(error);
|
|
49
88
|
}
|
|
50
89
|
else {
|
|
51
|
-
|
|
52
|
-
|
|
90
|
+
const hash = txHash;
|
|
91
|
+
options?.onTransactionHash?.(hash);
|
|
92
|
+
resolve(hash);
|
|
53
93
|
}
|
|
54
94
|
});
|
|
55
95
|
});
|
|
@@ -65,5 +105,39 @@ class BitcoinAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
65
105
|
throw new Error('Failed to transfer funds');
|
|
66
106
|
}
|
|
67
107
|
}
|
|
108
|
+
async sendPsbtTransaction(psbt, userAddress, inputIndexes, options) {
|
|
109
|
+
const hashPromise = new Promise((resolve, reject) => {
|
|
110
|
+
this.wallet.request({
|
|
111
|
+
method: 'sign_psbt',
|
|
112
|
+
params: {
|
|
113
|
+
psbt,
|
|
114
|
+
signInputs: {
|
|
115
|
+
[userAddress]: inputIndexes
|
|
116
|
+
},
|
|
117
|
+
allowedSignHash: 1,
|
|
118
|
+
broadcast: true
|
|
119
|
+
}
|
|
120
|
+
}, (error, txHash) => {
|
|
121
|
+
if (error) {
|
|
122
|
+
reject(error);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
const txData = txHash;
|
|
126
|
+
options?.onTransactionHash?.(txData.result.txId);
|
|
127
|
+
resolve(txData.result.txId);
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
try {
|
|
132
|
+
const hash = await hashPromise;
|
|
133
|
+
if (typeof hash === 'string') {
|
|
134
|
+
return hash;
|
|
135
|
+
}
|
|
136
|
+
throw new Error();
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
throw new Error('Failed to sign psbt transaction');
|
|
140
|
+
}
|
|
141
|
+
}
|
|
68
142
|
}
|
|
69
143
|
exports.BitcoinAdapter = BitcoinAdapter;
|
|
@@ -163,9 +163,7 @@ class EvmAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
163
163
|
return ['decimals', 'symbol', 'name'].map((method) => ({ ...contract, functionName: method }));
|
|
164
164
|
});
|
|
165
165
|
const results = contracts.length
|
|
166
|
-
? (await this.multicallByContract({
|
|
167
|
-
contracts
|
|
168
|
-
}, false))
|
|
166
|
+
? (await this.multicallByContract({ contracts }, false))
|
|
169
167
|
: [];
|
|
170
168
|
const tokens = [];
|
|
171
169
|
for (let i = 0; i < results.length; i += 3) {
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export interface BitcoinTxInput {
|
|
2
|
+
prev_hash: string;
|
|
3
|
+
output_index: number;
|
|
4
|
+
output_value: number;
|
|
5
|
+
sequence: number;
|
|
6
|
+
addresses: string[];
|
|
7
|
+
script_type: string;
|
|
8
|
+
age?: number;
|
|
9
|
+
witness: string[];
|
|
10
|
+
}
|
|
11
|
+
export interface BitcoinTxOutput {
|
|
12
|
+
value: number;
|
|
13
|
+
script: string;
|
|
14
|
+
addresses: string[];
|
|
15
|
+
script_type: string;
|
|
16
|
+
spent_by?: string;
|
|
17
|
+
}
|
|
18
|
+
export interface BitcoinTxInfo {
|
|
19
|
+
block_hash?: string;
|
|
20
|
+
block_height: number;
|
|
21
|
+
block_index: number;
|
|
22
|
+
hash: string;
|
|
23
|
+
addresses: string[];
|
|
24
|
+
total: number;
|
|
25
|
+
fees: number;
|
|
26
|
+
size: number;
|
|
27
|
+
vsize: number;
|
|
28
|
+
preference: string;
|
|
29
|
+
relayed_by: string;
|
|
30
|
+
confirmed?: string;
|
|
31
|
+
received: string;
|
|
32
|
+
ver: number;
|
|
33
|
+
double_spend: boolean;
|
|
34
|
+
vin_sz: number;
|
|
35
|
+
vout_sz: number;
|
|
36
|
+
confirmations?: number;
|
|
37
|
+
confidence?: number;
|
|
38
|
+
inputs: BitcoinTxInput[];
|
|
39
|
+
outputs: BitcoinTxOutput[];
|
|
40
|
+
opt_in_rbf?: boolean;
|
|
41
|
+
}
|
|
42
|
+
export interface BitcoinUserAddressInfo {
|
|
43
|
+
address: string;
|
|
44
|
+
total_received: number;
|
|
45
|
+
total_sent: number;
|
|
46
|
+
balance: number;
|
|
47
|
+
unconfirmed_balance: number;
|
|
48
|
+
final_balance: number;
|
|
49
|
+
n_tx: number;
|
|
50
|
+
unconfirmed_n_tx: number;
|
|
51
|
+
final_n_tx: number;
|
|
52
|
+
txs: BitcoinTxInfo[];
|
|
53
|
+
}
|
|
@@ -2,8 +2,8 @@ export interface BtcWallet {
|
|
|
2
2
|
on: (event: string, callback: (...args: unknown[]) => void) => unknown;
|
|
3
3
|
request<T>(args: {
|
|
4
4
|
method: string;
|
|
5
|
-
params: unknown[];
|
|
6
|
-
}, fn: (error: Error, txHash: string) => unknown): Promise<{
|
|
5
|
+
params: unknown[] | {};
|
|
6
|
+
}, fn: (error: Error, txHash: string | {}) => unknown): Promise<{
|
|
7
7
|
error: null | Error;
|
|
8
8
|
result: T;
|
|
9
9
|
}>;
|