Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
c0e7183
add cross-env&&dotenv
0xverin Nov 30, 2022
1425dea
set env
0xverin Nov 30, 2022
9baf628
random shard for testing
0xverin Nov 30, 2022
93d6634
Change the versions of @polkadot
0xverin Nov 30, 2022
c88e52f
change the version of @polkadot/types
0xverin Nov 30, 2022
782d6a7
Merge branch 'bugfix-ts-tests' into ts-tests
0xverin Nov 30, 2022
fb5bd53
rm yarn.lock
0xverin Nov 30, 2022
2999730
change yarn.lock
0xverin Dec 2, 2022
bd5b780
generateChallengeCode
0xverin Dec 2, 2022
c51f03c
modify yarn run command
0xverin Dec 2, 2022
40eb6e5
change defaultSinger
0xverin Dec 2, 2022
737dcd7
add getSinger
0xverin Dec 2, 2022
5380b94
add Sign functions
0xverin Dec 2, 2022
c88261c
add getSinger && generateChallengeCode
0xverin Dec 2, 2022
964f199
add ID_HUB_URL for sign message
0xverin Dec 2, 2022
fb2dde8
modify defaultSigner
0xverin Dec 2, 2022
2c983bf
Merge branch 'tee-dev' into ts-tests
0xverin Dec 2, 2022
9a52341
modify shard
0xverin Dec 5, 2022
7784235
add ethers provider&&wallet
0xverin Dec 6, 2022
38a1dd6
add eth endpoint
0xverin Dec 6, 2022
60048ee
add @ethersproject/providers&&ethers
0xverin Dec 7, 2022
d42c0c8
substrate&&ethereum tests
0xverin Dec 7, 2022
96d6eb2
web3 types
0xverin Dec 7, 2022
794a196
nonce
0xverin Dec 7, 2022
35ba8fd
Merge branch 'tee-dev' into ts-tests
0xverin Dec 7, 2022
fa0876c
change methods name
0xverin Dec 7, 2022
99a9cd5
change methods name
0xverin Dec 7, 2022
bce9612
change events
0xverin Dec 7, 2022
a407f90
change methods name
0xverin Dec 8, 2022
9626f4a
change event
0xverin Dec 8, 2022
c8a4c37
modify method name
0xverin Dec 8, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tee-worker/ts-tests/.env.local
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ WORKER_END_POINT = wss://localhost:2000

SUBSTRATE_END_POINT = "ws://localhost:9946"

ETH_END_POINT = "http://localhost:8545"


ID_HUB_URL='http://localhost:3000'
195 changes: 180 additions & 15 deletions tee-worker/ts-tests/identity.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { describeLitentry, generateVerificationMessage, getMessage } from "./utils";
import { hexToU8a, u8aToHex } from "@polkadot/util";
import {
describeLitentry,
generateVerificationMessage,
getMessage,
listenEncryptedEvents,
} from "./utils";
import { hexToU8a, u8aToHex, stringToU8a } from "@polkadot/util";
import {
createIdentity,
setUserShieldingKey,
Expand All @@ -10,6 +15,9 @@ import { step } from "mocha-steps";
import { assert } from "chai";
import { LitentryIdentity, LitentryValidationData } from "./type-definitions";
import { Sign } from "./web3/functions";
import { generateTestKeys } from "./web3/functions";
import { ethers } from "ethers";
import { HexString } from "@polkadot/util/types";
const twitterIdentity = <LitentryIdentity>{
handle: {
PlainString: `0x${Buffer.from("mock_user", "utf8").toString("hex")}`,
Expand All @@ -19,6 +27,27 @@ const twitterIdentity = <LitentryIdentity>{
},
};

const ethereumIdentity = <LitentryIdentity>{
handle: {
Address20: `0xff93B45308FD417dF303D6515aB04D9e89a750Ca`,
},
web_type: {
Web3Identity: {
Evm: "Ethereum",
},
},
};

const substrateIdentity = <LitentryIdentity>{
handle: {
Address32: `0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d`, //alice
},
web_type: {
Web3Identity: {
Substrate: "Litentry",
},
},
};
const twitterValidationData = <LitentryValidationData>{
Web2Validation: {
Twitter: {
Expand All @@ -27,6 +56,32 @@ const twitterValidationData = <LitentryValidationData>{
},
};

const ethereumValidationData = <LitentryValidationData>{
Web3Validation: {
Evm: {
message: `0x${Buffer.from("mock_message", "utf8").toString("hex")}`,
signature: {
Ethereum: `0x${Buffer.from(
"10ee76e356d944d17bce552a4fd0d4554ccc97dc81213f470367bd3b99c441c51",
"utf8"
).toString("hex")}`,
},
},
},
};
const substrateValidationData = <LitentryValidationData>{
Web3Validation: {
Substrate: {
message: `0x${Buffer.from("mock_message", "utf8").toString("hex")}`,
signature: {
Sr25519: `0x${Buffer.from(
"10ee76e356d944d17bce552a4fd0d4554ccc97dc81213f470367bd3b99c441c51",
"utf8"
).toString("hex")}`,
},
},
},
};
const discordIdentity = <LitentryIdentity>{
handle: {
PlainString: `0x${Buffer.from("859641379851337798", "utf8").toString("hex")}`,
Expand All @@ -48,21 +103,26 @@ const discordValidationData = <LitentryValidationData>{

describeLitentry("Test Identity", (context) => {
const aesKey = "0x22fc82db5b606998ad45099b7978b5b4f9dd4ea6017e57370ac56141caaabd12";
var signature_ethereum;
var signature_substrate;

step("set user shielding key", async function () {
//get signature
// const message = getMessage(context.defaultSigner.address, "polkadot-js");
// const signature = await Sign(message, context.defaultSigner);

const who = await setUserShieldingKey(context, context.defaultSigner, aesKey, true);
assert.equal(who, u8aToHex(context.defaultSigner.addressRaw), "check caller error");
});

step("create twitter identity", async function () {
const r = await createIdentity(context, context.defaultSigner, aesKey, true, twitterIdentity);
if (r) {
const [_who, challengeCode] = r;
console.log("challengeCode: ", challengeCode);
step("create identity", async function () {
//create twitter identity
const resp_twitter = await createIdentity(
context,
context.defaultSigner,
aesKey,
true,
twitterIdentity
);
if (resp_twitter) {
const [_who, challengeCode] = resp_twitter;
console.log("twitterIdentity challengeCode: ", challengeCode);
const msg = generateVerificationMessage(
context,
hexToU8a(challengeCode),
Expand All @@ -72,28 +132,133 @@ describeLitentry("Test Identity", (context) => {
console.log("post verification msg to twitter: ", msg);
assert.isNotEmpty(challengeCode, "challengeCode empty");
}
//create ethereum identity
const resp_ethereum = await createIdentity(
context,
context.defaultSigner,
aesKey,
true,
ethereumIdentity
);
if (resp_ethereum) {
const [_who, challengeCode] = resp_ethereum;
console.log("ethereumIdentity challengeCode: ", challengeCode);
const msg = generateVerificationMessage(
context,
hexToU8a(challengeCode),
context.defaultSigner.addressRaw,
ethereumIdentity
);
console.log("post verification msg to ethereum: ", msg);
ethereumValidationData!.Web3Validation!.Evm!.message = msg;
const msgHash = ethers.utils.arrayify(msg);
signature_ethereum = await context.ethersWallet.alice.signMessage(msgHash);
ethereumValidationData!.Web3Validation!.Evm!.signature!.Ethereum = signature_ethereum;
assert.isNotEmpty(challengeCode, "challengeCode empty");
}
// create substrate identity
const resp_substrate = await createIdentity(
context,
context.defaultSigner,
aesKey,
true,
substrateIdentity
);
if (resp_substrate) {
const [_who, challengeCode] = resp_substrate;
console.log("substrateIdentity challengeCode: ", challengeCode);
const msg = generateVerificationMessage(
context,
hexToU8a(challengeCode),
context.defaultSigner.addressRaw,
substrateIdentity
);

console.log("post verification msg to substrate: ", msg);
substrateValidationData!.Web3Validation!.Substrate!.message = msg;
signature_substrate = context.defaultSigner.sign(msg);
substrateValidationData!.Web3Validation!.Substrate!.signature!.Sr25519 =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qq: when would this be ED255 or EDCA?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonalvarezz I don't understand your question, can you explain it more clearly?:see_no_evil:

u8aToHex(signature_substrate);
assert.isNotEmpty(challengeCode, "challengeCode empty");
}
});

step("verify twitter identity", async function () {
const who = await verifyIdentity(
step("verify identity", async function () {
//verify twitter identity
const who_twitter = await verifyIdentity(
context,
context.defaultSigner,
aesKey,
true,
twitterIdentity,
twitterValidationData
);
assert.equal(who_twitter, u8aToHex(context.defaultSigner.addressRaw), "check caller error");

// verify ethereum identity
const who_ethereum = await verifyIdentity(
context,
context.defaultSigner,
aesKey,
true,
ethereumIdentity,
ethereumValidationData
);
assert.equal(
who_ethereum,
u8aToHex(context.defaultSigner.addressRaw),
"check caller error"
);

//verify substrate identity
const who = await verifyIdentity(
context,
context.defaultSigner,
aesKey,
true,
substrateIdentity,
substrateValidationData
);
assert.equal(who, u8aToHex(context.defaultSigner.addressRaw), "check caller error");
});

step("remove identity", async function () {
const who = await removeIdentity(
//remove twitter identity
const who_twitter = await removeIdentity(
context,
context.defaultSigner,
aesKey,
true,
twitterIdentity
);
assert.equal(who, u8aToHex(context.defaultSigner.addressRaw), "check caller error");
assert.equal(who_twitter, u8aToHex(context.defaultSigner.addressRaw), "check caller error");

//remove ethereum identity
const who_ethereum = await removeIdentity(
context,
context.defaultSigner,
aesKey,
true,
ethereumIdentity
);
assert.equal(
who_ethereum,
u8aToHex(context.defaultSigner.addressRaw),
"check caller error"
);

//remove substrate identity
const who_substrate = await removeIdentity(
context,
context.defaultSigner,
aesKey,
true,
substrateIdentity
);
assert.equal(
who_substrate,
u8aToHex(context.defaultSigner.addressRaw),
"check caller error"
);
});
});
11 changes: 8 additions & 3 deletions tee-worker/ts-tests/indirect_calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ export async function createIdentity(
): Promise<HexString[] | undefined> {
const encode = context.substrate.createType("LitentryIdentity", identity).toHex();
const ciphertext = encryptWithTeeShieldingKey(context.teeShieldingKey, encode).toString("hex");
const nonce = await context.substrate.rpc.system.accountNextIndex(signer.address);
await context.substrate.tx.identityManagement
.createIdentity(context.shard, `0x${ciphertext}`, null)
.signAndSend(signer);
.signAndSend(signer, { nonce });
if (listening) {
const event = await listenEncryptedEvents(context, aesKey, {
module: "identityManagement",
Expand All @@ -62,9 +63,11 @@ export async function removeIdentity(
): Promise<HexString | undefined> {
const encode = context.substrate.createType("LitentryIdentity", identity).toHex();
const ciphertext = encryptWithTeeShieldingKey(context.teeShieldingKey, encode).toString("hex");
const nonce = await context.substrate.rpc.system.accountNextIndex(signer.address);

await context.substrate.tx.identityManagement
.removeIdentity(context.shard, `0x${ciphertext}`)
.signAndSend(signer);
.signAndSend(signer, { nonce });
if (listening) {
const event = await listenEncryptedEvents(context, aesKey, {
module: "identityManagement",
Expand Down Expand Up @@ -95,9 +98,11 @@ export async function verifyIdentity(
context.teeShieldingKey,
validation_encode
).toString("hex");
const nonce = await context.substrate.rpc.system.accountNextIndex(signer.address);

await context.substrate.tx.identityManagement
.verifyIdentity(context.shard, `0x${identity_ciphertext}`, `0x${validation_ciphertext}`)
.signAndSend(signer);
.signAndSend(signer, { nonce });
if (listening) {
const event = await listenEncryptedEvents(context, aesKey, {
module: "identityManagement",
Expand Down
2 changes: 2 additions & 0 deletions tee-worker/ts-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
"ws": "^8.8.1"
},
"devDependencies": {
"@ethersproject/providers": "^5.7.2",
"@types/chai": "^4.3.3",
"@types/mocha": "^10.0.0",
"@types/ws": "^8.5.3",
"cross-env": "^7.0.3",
"dotenv": "^16.0.3",
"ethers": "^5.7.2",
"ts-node": "^10.8.1",
"typescript": "^4.7.3"
},
Expand Down
Loading