Skip to content

Commit d4a9f58

Browse files
authored
link web3 and verify web3 (#1072)
* add cross-env&&dotenv * set env * random shard for testing * Change the versions of @PolkaDot * change the version of @polkadot/types * rm yarn.lock * change yarn.lock * generateChallengeCode * modify yarn run command * change defaultSinger * add getSinger * add Sign functions * add getSinger && generateChallengeCode * add ID_HUB_URL for sign message * modify defaultSigner * modify shard * add ethers provider&&wallet * add eth endpoint * add @ethersproject/providers&&ethers * substrate&&ethereum tests * web3 types * nonce * change methods name * change methods name * change events * change methods name * change event * modify method name
1 parent c485920 commit d4a9f58

File tree

9 files changed

+826
-125
lines changed

9 files changed

+826
-125
lines changed

tee-worker/ts-tests/.env.local

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ WORKER_END_POINT = wss://localhost:2000
55

66
SUBSTRATE_END_POINT = "ws://localhost:9946"
77

8+
ETH_END_POINT = "http://localhost:8545"
9+
10+
811
ID_HUB_URL='http://localhost:3000'

tee-worker/ts-tests/identity.test.ts

Lines changed: 180 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import { describeLitentry, generateVerificationMessage, getMessage } from "./utils";
2-
import { hexToU8a, u8aToHex } from "@polkadot/util";
1+
import {
2+
describeLitentry,
3+
generateVerificationMessage,
4+
getMessage,
5+
listenEncryptedEvents,
6+
} from "./utils";
7+
import { hexToU8a, u8aToHex, stringToU8a } from "@polkadot/util";
38
import {
49
createIdentity,
510
setUserShieldingKey,
@@ -10,6 +15,9 @@ import { step } from "mocha-steps";
1015
import { assert } from "chai";
1116
import { LitentryIdentity, LitentryValidationData } from "./type-definitions";
1217
import { Sign } from "./web3/functions";
18+
import { generateTestKeys } from "./web3/functions";
19+
import { ethers } from "ethers";
20+
import { HexString } from "@polkadot/util/types";
1321
const twitterIdentity = <LitentryIdentity>{
1422
handle: {
1523
PlainString: `0x${Buffer.from("mock_user", "utf8").toString("hex")}`,
@@ -19,6 +27,27 @@ const twitterIdentity = <LitentryIdentity>{
1927
},
2028
};
2129

30+
const ethereumIdentity = <LitentryIdentity>{
31+
handle: {
32+
Address20: `0xff93B45308FD417dF303D6515aB04D9e89a750Ca`,
33+
},
34+
web_type: {
35+
Web3Identity: {
36+
Evm: "Ethereum",
37+
},
38+
},
39+
};
40+
41+
const substrateIdentity = <LitentryIdentity>{
42+
handle: {
43+
Address32: `0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d`, //alice
44+
},
45+
web_type: {
46+
Web3Identity: {
47+
Substrate: "Litentry",
48+
},
49+
},
50+
};
2251
const twitterValidationData = <LitentryValidationData>{
2352
Web2Validation: {
2453
Twitter: {
@@ -27,6 +56,32 @@ const twitterValidationData = <LitentryValidationData>{
2756
},
2857
};
2958

59+
const ethereumValidationData = <LitentryValidationData>{
60+
Web3Validation: {
61+
Evm: {
62+
message: `0x${Buffer.from("mock_message", "utf8").toString("hex")}`,
63+
signature: {
64+
Ethereum: `0x${Buffer.from(
65+
"10ee76e356d944d17bce552a4fd0d4554ccc97dc81213f470367bd3b99c441c51",
66+
"utf8"
67+
).toString("hex")}`,
68+
},
69+
},
70+
},
71+
};
72+
const substrateValidationData = <LitentryValidationData>{
73+
Web3Validation: {
74+
Substrate: {
75+
message: `0x${Buffer.from("mock_message", "utf8").toString("hex")}`,
76+
signature: {
77+
Sr25519: `0x${Buffer.from(
78+
"10ee76e356d944d17bce552a4fd0d4554ccc97dc81213f470367bd3b99c441c51",
79+
"utf8"
80+
).toString("hex")}`,
81+
},
82+
},
83+
},
84+
};
3085
const discordIdentity = <LitentryIdentity>{
3186
handle: {
3287
PlainString: `0x${Buffer.from("859641379851337798", "utf8").toString("hex")}`,
@@ -48,21 +103,26 @@ const discordValidationData = <LitentryValidationData>{
48103

49104
describeLitentry("Test Identity", (context) => {
50105
const aesKey = "0x22fc82db5b606998ad45099b7978b5b4f9dd4ea6017e57370ac56141caaabd12";
106+
var signature_ethereum;
107+
var signature_substrate;
51108

52109
step("set user shielding key", async function () {
53-
//get signature
54-
// const message = getMessage(context.defaultSigner.address, "polkadot-js");
55-
// const signature = await Sign(message, context.defaultSigner);
56-
57110
const who = await setUserShieldingKey(context, context.defaultSigner, aesKey, true);
58111
assert.equal(who, u8aToHex(context.defaultSigner.addressRaw), "check caller error");
59112
});
60113

61-
step("create twitter identity", async function () {
62-
const r = await createIdentity(context, context.defaultSigner, aesKey, true, twitterIdentity);
63-
if (r) {
64-
const [_who, challengeCode] = r;
65-
console.log("challengeCode: ", challengeCode);
114+
step("create identity", async function () {
115+
//create twitter identity
116+
const resp_twitter = await createIdentity(
117+
context,
118+
context.defaultSigner,
119+
aesKey,
120+
true,
121+
twitterIdentity
122+
);
123+
if (resp_twitter) {
124+
const [_who, challengeCode] = resp_twitter;
125+
console.log("twitterIdentity challengeCode: ", challengeCode);
66126
const msg = generateVerificationMessage(
67127
context,
68128
hexToU8a(challengeCode),
@@ -72,28 +132,133 @@ describeLitentry("Test Identity", (context) => {
72132
console.log("post verification msg to twitter: ", msg);
73133
assert.isNotEmpty(challengeCode, "challengeCode empty");
74134
}
135+
//create ethereum identity
136+
const resp_ethereum = await createIdentity(
137+
context,
138+
context.defaultSigner,
139+
aesKey,
140+
true,
141+
ethereumIdentity
142+
);
143+
if (resp_ethereum) {
144+
const [_who, challengeCode] = resp_ethereum;
145+
console.log("ethereumIdentity challengeCode: ", challengeCode);
146+
const msg = generateVerificationMessage(
147+
context,
148+
hexToU8a(challengeCode),
149+
context.defaultSigner.addressRaw,
150+
ethereumIdentity
151+
);
152+
console.log("post verification msg to ethereum: ", msg);
153+
ethereumValidationData!.Web3Validation!.Evm!.message = msg;
154+
const msgHash = ethers.utils.arrayify(msg);
155+
signature_ethereum = await context.ethersWallet.alice.signMessage(msgHash);
156+
ethereumValidationData!.Web3Validation!.Evm!.signature!.Ethereum = signature_ethereum;
157+
assert.isNotEmpty(challengeCode, "challengeCode empty");
158+
}
159+
// create substrate identity
160+
const resp_substrate = await createIdentity(
161+
context,
162+
context.defaultSigner,
163+
aesKey,
164+
true,
165+
substrateIdentity
166+
);
167+
if (resp_substrate) {
168+
const [_who, challengeCode] = resp_substrate;
169+
console.log("substrateIdentity challengeCode: ", challengeCode);
170+
const msg = generateVerificationMessage(
171+
context,
172+
hexToU8a(challengeCode),
173+
context.defaultSigner.addressRaw,
174+
substrateIdentity
175+
);
176+
177+
console.log("post verification msg to substrate: ", msg);
178+
substrateValidationData!.Web3Validation!.Substrate!.message = msg;
179+
signature_substrate = context.defaultSigner.sign(msg);
180+
substrateValidationData!.Web3Validation!.Substrate!.signature!.Sr25519 =
181+
u8aToHex(signature_substrate);
182+
assert.isNotEmpty(challengeCode, "challengeCode empty");
183+
}
75184
});
76185

77-
step("verify twitter identity", async function () {
78-
const who = await verifyIdentity(
186+
step("verify identity", async function () {
187+
//verify twitter identity
188+
const who_twitter = await verifyIdentity(
79189
context,
80190
context.defaultSigner,
81191
aesKey,
82192
true,
83193
twitterIdentity,
84194
twitterValidationData
85195
);
196+
assert.equal(who_twitter, u8aToHex(context.defaultSigner.addressRaw), "check caller error");
197+
198+
// verify ethereum identity
199+
const who_ethereum = await verifyIdentity(
200+
context,
201+
context.defaultSigner,
202+
aesKey,
203+
true,
204+
ethereumIdentity,
205+
ethereumValidationData
206+
);
207+
assert.equal(
208+
who_ethereum,
209+
u8aToHex(context.defaultSigner.addressRaw),
210+
"check caller error"
211+
);
212+
213+
//verify substrate identity
214+
const who = await verifyIdentity(
215+
context,
216+
context.defaultSigner,
217+
aesKey,
218+
true,
219+
substrateIdentity,
220+
substrateValidationData
221+
);
86222
assert.equal(who, u8aToHex(context.defaultSigner.addressRaw), "check caller error");
87223
});
88224

89225
step("remove identity", async function () {
90-
const who = await removeIdentity(
226+
//remove twitter identity
227+
const who_twitter = await removeIdentity(
91228
context,
92229
context.defaultSigner,
93230
aesKey,
94231
true,
95232
twitterIdentity
96233
);
97-
assert.equal(who, u8aToHex(context.defaultSigner.addressRaw), "check caller error");
234+
assert.equal(who_twitter, u8aToHex(context.defaultSigner.addressRaw), "check caller error");
235+
236+
//remove ethereum identity
237+
const who_ethereum = await removeIdentity(
238+
context,
239+
context.defaultSigner,
240+
aesKey,
241+
true,
242+
ethereumIdentity
243+
);
244+
assert.equal(
245+
who_ethereum,
246+
u8aToHex(context.defaultSigner.addressRaw),
247+
"check caller error"
248+
);
249+
250+
//remove substrate identity
251+
const who_substrate = await removeIdentity(
252+
context,
253+
context.defaultSigner,
254+
aesKey,
255+
true,
256+
substrateIdentity
257+
);
258+
assert.equal(
259+
who_substrate,
260+
u8aToHex(context.defaultSigner.addressRaw),
261+
"check caller error"
262+
);
98263
});
99264
});

tee-worker/ts-tests/indirect_calls.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ export async function createIdentity(
3838
): Promise<HexString[] | undefined> {
3939
const encode = context.substrate.createType("LitentryIdentity", identity).toHex();
4040
const ciphertext = encryptWithTeeShieldingKey(context.teeShieldingKey, encode).toString("hex");
41+
const nonce = await context.substrate.rpc.system.accountNextIndex(signer.address);
4142
await context.substrate.tx.identityManagement
4243
.createIdentity(context.shard, `0x${ciphertext}`, null)
43-
.signAndSend(signer);
44+
.signAndSend(signer, { nonce });
4445
if (listening) {
4546
const event = await listenEncryptedEvents(context, aesKey, {
4647
module: "identityManagement",
@@ -62,9 +63,11 @@ export async function removeIdentity(
6263
): Promise<HexString | undefined> {
6364
const encode = context.substrate.createType("LitentryIdentity", identity).toHex();
6465
const ciphertext = encryptWithTeeShieldingKey(context.teeShieldingKey, encode).toString("hex");
66+
const nonce = await context.substrate.rpc.system.accountNextIndex(signer.address);
67+
6568
await context.substrate.tx.identityManagement
6669
.removeIdentity(context.shard, `0x${ciphertext}`)
67-
.signAndSend(signer);
70+
.signAndSend(signer, { nonce });
6871
if (listening) {
6972
const event = await listenEncryptedEvents(context, aesKey, {
7073
module: "identityManagement",
@@ -95,9 +98,11 @@ export async function verifyIdentity(
9598
context.teeShieldingKey,
9699
validation_encode
97100
).toString("hex");
101+
const nonce = await context.substrate.rpc.system.accountNextIndex(signer.address);
102+
98103
await context.substrate.tx.identityManagement
99104
.verifyIdentity(context.shard, `0x${identity_ciphertext}`, `0x${validation_ciphertext}`)
100-
.signAndSend(signer);
105+
.signAndSend(signer, { nonce });
101106
if (listening) {
102107
const event = await listenEncryptedEvents(context, aesKey, {
103108
module: "identityManagement",

tee-worker/ts-tests/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
"ws": "^8.8.1"
1616
},
1717
"devDependencies": {
18+
"@ethersproject/providers": "^5.7.2",
1819
"@types/chai": "^4.3.3",
1920
"@types/mocha": "^10.0.0",
2021
"@types/ws": "^8.5.3",
2122
"cross-env": "^7.0.3",
2223
"dotenv": "^16.0.3",
24+
"ethers": "^5.7.2",
2325
"ts-node": "^10.8.1",
2426
"typescript": "^4.7.3"
2527
},

0 commit comments

Comments
 (0)