-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathethereum.ts
More file actions
137 lines (122 loc) · 3.18 KB
/
ethereum.ts
File metadata and controls
137 lines (122 loc) · 3.18 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import {
Client,
ClientConfigBuilder,
} from "@polywrap/client-js";
import { Uri } from "@polywrap/core-js";
import {
ethereumWalletPlugin,
Connection,
Connections,
} from "@polywrap/ethereum-wallet-js";
import { Wallet } from "ethers";
const ETHEREUM_CORE_URI = "wrapscan.io/polywrap/ethers@1.0.0";
const ETHEREUM_UTIL_URI = "wrapscan.io/polywrap/ethers-utils@1.0.0";
const main = async () => {
const { uri: ethereumWalletUri } = Uri.from(
"wrapscan.io/polywrap/ethereum-wallet@1.0"
);
const builder = new ClientConfigBuilder();
builder.addBundle("sys");
const ethereumWalletPackage = ethereumWalletPlugin({
connections: new Connections({
networks: {
mainnet: new Connection({
provider:
"https://mainnet.infura.io/v3/f1f688077be642c190ac9b28769daecf",
signer: new Wallet(
"4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d"
),
}),
},
}),
});
builder.setPackage(ethereumWalletUri, ethereumWalletPackage);
const client = new Client(builder.build());
const balance = await client.invoke<String>({
uri: ETHEREUM_CORE_URI,
method: "getBalance",
args: {
address: "0x00000000219ab540356cbb839cbe05303d7705fa",
},
});
if (!balance.ok) {
throw Error("Error getting balance: " + balance.error);
}
console.log("Balance in Wei: " + balance.value);
const balanceInEth = await client.invoke<String>({
uri: ETHEREUM_UTIL_URI,
method: "toEth",
args: {
wei: balance.value,
},
});
if (!balanceInEth.ok) {
throw Error("Error converting balance to ETH: " + balanceInEth.error);
}
console.log("Balance in Eth: " + balanceInEth.value);
const domain = {
name: "Ether Mail",
version: "1",
chainId: 1,
verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
};
// The named list of all type definitions
const types = {
EIP712Domain: [
{
type: "string",
name: "name",
},
{
type: "string",
name: "version",
},
{
type: "uint256",
name: "chainId",
},
{
type: "address",
name: "verifyingContract",
},
],
Person: [
{ name: "name", type: "string" },
{ name: "wallet", type: "address" },
],
Mail: [
{ name: "from", type: "Person" },
{ name: "to", type: "Person" },
{ name: "contents", type: "string" },
],
};
// The data to sign
const message = {
from: {
name: "Cow",
wallet: "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",
},
to: {
name: "Bob",
wallet: "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB",
},
contents: "Hello, Bob!",
};
console.log("Signing typed data...");
const signedData = await client.invoke<string>({
uri: ETHEREUM_CORE_URI,
method: "signTypedData",
args: {
payload: JSON.stringify({ domain, primaryType: "Mail", types, message }),
},
});
if (!signedData.ok) {
throw Error("Error signing data: " + signedData.error);
}
console.log("Signed data: " + signedData.value);
};
main()
.then()
.catch((e) => {
console.error("Error in Ethereum example: ", e);
});