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

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.3",
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.3",
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.3",
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,19 @@ 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
+ purposes: ['payment']
57
+ }
58
+ }, () => { });
59
+ if (res.error) {
60
+ console.error(res.error);
61
+ throw res.error;
62
+ }
63
+ return res.result[0].publicKey;
64
+ }
26
65
  async transfer(recipient, amount, fromAddress, memo, options) {
27
66
  const hashPromise = new Promise((resolve, reject) => {
28
67
  this.wallet.request({
@@ -44,8 +83,9 @@ class BitcoinAdapter extends abstract_adapter_1.AbstractAdapter {
44
83
  reject(error);
45
84
  }
46
85
  else {
47
- options?.onTransactionHash?.(txHash);
48
- resolve(txHash);
86
+ const hash = txHash;
87
+ options?.onTransactionHash?.(hash);
88
+ resolve(hash);
49
89
  }
50
90
  });
51
91
  });
@@ -61,5 +101,39 @@ class BitcoinAdapter extends abstract_adapter_1.AbstractAdapter {
61
101
  throw new Error('Failed to transfer funds');
62
102
  }
63
103
  }
104
+ async sendPsbtTransaction(psbt, userAddress, inputIndexes, options) {
105
+ const hashPromise = new Promise((resolve, reject) => {
106
+ this.wallet.request({
107
+ method: 'sign_psbt',
108
+ params: {
109
+ psbt,
110
+ signInputs: {
111
+ [userAddress]: inputIndexes
112
+ },
113
+ allowedSignHash: 1,
114
+ broadcast: true
115
+ }
116
+ }, (error, txHash) => {
117
+ if (error) {
118
+ reject(error);
119
+ }
120
+ else {
121
+ const txData = txHash;
122
+ options?.onTransactionHash?.(txData.result.txId);
123
+ resolve(txData.result.txId);
124
+ }
125
+ });
126
+ });
127
+ try {
128
+ const hash = await hashPromise;
129
+ if (typeof hash === 'string') {
130
+ return hash;
131
+ }
132
+ throw new Error();
133
+ }
134
+ catch {
135
+ throw new Error('Failed to sign psbt transaction');
136
+ }
137
+ }
64
138
  }
65
139
  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
  }>;
@@ -7,10 +7,8 @@ import { TonAdapter } from './adapters/ton-adapter';
7
7
  import { TonAdapterConfig } from './adapters/models/ton-adapter-config';
8
8
  import { EvmAdapter } from './adapters/evm-adapter';
9
9
  import { BitcoinAdapter } from './adapters/bitcoin-adapter';
10
- import { TeleswapSDK } from '@teleportdao/teleswap-sdk';
11
10
  export declare class BlockchainAdapterFactoryService {
12
11
  private readonly rpcList;
13
- readonly teleSwapSdk: TeleswapSDK;
14
12
  private readonly createLogger?;
15
13
  private readonly httpClient?;
16
14
  private readonly tonParams?;
@@ -26,5 +24,4 @@ export declare class BlockchainAdapterFactoryService {
26
24
  private createStorage;
27
25
  private createAdapter;
28
26
  connectWallet(walletProvider: WalletProvider): void;
29
- private static createTeleSwapSdkInstance;
30
27
  }
@@ -9,11 +9,9 @@ const core_1 = require("@cryptorubic/core");
9
9
  const ton_adapter_1 = require("./adapters/ton-adapter");
10
10
  const evm_adapter_1 = require("./adapters/evm-adapter");
11
11
  const bitcoin_adapter_1 = require("./adapters/bitcoin-adapter");
12
- const teleswap_sdk_1 = require("@teleportdao/teleswap-sdk");
13
12
  class BlockchainAdapterFactoryService {
14
- constructor(rpcList, teleSwapSdk, createLogger, httpClient, tonParams) {
13
+ constructor(rpcList, createLogger, httpClient, tonParams) {
15
14
  this.rpcList = rpcList;
16
- this.teleSwapSdk = teleSwapSdk;
17
15
  this.createLogger = createLogger;
18
16
  this.httpClient = httpClient;
19
17
  this.tonParams = tonParams;
@@ -27,8 +25,7 @@ class BlockchainAdapterFactoryService {
27
25
  // @TODO Add default logger
28
26
  const loggerFn = createLogger || undefined;
29
27
  const resolvedHttpClient = httpClient ?? (await Promise.resolve().then(() => require('axios')));
30
- const teleSwapSdk = await BlockchainAdapterFactoryService.createTeleSwapSdkInstance();
31
- return new this(rpcList, teleSwapSdk, createLogger, resolvedHttpClient, tonParams);
28
+ return new this(rpcList, createLogger, resolvedHttpClient, tonParams);
32
29
  }
33
30
  getAdapter(blockchain) {
34
31
  const adapter = this.adapterStore?.[blockchain];
@@ -132,10 +129,5 @@ class BlockchainAdapterFactoryService {
132
129
  store.walletAddress = walletProvider[core_1.CHAIN_TYPE.BITCOIN].address;
133
130
  }
134
131
  }
135
- static async createTeleSwapSdkInstance() {
136
- const teleSwapSdk = new teleswap_sdk_1.TeleswapSDK();
137
- await teleSwapSdk.initNetworksConnection();
138
- return teleSwapSdk;
139
- }
140
132
  }
141
133
  exports.BlockchainAdapterFactoryService = BlockchainAdapterFactoryService;