-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdual-sign-typed-data-from-private-key.ts
More file actions
77 lines (70 loc) · 2.51 KB
/
Copy pathdual-sign-typed-data-from-private-key.ts
File metadata and controls
77 lines (70 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* Demo: Resolve both TRON and EVM signers from one external input.
*
* This example maps one of these external environment variables into the SDK's
* expected env vars:
*
* - `PRIVATE_KEY`
* - `MNEMONIC`
* - `WALLET_PASSWORD`
* - `MNEMONIC_ACCOUNT_INDEX` (optional, mnemonic mode only)
*
* Then it resolves two wallet providers:
*
* - TRON via `resolveWalletProvider({ network: "tron" })`
* - EVM via `resolveWalletProvider({ network: "eip155" })`
*
* Usage:
* PRIVATE_KEY=<hex> npx tsx examples/dual-sign-typed-data-from-private-key.ts
* MNEMONIC="word1 word2 ..." npx tsx examples/dual-sign-typed-data-from-private-key.ts
* MNEMONIC="word1 word2 ..." MNEMONIC_ACCOUNT_INDEX=1 npx tsx examples/dual-sign-typed-data-from-private-key.ts
* WALLET_PASSWORD=<password> npx tsx examples/dual-sign-typed-data-from-private-key.ts
*/
import { resolveWalletProvider, type Eip712Capable } from "../src/index.js";
const PAYMENT_PERMIT = {
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" },
],
PaymentPermitDetails: [
{ name: "buyer", type: "address" },
{ name: "amount", type: "uint256" },
{ name: "nonce", type: "uint256" },
],
},
primaryType: "PaymentPermitDetails",
domain: {
name: "x402PaymentPermit",
chainId: 1,
verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
},
message: {
buyer: "0x70997970C51812dc3A010C7d01b50e0d17dc79C8",
amount: 1000000,
nonce: 0,
},
};
async function main() {
const tronProvider = resolveWalletProvider({ network: "tron" });
const tronWallet = (await tronProvider.getActiveWallet()) as unknown as Eip712Capable & {
getAddress(): Promise<string>;
};
const tronAddress = await tronWallet.getAddress();
const tronSignature = await tronWallet.signTypedData(PAYMENT_PERMIT);
const evmProvider = resolveWalletProvider({ network: "eip155" });
const evmWallet = (await evmProvider.getActiveWallet()) as unknown as Eip712Capable & {
getAddress(): Promise<string>;
};
const evmAddress = await evmWallet.getAddress();
const evmSignature = await evmWallet.signTypedData(PAYMENT_PERMIT);
console.log("=== TRON ===");
console.log(`Address: ${tronAddress}`);
console.log(`Signature: ${tronSignature}`);
console.log();
console.log("=== EVM ===");
console.log(`Address: ${evmAddress}`);
console.log(`Signature: ${evmSignature}`);
}
main().catch(console.error);