@t402/wdk 2.3.0 → 2.4.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/README.md +8 -0
- package/dist/cjs/adapters/index.d.ts +5 -0
- package/dist/cjs/adapters/index.js +455 -0
- package/dist/cjs/adapters/index.js.map +1 -0
- package/dist/cjs/adapters/svm-adapter.d.ts +125 -0
- package/dist/cjs/adapters/svm-adapter.js +132 -0
- package/dist/cjs/adapters/svm-adapter.js.map +1 -0
- package/dist/cjs/adapters/ton-adapter.d.ts +139 -0
- package/dist/cjs/adapters/ton-adapter.js +152 -0
- package/dist/cjs/adapters/ton-adapter.js.map +1 -0
- package/dist/cjs/adapters/tron-adapter.d.ts +139 -0
- package/dist/cjs/adapters/tron-adapter.js +221 -0
- package/dist/cjs/adapters/tron-adapter.js.map +1 -0
- package/dist/cjs/index.d.ts +292 -217
- package/dist/cjs/index.js +1042 -23
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types-V7c-qhn6.d.ts +489 -0
- package/dist/esm/adapters/index.d.mts +5 -0
- package/dist/esm/adapters/index.mjs +21 -0
- package/dist/esm/adapters/index.mjs.map +1 -0
- package/dist/esm/adapters/svm-adapter.d.mts +125 -0
- package/dist/esm/adapters/svm-adapter.mjs +9 -0
- package/dist/esm/adapters/svm-adapter.mjs.map +1 -0
- package/dist/esm/adapters/ton-adapter.d.mts +139 -0
- package/dist/esm/adapters/ton-adapter.mjs +9 -0
- package/dist/esm/adapters/ton-adapter.mjs.map +1 -0
- package/dist/esm/adapters/tron-adapter.d.mts +139 -0
- package/dist/esm/adapters/tron-adapter.mjs +9 -0
- package/dist/esm/adapters/tron-adapter.mjs.map +1 -0
- package/dist/esm/chunk-HB2DGKQ3.mjs +196 -0
- package/dist/esm/chunk-HB2DGKQ3.mjs.map +1 -0
- package/dist/esm/chunk-MCFHZSF7.mjs +107 -0
- package/dist/esm/chunk-MCFHZSF7.mjs.map +1 -0
- package/dist/esm/chunk-YWBJJV5M.mjs +117 -0
- package/dist/esm/chunk-YWBJJV5M.mjs.map +1 -0
- package/dist/esm/index.d.mts +292 -217
- package/dist/esm/index.mjs +640 -23
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/types-V7c-qhn6.d.mts +489 -0
- package/package.json +70 -6
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { a as WDKTonAccount } from '../types-V7c-qhn6.mjs';
|
|
2
|
+
import 'viem';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* TON Signer Adapter for WDK
|
|
6
|
+
*
|
|
7
|
+
* Wraps a Tether WDK TON account to implement T402's ClientTonSigner interface.
|
|
8
|
+
* This allows WDK-managed TON wallets to be used for T402 payments.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* TON Address type (compatible with @ton/core Address)
|
|
13
|
+
* We define our own interface to avoid direct import
|
|
14
|
+
*/
|
|
15
|
+
interface TonAddress {
|
|
16
|
+
toString(): string;
|
|
17
|
+
toRawString(): string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* TON Cell type (compatible with @ton/core Cell)
|
|
21
|
+
* We define our own interface to avoid direct import
|
|
22
|
+
*/
|
|
23
|
+
interface TonCell {
|
|
24
|
+
hash(): Uint8Array;
|
|
25
|
+
toBoc(): Uint8Array;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* SignMessageParams type matching T402's @t402/ton interface
|
|
29
|
+
*/
|
|
30
|
+
interface SignMessageParams {
|
|
31
|
+
/** Destination address */
|
|
32
|
+
to: TonAddress;
|
|
33
|
+
/** Amount of TON to attach (for gas) in nanoTON */
|
|
34
|
+
value: bigint;
|
|
35
|
+
/** Message body (Jetton transfer cell) */
|
|
36
|
+
body: TonCell;
|
|
37
|
+
/** Send mode flags (from @ton/core SendMode) */
|
|
38
|
+
sendMode?: number;
|
|
39
|
+
/** Bounce flag */
|
|
40
|
+
bounce?: boolean;
|
|
41
|
+
/** Message validity timeout in seconds */
|
|
42
|
+
timeout?: number;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* ClientTonSigner interface matching T402's @t402/ton
|
|
46
|
+
*/
|
|
47
|
+
interface ClientTonSigner {
|
|
48
|
+
readonly address: TonAddress;
|
|
49
|
+
signMessage(params: SignMessageParams): Promise<TonCell>;
|
|
50
|
+
getSeqno(): Promise<number>;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* WDKTonSignerAdapter - Adapts a WDK TON account to T402's ClientTonSigner
|
|
54
|
+
*
|
|
55
|
+
* This adapter wraps a Tether WDK TON account and provides T402-compatible
|
|
56
|
+
* signing functionality. The actual message building and signing is delegated
|
|
57
|
+
* to the WDK account, which handles TON-specific details internally.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* const adapter = await createWDKTonSigner(wdkTonAccount);
|
|
62
|
+
* const signed = await adapter.signMessage({
|
|
63
|
+
* to: jettonWalletAddress,
|
|
64
|
+
* value: toNano('0.05'),
|
|
65
|
+
* body: jettonTransferBody,
|
|
66
|
+
* });
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
declare class WDKTonSignerAdapter implements ClientTonSigner {
|
|
70
|
+
private _account;
|
|
71
|
+
private _address;
|
|
72
|
+
private _initialized;
|
|
73
|
+
constructor(account: WDKTonAccount);
|
|
74
|
+
/**
|
|
75
|
+
* Get the wallet address
|
|
76
|
+
* @throws Error if not initialized
|
|
77
|
+
*/
|
|
78
|
+
get address(): TonAddress;
|
|
79
|
+
/**
|
|
80
|
+
* Check if the adapter is initialized
|
|
81
|
+
*/
|
|
82
|
+
get isInitialized(): boolean;
|
|
83
|
+
/**
|
|
84
|
+
* Initialize the adapter by fetching the address
|
|
85
|
+
* Must be called before using the signer
|
|
86
|
+
*/
|
|
87
|
+
initialize(): Promise<void>;
|
|
88
|
+
/**
|
|
89
|
+
* Sign an internal message for Jetton transfer
|
|
90
|
+
*
|
|
91
|
+
* Attempts to build a proper signed Cell using @ton/core if available.
|
|
92
|
+
* Falls back to a simplified wrapper that embeds the raw signature.
|
|
93
|
+
*
|
|
94
|
+
* @param params - Message parameters
|
|
95
|
+
* @returns Signed external message as Cell (BOC)
|
|
96
|
+
*/
|
|
97
|
+
signMessage(params: SignMessageParams): Promise<TonCell>;
|
|
98
|
+
/**
|
|
99
|
+
* Get current seqno for the wallet
|
|
100
|
+
* Used for replay protection
|
|
101
|
+
*/
|
|
102
|
+
getSeqno(): Promise<number>;
|
|
103
|
+
/**
|
|
104
|
+
* Get TON balance in nanoTON
|
|
105
|
+
*/
|
|
106
|
+
getBalance(): Promise<bigint>;
|
|
107
|
+
/**
|
|
108
|
+
* Get Jetton balance
|
|
109
|
+
* @param jettonMaster - Jetton master contract address
|
|
110
|
+
*/
|
|
111
|
+
getJettonBalance(jettonMaster: string): Promise<bigint>;
|
|
112
|
+
/**
|
|
113
|
+
* Get the underlying WDK account
|
|
114
|
+
* Useful for advanced operations not covered by this adapter
|
|
115
|
+
*/
|
|
116
|
+
getWDKAccount(): WDKTonAccount;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Create an initialized WDK TON signer
|
|
120
|
+
*
|
|
121
|
+
* @param account - WDK TON account from @tetherto/wdk-wallet-ton
|
|
122
|
+
* @returns Initialized ClientTonSigner
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```typescript
|
|
126
|
+
* import { T402WDK } from '@t402/wdk';
|
|
127
|
+
*
|
|
128
|
+
* const wallet = new T402WDK(seedPhrase, config);
|
|
129
|
+
* const tonSigner = await wallet.getTonSigner();
|
|
130
|
+
*
|
|
131
|
+
* // Use with T402 client
|
|
132
|
+
* const client = createT402HTTPClient({
|
|
133
|
+
* signers: [{ scheme: 'exact', network: 'ton:mainnet', signer: tonSigner }]
|
|
134
|
+
* });
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
declare function createWDKTonSigner(account: WDKTonAccount): Promise<WDKTonSignerAdapter>;
|
|
138
|
+
|
|
139
|
+
export { type ClientTonSigner, type SignMessageParams, type TonAddress, type TonCell, WDKTonSignerAdapter, createWDKTonSigner };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { b as WDKTronAccount } from '../types-V7c-qhn6.mjs';
|
|
2
|
+
import 'viem';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* TRON Signer Adapter for WDK
|
|
6
|
+
*
|
|
7
|
+
* Wraps a Tether WDK TRON account to implement T402's ClientTronSigner interface.
|
|
8
|
+
* This allows WDK-managed TRON wallets to be used for T402 payments.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* SignTransactionParams matching T402's @t402/tron interface
|
|
13
|
+
*/
|
|
14
|
+
interface SignTransactionParams {
|
|
15
|
+
/** TRC20 contract address */
|
|
16
|
+
contractAddress: string;
|
|
17
|
+
/** Recipient address (T-prefix base58check) */
|
|
18
|
+
to: string;
|
|
19
|
+
/** Amount to transfer (in smallest units) */
|
|
20
|
+
amount: string;
|
|
21
|
+
/** Fee limit in SUN (optional, defaults to 100 TRX) */
|
|
22
|
+
feeLimit?: number;
|
|
23
|
+
/** Transaction expiration time in milliseconds (optional) */
|
|
24
|
+
expiration?: number;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Block info for transaction building
|
|
28
|
+
*/
|
|
29
|
+
interface BlockInfo {
|
|
30
|
+
/** Reference block bytes (hex) */
|
|
31
|
+
refBlockBytes: string;
|
|
32
|
+
/** Reference block hash (hex) */
|
|
33
|
+
refBlockHash: string;
|
|
34
|
+
/** Expiration timestamp in milliseconds */
|
|
35
|
+
expiration: number;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* ClientTronSigner interface matching T402's @t402/tron
|
|
39
|
+
*/
|
|
40
|
+
interface ClientTronSigner {
|
|
41
|
+
readonly address: string;
|
|
42
|
+
signTransaction(params: SignTransactionParams): Promise<string>;
|
|
43
|
+
getBlockInfo(): Promise<BlockInfo>;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* WDKTronSignerAdapter - Adapts a WDK TRON account to T402's ClientTronSigner
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* const adapter = await createWDKTronSigner(wdkTronAccount);
|
|
51
|
+
* const signedTx = await adapter.signTransaction({
|
|
52
|
+
* contractAddress: 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t',
|
|
53
|
+
* to: 'TRecipientAddress...',
|
|
54
|
+
* amount: '1000000', // 1 USDT
|
|
55
|
+
* });
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
declare class WDKTronSignerAdapter implements ClientTronSigner {
|
|
59
|
+
private _account;
|
|
60
|
+
private _address;
|
|
61
|
+
private _initialized;
|
|
62
|
+
private _rpcUrl;
|
|
63
|
+
constructor(account: WDKTronAccount, rpcUrl?: string);
|
|
64
|
+
/**
|
|
65
|
+
* Get the wallet address (T-prefix base58check)
|
|
66
|
+
* @throws Error if not initialized
|
|
67
|
+
*/
|
|
68
|
+
get address(): string;
|
|
69
|
+
/**
|
|
70
|
+
* Check if the adapter is initialized
|
|
71
|
+
*/
|
|
72
|
+
get isInitialized(): boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Initialize the adapter by fetching the address
|
|
75
|
+
* Must be called before using the signer
|
|
76
|
+
*/
|
|
77
|
+
initialize(): Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* Sign a TRC20 transfer transaction
|
|
80
|
+
*
|
|
81
|
+
* This method:
|
|
82
|
+
* 1. Builds a TRC20 transfer transaction
|
|
83
|
+
* 2. Signs it using the WDK account
|
|
84
|
+
* 3. Returns the hex-encoded signed transaction
|
|
85
|
+
*
|
|
86
|
+
* @param params - Transaction parameters
|
|
87
|
+
* @returns Hex-encoded signed transaction
|
|
88
|
+
*/
|
|
89
|
+
signTransaction(params: SignTransactionParams): Promise<string>;
|
|
90
|
+
/**
|
|
91
|
+
* Get the current reference block info for transaction building
|
|
92
|
+
* This is required for TRON's replay protection mechanism
|
|
93
|
+
*/
|
|
94
|
+
getBlockInfo(): Promise<BlockInfo>;
|
|
95
|
+
/**
|
|
96
|
+
* Build a TRC20 transfer transaction
|
|
97
|
+
*/
|
|
98
|
+
private buildTrc20Transaction;
|
|
99
|
+
/**
|
|
100
|
+
* Serialize a signed transaction to hex format
|
|
101
|
+
*/
|
|
102
|
+
private serializeTransaction;
|
|
103
|
+
/**
|
|
104
|
+
* Convert TRON base58 address to hex format
|
|
105
|
+
*/
|
|
106
|
+
private addressToHex;
|
|
107
|
+
/**
|
|
108
|
+
* Get TRX balance in SUN
|
|
109
|
+
*/
|
|
110
|
+
getBalance(): Promise<bigint>;
|
|
111
|
+
/**
|
|
112
|
+
* Get TRC20 token balance
|
|
113
|
+
* @param contractAddress - TRC20 contract address
|
|
114
|
+
*/
|
|
115
|
+
getTrc20Balance(contractAddress: string): Promise<bigint>;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Create an initialized WDK TRON signer
|
|
119
|
+
*
|
|
120
|
+
* @param account - WDK TRON account from @tetherto/wdk-wallet-tron
|
|
121
|
+
* @param rpcUrl - Optional custom RPC URL (default: https://api.trongrid.io)
|
|
122
|
+
* @returns Initialized ClientTronSigner
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```typescript
|
|
126
|
+
* import { T402WDK } from '@t402/wdk';
|
|
127
|
+
*
|
|
128
|
+
* const wallet = new T402WDK(seedPhrase, config);
|
|
129
|
+
* const tronSigner = await wallet.getTronSigner();
|
|
130
|
+
*
|
|
131
|
+
* // Use with T402 client
|
|
132
|
+
* const client = createT402HTTPClient({
|
|
133
|
+
* signers: [{ scheme: 'exact', network: 'tron:mainnet', signer: tronSigner }]
|
|
134
|
+
* });
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
declare function createWDKTronSigner(account: WDKTronAccount, rpcUrl?: string): Promise<WDKTronSignerAdapter>;
|
|
138
|
+
|
|
139
|
+
export { type BlockInfo, type ClientTronSigner, type SignTransactionParams, WDKTronSignerAdapter, createWDKTronSigner };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
// src/adapters/tron-adapter.ts
|
|
2
|
+
var WDKTronSignerAdapter = class {
|
|
3
|
+
_account;
|
|
4
|
+
_address = null;
|
|
5
|
+
_initialized = false;
|
|
6
|
+
_rpcUrl;
|
|
7
|
+
constructor(account, rpcUrl = "https://api.trongrid.io") {
|
|
8
|
+
if (!account) {
|
|
9
|
+
throw new Error("WDK TRON account is required");
|
|
10
|
+
}
|
|
11
|
+
this._account = account;
|
|
12
|
+
this._rpcUrl = rpcUrl;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Get the wallet address (T-prefix base58check)
|
|
16
|
+
* @throws Error if not initialized
|
|
17
|
+
*/
|
|
18
|
+
get address() {
|
|
19
|
+
if (!this._address) {
|
|
20
|
+
throw new Error(
|
|
21
|
+
"TRON signer not initialized. Call initialize() first or use createWDKTronSigner()."
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
return this._address;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Check if the adapter is initialized
|
|
28
|
+
*/
|
|
29
|
+
get isInitialized() {
|
|
30
|
+
return this._initialized;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Initialize the adapter by fetching the address
|
|
34
|
+
* Must be called before using the signer
|
|
35
|
+
*/
|
|
36
|
+
async initialize() {
|
|
37
|
+
if (this._initialized) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
this._address = await this._account.getAddress();
|
|
41
|
+
this._initialized = true;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Sign a TRC20 transfer transaction
|
|
45
|
+
*
|
|
46
|
+
* This method:
|
|
47
|
+
* 1. Builds a TRC20 transfer transaction
|
|
48
|
+
* 2. Signs it using the WDK account
|
|
49
|
+
* 3. Returns the hex-encoded signed transaction
|
|
50
|
+
*
|
|
51
|
+
* @param params - Transaction parameters
|
|
52
|
+
* @returns Hex-encoded signed transaction
|
|
53
|
+
*/
|
|
54
|
+
async signTransaction(params) {
|
|
55
|
+
if (!params.contractAddress) {
|
|
56
|
+
throw new Error("contractAddress is required");
|
|
57
|
+
}
|
|
58
|
+
if (!params.to) {
|
|
59
|
+
throw new Error("recipient address (to) is required");
|
|
60
|
+
}
|
|
61
|
+
if (!params.amount || BigInt(params.amount) <= 0n) {
|
|
62
|
+
throw new Error("amount must be a positive value");
|
|
63
|
+
}
|
|
64
|
+
const blockInfo = await this.getBlockInfo();
|
|
65
|
+
const feeLimit = params.feeLimit ?? 1e8;
|
|
66
|
+
const transaction = await this.buildTrc20Transaction({
|
|
67
|
+
contractAddress: params.contractAddress,
|
|
68
|
+
to: params.to,
|
|
69
|
+
amount: params.amount,
|
|
70
|
+
feeLimit,
|
|
71
|
+
refBlockBytes: blockInfo.refBlockBytes,
|
|
72
|
+
refBlockHash: blockInfo.refBlockHash,
|
|
73
|
+
expiration: params.expiration ?? blockInfo.expiration
|
|
74
|
+
});
|
|
75
|
+
const signedTx = await this._account.signTransaction(transaction);
|
|
76
|
+
return this.serializeTransaction(signedTx);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Get the current reference block info for transaction building
|
|
80
|
+
* This is required for TRON's replay protection mechanism
|
|
81
|
+
*/
|
|
82
|
+
async getBlockInfo() {
|
|
83
|
+
try {
|
|
84
|
+
const response = await fetch(`${this._rpcUrl}/wallet/getnowblock`, {
|
|
85
|
+
method: "POST",
|
|
86
|
+
headers: { "Content-Type": "application/json" },
|
|
87
|
+
body: JSON.stringify({})
|
|
88
|
+
});
|
|
89
|
+
if (!response.ok) {
|
|
90
|
+
throw new Error(`Failed to get block info: ${response.status}`);
|
|
91
|
+
}
|
|
92
|
+
const block = await response.json();
|
|
93
|
+
const blockNum = block.block_header.raw_data.number;
|
|
94
|
+
const refBlockBytes = blockNum.toString(16).padStart(8, "0").slice(-4);
|
|
95
|
+
const refBlockHash = block.blockID.slice(16, 32);
|
|
96
|
+
const expiration = block.block_header.raw_data.timestamp + 6e4;
|
|
97
|
+
return {
|
|
98
|
+
refBlockBytes,
|
|
99
|
+
refBlockHash,
|
|
100
|
+
expiration
|
|
101
|
+
};
|
|
102
|
+
} catch (error) {
|
|
103
|
+
throw new Error(
|
|
104
|
+
`Failed to get TRON block info: ${error instanceof Error ? error.message : String(error)}`
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Build a TRC20 transfer transaction
|
|
110
|
+
*/
|
|
111
|
+
async buildTrc20Transaction(params) {
|
|
112
|
+
const functionSelector = "transfer(address,uint256)";
|
|
113
|
+
const toAddressHex = this.addressToHex(params.to).slice(2).padStart(64, "0");
|
|
114
|
+
const amountHex = BigInt(params.amount).toString(16).padStart(64, "0");
|
|
115
|
+
const parameter = toAddressHex + amountHex;
|
|
116
|
+
try {
|
|
117
|
+
const response = await fetch(`${this._rpcUrl}/wallet/triggersmartcontract`, {
|
|
118
|
+
method: "POST",
|
|
119
|
+
headers: { "Content-Type": "application/json" },
|
|
120
|
+
body: JSON.stringify({
|
|
121
|
+
owner_address: this.addressToHex(this._address),
|
|
122
|
+
contract_address: this.addressToHex(params.contractAddress),
|
|
123
|
+
function_selector: functionSelector,
|
|
124
|
+
parameter,
|
|
125
|
+
fee_limit: params.feeLimit
|
|
126
|
+
})
|
|
127
|
+
});
|
|
128
|
+
if (!response.ok) {
|
|
129
|
+
throw new Error(`Failed to build transaction: ${response.status}`);
|
|
130
|
+
}
|
|
131
|
+
const result = await response.json();
|
|
132
|
+
if (result.result?.code) {
|
|
133
|
+
throw new Error(`Transaction build failed: ${result.result.message}`);
|
|
134
|
+
}
|
|
135
|
+
return result.transaction;
|
|
136
|
+
} catch (error) {
|
|
137
|
+
throw new Error(
|
|
138
|
+
`Failed to build TRC20 transaction: ${error instanceof Error ? error.message : String(error)}`
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Serialize a signed transaction to hex format
|
|
144
|
+
*/
|
|
145
|
+
serializeTransaction(signedTx) {
|
|
146
|
+
if (signedTx.signature && signedTx.signature.length > 0) {
|
|
147
|
+
return JSON.stringify(signedTx);
|
|
148
|
+
}
|
|
149
|
+
return signedTx.raw_data_hex;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Convert TRON base58 address to hex format
|
|
153
|
+
*/
|
|
154
|
+
addressToHex(address) {
|
|
155
|
+
if (address.startsWith("41") || address.startsWith("0x")) {
|
|
156
|
+
return address.startsWith("0x") ? "41" + address.slice(2) : address;
|
|
157
|
+
}
|
|
158
|
+
const ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
159
|
+
let num = BigInt(0);
|
|
160
|
+
for (const char of address) {
|
|
161
|
+
num = num * BigInt(58) + BigInt(ALPHABET.indexOf(char));
|
|
162
|
+
}
|
|
163
|
+
let hex = num.toString(16);
|
|
164
|
+
let leadingZeros = 0;
|
|
165
|
+
for (const char of address) {
|
|
166
|
+
if (char === "1") leadingZeros++;
|
|
167
|
+
else break;
|
|
168
|
+
}
|
|
169
|
+
hex = "00".repeat(leadingZeros) + hex;
|
|
170
|
+
return hex.slice(0, 42);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Get TRX balance in SUN
|
|
174
|
+
*/
|
|
175
|
+
async getBalance() {
|
|
176
|
+
return this._account.getBalance();
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Get TRC20 token balance
|
|
180
|
+
* @param contractAddress - TRC20 contract address
|
|
181
|
+
*/
|
|
182
|
+
async getTrc20Balance(contractAddress) {
|
|
183
|
+
return this._account.getTrc20Balance(contractAddress);
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
async function createWDKTronSigner(account, rpcUrl) {
|
|
187
|
+
const adapter = new WDKTronSignerAdapter(account, rpcUrl);
|
|
188
|
+
await adapter.initialize();
|
|
189
|
+
return adapter;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export {
|
|
193
|
+
WDKTronSignerAdapter,
|
|
194
|
+
createWDKTronSigner
|
|
195
|
+
};
|
|
196
|
+
//# sourceMappingURL=chunk-HB2DGKQ3.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/adapters/tron-adapter.ts"],"sourcesContent":["/**\n * TRON Signer Adapter for WDK\n *\n * Wraps a Tether WDK TRON account to implement T402's ClientTronSigner interface.\n * This allows WDK-managed TRON wallets to be used for T402 payments.\n */\n\nimport type { WDKTronAccount } from '../types.js'\n\n/**\n * SignTransactionParams matching T402's @t402/tron interface\n */\nexport interface SignTransactionParams {\n /** TRC20 contract address */\n contractAddress: string\n /** Recipient address (T-prefix base58check) */\n to: string\n /** Amount to transfer (in smallest units) */\n amount: string\n /** Fee limit in SUN (optional, defaults to 100 TRX) */\n feeLimit?: number\n /** Transaction expiration time in milliseconds (optional) */\n expiration?: number\n}\n\n/**\n * Block info for transaction building\n */\nexport interface BlockInfo {\n /** Reference block bytes (hex) */\n refBlockBytes: string\n /** Reference block hash (hex) */\n refBlockHash: string\n /** Expiration timestamp in milliseconds */\n expiration: number\n}\n\n/**\n * ClientTronSigner interface matching T402's @t402/tron\n */\nexport interface ClientTronSigner {\n readonly address: string\n signTransaction(params: SignTransactionParams): Promise<string>\n getBlockInfo(): Promise<BlockInfo>\n}\n\n// TronWeb-compatible types\ninterface TronWebTransaction {\n txID: string\n raw_data: {\n contract: unknown[]\n ref_block_bytes: string\n ref_block_hash: string\n expiration: number\n timestamp: number\n }\n raw_data_hex: string\n signature?: string[]\n}\n\ninterface TronWebBlock {\n block_header: {\n raw_data: {\n number: number\n txTrieRoot: string\n witness_address: string\n parentHash: string\n version: number\n timestamp: number\n }\n witness_signature: string\n }\n blockID: string\n}\n\n/**\n * WDKTronSignerAdapter - Adapts a WDK TRON account to T402's ClientTronSigner\n *\n * @example\n * ```typescript\n * const adapter = await createWDKTronSigner(wdkTronAccount);\n * const signedTx = await adapter.signTransaction({\n * contractAddress: 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t',\n * to: 'TRecipientAddress...',\n * amount: '1000000', // 1 USDT\n * });\n * ```\n */\nexport class WDKTronSignerAdapter implements ClientTronSigner {\n private _account: WDKTronAccount\n private _address: string | null = null\n private _initialized = false\n private _rpcUrl: string\n\n constructor(account: WDKTronAccount, rpcUrl = 'https://api.trongrid.io') {\n if (!account) {\n throw new Error('WDK TRON account is required')\n }\n this._account = account\n this._rpcUrl = rpcUrl\n }\n\n /**\n * Get the wallet address (T-prefix base58check)\n * @throws Error if not initialized\n */\n get address(): string {\n if (!this._address) {\n throw new Error(\n 'TRON signer not initialized. Call initialize() first or use createWDKTronSigner().',\n )\n }\n return this._address\n }\n\n /**\n * Check if the adapter is initialized\n */\n get isInitialized(): boolean {\n return this._initialized\n }\n\n /**\n * Initialize the adapter by fetching the address\n * Must be called before using the signer\n */\n async initialize(): Promise<void> {\n if (this._initialized) {\n return\n }\n\n this._address = await this._account.getAddress()\n this._initialized = true\n }\n\n /**\n * Sign a TRC20 transfer transaction\n *\n * This method:\n * 1. Builds a TRC20 transfer transaction\n * 2. Signs it using the WDK account\n * 3. Returns the hex-encoded signed transaction\n *\n * @param params - Transaction parameters\n * @returns Hex-encoded signed transaction\n */\n async signTransaction(params: SignTransactionParams): Promise<string> {\n if (!params.contractAddress) {\n throw new Error('contractAddress is required')\n }\n if (!params.to) {\n throw new Error('recipient address (to) is required')\n }\n if (!params.amount || BigInt(params.amount) <= 0n) {\n throw new Error('amount must be a positive value')\n }\n\n // Get block info for transaction\n const blockInfo = await this.getBlockInfo()\n\n // Default fee limit: 100 TRX = 100_000_000 SUN\n const feeLimit = params.feeLimit ?? 100_000_000\n\n // Build the TRC20 transfer transaction\n const transaction = await this.buildTrc20Transaction({\n contractAddress: params.contractAddress,\n to: params.to,\n amount: params.amount,\n feeLimit,\n refBlockBytes: blockInfo.refBlockBytes,\n refBlockHash: blockInfo.refBlockHash,\n expiration: params.expiration ?? blockInfo.expiration,\n })\n\n // Sign the transaction using WDK account\n const signedTx = await this._account.signTransaction(transaction)\n\n // Serialize to hex format\n return this.serializeTransaction(signedTx as TronWebTransaction)\n }\n\n /**\n * Get the current reference block info for transaction building\n * This is required for TRON's replay protection mechanism\n */\n async getBlockInfo(): Promise<BlockInfo> {\n try {\n const response = await fetch(`${this._rpcUrl}/wallet/getnowblock`, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({}),\n })\n\n if (!response.ok) {\n throw new Error(`Failed to get block info: ${response.status}`)\n }\n\n const block = (await response.json()) as TronWebBlock\n\n // Extract reference block bytes (last 4 bytes of block number)\n const blockNum = block.block_header.raw_data.number\n const refBlockBytes = blockNum.toString(16).padStart(8, '0').slice(-4)\n\n // Reference block hash (first 8 bytes of block ID)\n const refBlockHash = block.blockID.slice(16, 32)\n\n // Expiration: block timestamp + 60 seconds\n const expiration = block.block_header.raw_data.timestamp + 60000\n\n return {\n refBlockBytes,\n refBlockHash,\n expiration,\n }\n } catch (error) {\n throw new Error(\n `Failed to get TRON block info: ${error instanceof Error ? error.message : String(error)}`,\n )\n }\n }\n\n /**\n * Build a TRC20 transfer transaction\n */\n private async buildTrc20Transaction(params: {\n contractAddress: string\n to: string\n amount: string\n feeLimit: number\n refBlockBytes: string\n refBlockHash: string\n expiration: number\n }): Promise<TronWebTransaction> {\n // Build TRC20 transfer function call\n // transfer(address,uint256) = 0xa9059cbb\n const functionSelector = 'transfer(address,uint256)'\n\n // Encode parameters\n const toAddressHex = this.addressToHex(params.to).slice(2).padStart(64, '0')\n const amountHex = BigInt(params.amount).toString(16).padStart(64, '0')\n const parameter = toAddressHex + amountHex\n\n try {\n const response = await fetch(`${this._rpcUrl}/wallet/triggersmartcontract`, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({\n owner_address: this.addressToHex(this._address!),\n contract_address: this.addressToHex(params.contractAddress),\n function_selector: functionSelector,\n parameter,\n fee_limit: params.feeLimit,\n }),\n })\n\n if (!response.ok) {\n throw new Error(`Failed to build transaction: ${response.status}`)\n }\n\n const result = await response.json()\n\n if (result.result?.code) {\n throw new Error(`Transaction build failed: ${result.result.message}`)\n }\n\n return result.transaction as TronWebTransaction\n } catch (error) {\n throw new Error(\n `Failed to build TRC20 transaction: ${error instanceof Error ? error.message : String(error)}`,\n )\n }\n }\n\n /**\n * Serialize a signed transaction to hex format\n */\n private serializeTransaction(signedTx: TronWebTransaction): string {\n // Return the raw_data_hex with signature appended\n if (signedTx.signature && signedTx.signature.length > 0) {\n return JSON.stringify(signedTx)\n }\n return signedTx.raw_data_hex\n }\n\n /**\n * Convert TRON base58 address to hex format\n */\n private addressToHex(address: string): string {\n // If already hex, return as-is\n if (address.startsWith('41') || address.startsWith('0x')) {\n return address.startsWith('0x') ? '41' + address.slice(2) : address\n }\n\n // Convert base58 to hex using simple algorithm\n const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'\n let num = BigInt(0)\n for (const char of address) {\n num = num * BigInt(58) + BigInt(ALPHABET.indexOf(char))\n }\n\n // Convert to hex and take first 42 chars (21 bytes)\n let hex = num.toString(16)\n // Handle leading zeros\n let leadingZeros = 0\n for (const char of address) {\n if (char === '1') leadingZeros++\n else break\n }\n hex = '00'.repeat(leadingZeros) + hex\n\n // TRON address is 21 bytes = 42 hex chars\n return hex.slice(0, 42)\n }\n\n /**\n * Get TRX balance in SUN\n */\n async getBalance(): Promise<bigint> {\n return this._account.getBalance()\n }\n\n /**\n * Get TRC20 token balance\n * @param contractAddress - TRC20 contract address\n */\n async getTrc20Balance(contractAddress: string): Promise<bigint> {\n return this._account.getTrc20Balance(contractAddress)\n }\n}\n\n/**\n * Create an initialized WDK TRON signer\n *\n * @param account - WDK TRON account from @tetherto/wdk-wallet-tron\n * @param rpcUrl - Optional custom RPC URL (default: https://api.trongrid.io)\n * @returns Initialized ClientTronSigner\n *\n * @example\n * ```typescript\n * import { T402WDK } from '@t402/wdk';\n *\n * const wallet = new T402WDK(seedPhrase, config);\n * const tronSigner = await wallet.getTronSigner();\n *\n * // Use with T402 client\n * const client = createT402HTTPClient({\n * signers: [{ scheme: 'exact', network: 'tron:mainnet', signer: tronSigner }]\n * });\n * ```\n */\nexport async function createWDKTronSigner(\n account: WDKTronAccount,\n rpcUrl?: string,\n): Promise<WDKTronSignerAdapter> {\n const adapter = new WDKTronSignerAdapter(account, rpcUrl)\n await adapter.initialize()\n return adapter\n}\n"],"mappings":";AAwFO,IAAM,uBAAN,MAAuD;AAAA,EACpD;AAAA,EACA,WAA0B;AAAA,EAC1B,eAAe;AAAA,EACf;AAAA,EAER,YAAY,SAAyB,SAAS,2BAA2B;AACvE,QAAI,CAAC,SAAS;AACZ,YAAM,IAAI,MAAM,8BAA8B;AAAA,IAChD;AACA,SAAK,WAAW;AAChB,SAAK,UAAU;AAAA,EACjB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,IAAI,UAAkB;AACpB,QAAI,CAAC,KAAK,UAAU;AAClB,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AACA,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,gBAAyB;AAC3B,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,aAA4B;AAChC,QAAI,KAAK,cAAc;AACrB;AAAA,IACF;AAEA,SAAK,WAAW,MAAM,KAAK,SAAS,WAAW;AAC/C,SAAK,eA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
|
+
_address = null;
|
|
5
|
+
_initialized = false;
|
|
6
|
+
constructor(account) {
|
|
7
|
+
if (!account) {
|
|
8
|
+
throw new Error("WDK Solana account is required");
|
|
9
|
+
}
|
|
10
|
+
this._account = account;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Get the wallet address (base58)
|
|
14
|
+
* @throws Error if not initialized
|
|
15
|
+
*/
|
|
16
|
+
get address() {
|
|
17
|
+
if (!this._address) {
|
|
18
|
+
throw new Error(
|
|
19
|
+
"Solana signer not initialized. Call initialize() first or use createWDKSvmSigner()."
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
return this._address;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Check if the adapter is initialized
|
|
26
|
+
*/
|
|
27
|
+
get isInitialized() {
|
|
28
|
+
return this._initialized;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Initialize the adapter by fetching the address
|
|
32
|
+
* Must be called before using the signer
|
|
33
|
+
*/
|
|
34
|
+
async initialize() {
|
|
35
|
+
if (this._initialized) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const addressStr = await this._account.getAddress();
|
|
39
|
+
this._address = addressStr;
|
|
40
|
+
this._initialized = true;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Sign transactions with this signer
|
|
44
|
+
*
|
|
45
|
+
* This method signs the message bytes of each transaction and returns
|
|
46
|
+
* signature dictionaries mapping address to signature.
|
|
47
|
+
*
|
|
48
|
+
* @param transactions - Array of transactions to sign
|
|
49
|
+
* @returns Array of signature dictionaries
|
|
50
|
+
*/
|
|
51
|
+
async signTransactions(transactions) {
|
|
52
|
+
if (!transactions || transactions.length === 0) {
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
const results = [];
|
|
56
|
+
for (const tx of transactions) {
|
|
57
|
+
if (!tx.messageBytes || tx.messageBytes.length === 0) {
|
|
58
|
+
throw new Error("Transaction messageBytes must not be empty");
|
|
59
|
+
}
|
|
60
|
+
const signature = await this._account.sign(tx.messageBytes);
|
|
61
|
+
results.push({
|
|
62
|
+
[this._address]: signature
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return results;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Sign a single message (utility method)
|
|
69
|
+
* @param message - Message bytes to sign
|
|
70
|
+
* @returns Signature bytes
|
|
71
|
+
*/
|
|
72
|
+
async sign(message) {
|
|
73
|
+
return this._account.sign(message);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Get SOL balance in lamports
|
|
77
|
+
*/
|
|
78
|
+
async getBalance() {
|
|
79
|
+
return this._account.getBalance();
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Get SPL token balance
|
|
83
|
+
* @param mint - Token mint address
|
|
84
|
+
*/
|
|
85
|
+
async getTokenBalance(mint) {
|
|
86
|
+
return this._account.getTokenBalance(mint);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Transfer SPL tokens
|
|
90
|
+
* @param params - Transfer parameters
|
|
91
|
+
* @returns Transaction signature
|
|
92
|
+
*/
|
|
93
|
+
async transfer(params) {
|
|
94
|
+
return this._account.transfer(params);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
async function createWDKSvmSigner(account) {
|
|
98
|
+
const adapter = new WDKSvmSignerAdapter(account);
|
|
99
|
+
await adapter.initialize();
|
|
100
|
+
return adapter;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export {
|
|
104
|
+
WDKSvmSignerAdapter,
|
|
105
|
+
createWDKSvmSigner
|
|
106
|
+
};
|
|
107
|
+
//# sourceMappingURL=chunk-MCFHZSF7.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/adapters/svm-adapter.ts"],"sourcesContent":["/**\n * Solana (SVM) Signer Adapter for WDK\n *\n * Wraps a Tether WDK Solana account to implement T402's ClientSvmSigner interface.\n * ClientSvmSigner is just TransactionSigner from @solana/kit.\n */\n\nimport type { WDKSolanaAccount } from '../types.js'\n\n/**\n * Address type from @solana/kit (base58 string)\n * We use a branded type for compatibility\n */\nexport type SolanaAddress = string & { readonly __brand?: unique symbol }\n\n/**\n * TransactionSigner interface matching @solana/kit\n * This is what T402's ClientSvmSigner expects\n */\nexport interface TransactionSigner {\n readonly address: SolanaAddress\n signTransactions<T extends { messageBytes: Uint8Array; signatures: Record<string, unknown> }>(\n transactions: readonly T[],\n ): Promise<readonly Record<string, Uint8Array>[]>\n}\n\n/**\n * WDKSvmSignerAdapter - Adapts a WDK Solana account to T402's ClientSvmSigner\n *\n * ClientSvmSigner is TransactionSigner from @solana/kit which requires:\n * - address: The public key as Address type\n * - signTransactions: Sign multiple transactions, returning signature dictionaries\n *\n * @example\n * ```typescript\n * const adapter = await createWDKSvmSigner(wdkSolanaAccount);\n *\n * // Use with T402 client\n * const client = createT402HTTPClient({\n * signers: [{ scheme: 'exact', network: 'solana:mainnet', signer: adapter }]\n * });\n * ```\n */\nexport class WDKSvmSignerAdapter implements TransactionSigner {\n private _account: WDKSolanaAccount\n private _address: SolanaAddress | null = null\n private _initialized = false\n\n constructor(account: WDKSolanaAccount) {\n if (!account) {\n throw new Error('WDK Solana account is required')\n }\n this._account = account\n }\n\n /**\n * Get the wallet address (base58)\n * @throws Error if not initialized\n */\n get address(): SolanaAddress {\n if (!this._address) {\n throw new Error(\n 'Solana signer not initialized. Call initialize() first or use createWDKSvmSigner().',\n )\n }\n return this._address\n }\n\n /**\n * Check if the adapter is initialized\n */\n get isInitialized(): boolean {\n return this._initialized\n }\n\n /**\n * Initialize the adapter by fetching the address\n * Must be called before using the signer\n */\n async initialize(): Promise<void> {\n if (this._initialized) {\n return\n }\n\n const addressStr = await this._account.getAddress()\n this._address = addressStr as SolanaAddress\n this._initialized = true\n }\n\n /**\n * Sign transactions with this signer\n *\n * This method signs the message bytes of each transaction and returns\n * signature dictionaries mapping address to signature.\n *\n * @param transactions - Array of transactions to sign\n * @returns Array of signature dictionaries\n */\n async signTransactions<\n T extends { messageBytes: Uint8Array; signatures: Record<string, unknown> },\n >(transactions: readonly T[]): Promise<readonly Record<string, Uint8Array>[]> {\n if (!transactions || transactions.length === 0) {\n return []\n }\n\n const results: Record<string, Uint8Array>[] = []\n\n for (const tx of transactions) {\n if (!tx.messageBytes || tx.messageBytes.length === 0) {\n throw new Error('Transaction messageBytes must not be empty')\n }\n\n // Sign the message bytes using WDK account\n const signature = await this._account.sign(tx.messageBytes)\n\n // Return as a dictionary mapping our address to the signature\n results.push({\n [this._address as string]: signature,\n })\n }\n\n return results\n }\n\n /**\n * Sign a single message (utility method)\n * @param message - Message bytes to sign\n * @returns Signature bytes\n */\n async sign(message: Uint8Array): Promise<Uint8Array> {\n return this._account.sign(message)\n }\n\n /**\n * Get SOL balance in lamports\n */\n async getBalance(): Promise<bigint> {\n return this._account.getBalance()\n }\n\n /**\n * Get SPL token balance\n * @param mint - Token mint address\n */\n async getTokenBalance(mint: string): Promise<bigint> {\n return this._account.getTokenBalance(mint)\n }\n\n /**\n * Transfer SPL tokens\n * @param params - Transfer parameters\n * @returns Transaction signature\n */\n async transfer(params: { token: string; recipient: string; amount: bigint }): Promise<string> {\n return this._account.transfer(params)\n }\n}\n\n/**\n * Create an initialized WDK Solana signer\n *\n * @param account - WDK Solana account from @tetherto/wdk-wallet-solana\n * @returns Initialized TransactionSigner (ClientSvmSigner)\n *\n * @example\n * ```typescript\n * import { T402WDK } from '@t402/wdk';\n *\n * const wallet = new T402WDK(seedPhrase, config);\n * const svmSigner = await wallet.getSvmSigner();\n *\n * // Use with T402 client\n * const client = createT402HTTPClient({\n * signers: [{ scheme: 'exact', network: 'solana:mainnet', signer: svmSigner }]\n * });\n * ```\n */\nexport async function createWDKSvmSigner(account: WDKSolanaAccount): Promise<WDKSvmSignerAdapter> {\n const adapter = new WDKSvmSignerAdapter(account)\n await adapter.initialize()\n return adapter\n}\n"],"mappings":";AA2CO,IAAM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