@cryptorubic/web3 0.5.2-alpha-teleswap.1 → 0.5.2-alpha-teleswap.2

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 CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@cryptorubic/web3",
3
- "version": "0.5.2-alpha-teleswap.1",
3
+ "version": "0.5.2-alpha-teleswap.2",
4
4
  "dependencies": {
5
5
  "tslib": "^2.3.0",
6
6
  "bignumber.js": "9.1.2",
7
- "@cryptorubic/core": "0.5.2-alpha-teleswap.1",
7
+ "@cryptorubic/core": "0.5.2-alpha-teleswap.2",
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.5.2-alpha-teleswap.1",
14
+ "@cryptorubic/tron-types": "0.5.2-alpha-teleswap.2",
15
15
  "bitcoin-address-validation": "^2.2.3",
16
16
  "axios": "0.27.2",
17
17
  "crc-32": "^1.2.2",
@@ -7,8 +7,11 @@ 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
  callForTokensInfo(tokenAddresses: string[] | ReadonlyArray<string>): Promise<Token<BlockchainName>[]>;
11
12
  read<T>(args: unknown): Promise<T>;
12
13
  write(args: unknown): Promise<string>;
14
+ getPublicKeyFromWallet(): Promise<string>;
13
15
  transfer(recipient: string, amount: string, fromAddress: string, memo?: string, options?: BasicTransactionOptions): Promise<string>;
16
+ sendPsbtTransaction(psbt: string, userAddress: string, inputIndexes: number[], options?: BasicTransactionOptions): Promise<string>;
14
17
  }
@@ -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 callForTokensInfo(tokenAddresses) {
18
44
  return [core_1.nativeTokensList[core_1.BLOCKCHAIN_NAME.BITCOIN]];
19
45
  }
@@ -23,6 +49,20 @@ class BitcoinAdapter extends abstract_adapter_1.AbstractAdapter {
23
49
  write(args) {
24
50
  throw new Error('Not implemented');
25
51
  }
52
+ async getPublicKeyFromWallet() {
53
+ const res = await this.wallet.request({
54
+ method: 'request_accounts_and_keys',
55
+ params: {
56
+ //@ts-ignore
57
+ purposes: ['payment'],
58
+ },
59
+ }, () => { });
60
+ if (res.error) {
61
+ console.error(res.error);
62
+ throw res.error;
63
+ }
64
+ return res.result[0]?.publicKey;
65
+ }
26
66
  async transfer(recipient, amount, fromAddress, memo, options) {
27
67
  const hashPromise = new Promise((resolve, reject) => {
28
68
  this.wallet.request({
@@ -34,18 +74,19 @@ class BitcoinAdapter extends abstract_adapter_1.AbstractAdapter {
34
74
  recipient,
35
75
  amount: {
36
76
  amount,
37
- decimals: 8
77
+ decimals: 8,
38
78
  },
39
- ...(memo && { memo })
40
- }
41
- ]
79
+ ...(memo && { memo }),
80
+ },
81
+ ],
42
82
  }, (error, txHash) => {
43
83
  if (error) {
44
84
  reject(error);
45
85
  }
46
86
  else {
47
- options?.onTransactionHash?.(txHash);
48
- resolve(txHash);
87
+ const hash = txHash;
88
+ options?.onTransactionHash?.(hash);
89
+ resolve(hash);
49
90
  }
50
91
  });
51
92
  });
@@ -61,5 +102,39 @@ class BitcoinAdapter extends abstract_adapter_1.AbstractAdapter {
61
102
  throw new Error('Failed to transfer funds');
62
103
  }
63
104
  }
105
+ async sendPsbtTransaction(psbt, userAddress, inputIndexes, options) {
106
+ const hashPromise = new Promise((resolve, reject) => {
107
+ this.wallet.request({
108
+ method: 'sign_psbt',
109
+ params: {
110
+ psbt,
111
+ signInputs: {
112
+ [userAddress]: inputIndexes,
113
+ },
114
+ allowedSignHash: 1,
115
+ broadcast: true,
116
+ },
117
+ }, (error, txHash) => {
118
+ if (error) {
119
+ reject(error);
120
+ }
121
+ else {
122
+ const txData = txHash;
123
+ options?.onTransactionHash?.(txData.result.txId);
124
+ resolve(txData.result.txId);
125
+ }
126
+ });
127
+ });
128
+ try {
129
+ const hash = await hashPromise;
130
+ if (typeof hash === 'string') {
131
+ return hash;
132
+ }
133
+ throw new Error();
134
+ }
135
+ catch {
136
+ throw new Error('Failed to sign psbt transaction');
137
+ }
138
+ }
64
139
  }
65
140
  exports.BitcoinAdapter = BitcoinAdapter;
@@ -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
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -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
  }>;