From b827d678d4e7f419b7318aea6d3ebfd4c62d7587 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Weber=E2=9D=A4=EF=B8=8FH2O2?= <82763013+Weber-H2O2@users.noreply.github.com> Date: Mon, 7 Nov 2022 23:34:03 +0800 Subject: [PATCH 1/3] test: add a script to test with another node --- package.json | 3 +- scripts/run_with_eigen_service.js | 287 ++++++++++++++++++++++++++++++ 2 files changed, 289 insertions(+), 1 deletion(-) create mode 100644 scripts/run_with_eigen_service.js diff --git a/package.json b/package.json index f958cf1..f0a5086 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,8 @@ "webpack": "^4.29.4", "webpack-cli": "^3.1.1", "webpack-dev-server": "^3.1.0", - "ethers": "^5.7.1" + "ethers": "^5.7.1", + "axios": "^0.23.0" }, "author": "EigenLabs", "main": "index.js", diff --git a/scripts/run_with_eigen_service.js b/scripts/run_with_eigen_service.js new file mode 100644 index 0000000..68c9638 --- /dev/null +++ b/scripts/run_with_eigen_service.js @@ -0,0 +1,287 @@ +const gg18 = require("../pkg"); +const ethers = require("ethers"); +const axios = require("axios"); +const querystring = require("querystring"); + +// var items = [{ idx: 0 }, { idx: 1 }, { idx: 2 }] +// var items = [{ idx: 0 }, { idx: 1 }]; +var items = [{ idx: 0 }]; + +let t = 1; +let n = 2; +let addr = "http://127.0.0.1:8000"; +let eigen_service = "http://127.0.0.1:3000"; + +const delay_ms = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); +const digest = ethers.utils.keccak256(ethers.utils.toUtf8Bytes("Hello Eigen")); + +async function keygen(m, delay) { + user_id = 1; + keygen_url = `${eigen_service}/user/${user_id}/tss/keygen`; + res = await axios.post(keygen_url, { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + }); + console.log(res); + await delay_ms(1000); + + let context = await m.gg18_keygen_client_new_context(addr, t, n, delay); + + console.log("keygen new context: "); + + round = 1; + res = await axios.post(keygen_url, querystring.stringify({ round: round }), { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + }); + console.log(res); + await delay_ms(1000); + + context = await m.gg18_keygen_client_round1(context, delay); + console.log("keygen round1:"); + + round = 2; + res = await axios.post(keygen_url, querystring.stringify({ round: round }), { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + }); + console.log(res); + await delay_ms(1000); + + context = await m.gg18_keygen_client_round2(context, delay); + console.log("keygen round2: "); + + round = 3; + res = await axios.post(keygen_url, querystring.stringify({ round: round }), { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + }); + console.log(res); + await delay_ms(1000); + + context = await m.gg18_keygen_client_round3(context, delay); + console.log("keygen round3: "); + + round = 4; + res = await axios.post(keygen_url, querystring.stringify({ round: round }), { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + }); + console.log(res); + await delay_ms(1000); + + context = await m.gg18_keygen_client_round4(context, delay); + console.log("keygen round4: "); + + round = 5; + res = await axios.post(keygen_url, querystring.stringify({ round: round }), { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + }); + console.log(res); + await delay_ms(1000); + + keygen_json = await m.gg18_keygen_client_round5(context, delay); + console.log("keygen json: ", keygen_json); + context = JSON.parse(context); + + public_key_address = context["public_key_address"]; + console.log("public_key_address: ", public_key_address); + return { keygen_json: keygen_json, public_key_address: public_key_address }; +} + +async function sign(m, key_store, delay, public_key_address) { + console.log("public_key_address = ", public_key_address); + user_id = 1; + sign_url = `${eigen_service}/user/${user_id}/tss/sign`; + res = await axios.post( + sign_url, + querystring.stringify({ + digest: digest.slice(2), + user_address: public_key_address, + }), + { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + } + ); + console.log(res); + await delay_ms(1000); + + let context = await m.gg18_sign_client_new_context( + addr, + t, + n, + key_store, + digest.slice(2) + ); + console.log("sign new context: ", context); + + round = 0; + res = await axios.post(sign_url, querystring.stringify({ round: round }), { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + }); + console.log(res); + await delay_ms(1000); + + context = await m.gg18_sign_client_round0(context, delay); + console.log("sign round0: "); + + round = 1; + res = await axios.post(sign_url, querystring.stringify({ round: round }), { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + }); + console.log(res); + await delay_ms(1000); + + context = await m.gg18_sign_client_round1(context, delay); + console.log("sign round1: "); + + round = 2; + res = await axios.post(sign_url, querystring.stringify({ round: round }), { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + }); + console.log(res); + await delay_ms(1000); + + context = await m.gg18_sign_client_round2(context, delay); + console.log("sign round2: "); + + round = 3; + res = await axios.post(sign_url, querystring.stringify({ round: round }), { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + }); + console.log(res); + await delay_ms(1000); + + context = await m.gg18_sign_client_round3(context, delay); + console.log("sign round3: "); + + round = 4; + res = await axios.post(sign_url, querystring.stringify({ round: round }), { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + }); + console.log(res); + await delay_ms(1000); + + context = await m.gg18_sign_client_round4(context, delay); + console.log("sign round4: "); + + round = 5; + res = await axios.post(sign_url, querystring.stringify({ round: round }), { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + }); + console.log(res); + await delay_ms(1000); + + context = await m.gg18_sign_client_round5(context, delay); + console.log("sign round5: "); + + round = 6; + res = await axios.post(sign_url, querystring.stringify({ round: round }), { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + }); + console.log(res); + await delay_ms(1000); + + context = await m.gg18_sign_client_round6(context, delay); + console.log("sign round6: "); + + round = 7; + res = await axios.post(sign_url, querystring.stringify({ round: round }), { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + }); + console.log(res); + await delay_ms(1000); + + context = await m.gg18_sign_client_round7(context, delay); + console.log("sign round7: "); + + round = 8; + res = await axios.post(sign_url, querystring.stringify({ round: round }), { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + }); + console.log(res); + await delay_ms(1000); + + context = await m.gg18_sign_client_round8(context, delay); + console.log("sign round8: "); + + round = 9; + res = await axios.post(sign_url, querystring.stringify({ round: round }), { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + }); + console.log(res); + await delay_ms(1000); + + sign_json = await m.gg18_sign_client_round9(context, delay); + console.log("keysign json: ", sign_json); + return sign_json; +} + +async function main() { + var results = await Promise.all( + items.map(async (item) => { + let delay = Math.max(Math.random() % 500, 100); + let { keygen_json, public_key_address } = await keygen(gg18, delay); + return { + idx: item.idx, + res: keygen_json, + public_key_address: public_key_address, + }; + }) + ); + + console.log("sign items: ", results); + await Promise.all( + results.map(async (item) => { + if (item.idx < t + 1) { + let delay = Math.max(Math.random() % 500, 100); + //select random signer + res = JSON.parse( + await sign(gg18, item.res, delay, item.public_key_address) + ); + console.log("Sign result: ", res); + // recover the address + console.log("digest", digest); + let address = ethers.utils.recoverAddress(digest, { + r: "0x" + res[0], + s: "0x" + res[1], + v: res[2], + }); + console.log("recover address by etherjs", address); + } + }) + ); +} + +main().then(() => { + console.log("Done"); +}); From 01d43ea6d71d187b0b6795bccb7f3a150315139d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Weber=E2=9D=A4=EF=B8=8FH2O2?= <82763013+Weber-H2O2@users.noreply.github.com> Date: Tue, 8 Nov 2022 22:55:31 +0800 Subject: [PATCH 2/3] test: modify code according to the changement of apis --- scripts/run_with_eigen_service.js | 264 +++++++++++++++++++----------- 1 file changed, 165 insertions(+), 99 deletions(-) diff --git a/scripts/run_with_eigen_service.js b/scripts/run_with_eigen_service.js index 68c9638..6c4f7cb 100644 --- a/scripts/run_with_eigen_service.js +++ b/scripts/run_with_eigen_service.js @@ -17,13 +17,18 @@ const digest = ethers.utils.keccak256(ethers.utils.toUtf8Bytes("Hello Eigen")); async function keygen(m, delay) { user_id = 1; - keygen_url = `${eigen_service}/user/${user_id}/tss/keygen`; - res = await axios.post(keygen_url, { - headers: { - "Content-Type": "application/x-www-form-urlencoded", - }, - }); - console.log(res); + key_name = "Just test"; + keygen_url = `${eigen_service}/tss/keygen`; + res = await axios.post( + keygen_url, + querystring.stringify({ user_id: user_id, name: key_name }), + { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + } + ); + console.log(res.data); await delay_ms(1000); let context = await m.gg18_keygen_client_new_context(addr, t, n, delay); @@ -31,60 +36,80 @@ async function keygen(m, delay) { console.log("keygen new context: "); round = 1; - res = await axios.post(keygen_url, querystring.stringify({ round: round }), { - headers: { - "Content-Type": "application/x-www-form-urlencoded", - }, - }); - console.log(res); + res = await axios.post( + keygen_url, + querystring.stringify({ user_id: user_id, round: round }), + { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + } + ); + console.log(res.data); await delay_ms(1000); context = await m.gg18_keygen_client_round1(context, delay); console.log("keygen round1:"); round = 2; - res = await axios.post(keygen_url, querystring.stringify({ round: round }), { - headers: { - "Content-Type": "application/x-www-form-urlencoded", - }, - }); - console.log(res); + res = await axios.post( + keygen_url, + querystring.stringify({ user_id: user_id, round: round }), + { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + } + ); + console.log(res.data); await delay_ms(1000); context = await m.gg18_keygen_client_round2(context, delay); console.log("keygen round2: "); round = 3; - res = await axios.post(keygen_url, querystring.stringify({ round: round }), { - headers: { - "Content-Type": "application/x-www-form-urlencoded", - }, - }); - console.log(res); + res = await axios.post( + keygen_url, + querystring.stringify({ user_id: user_id, round: round }), + { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + } + ); + console.log(res.data); await delay_ms(1000); context = await m.gg18_keygen_client_round3(context, delay); console.log("keygen round3: "); round = 4; - res = await axios.post(keygen_url, querystring.stringify({ round: round }), { - headers: { - "Content-Type": "application/x-www-form-urlencoded", - }, - }); - console.log(res); + res = await axios.post( + keygen_url, + querystring.stringify({ user_id: user_id, round: round }), + { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + } + ); + console.log(res.data); await delay_ms(1000); context = await m.gg18_keygen_client_round4(context, delay); console.log("keygen round4: "); round = 5; - res = await axios.post(keygen_url, querystring.stringify({ round: round }), { - headers: { - "Content-Type": "application/x-www-form-urlencoded", - }, - }); - console.log(res); + res = await axios.post( + keygen_url, + querystring.stringify({ user_id: user_id, round: round }), + { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + } + ); + console.log(res.data); await delay_ms(1000); keygen_json = await m.gg18_keygen_client_round5(context, delay); @@ -99,12 +124,13 @@ async function keygen(m, delay) { async function sign(m, key_store, delay, public_key_address) { console.log("public_key_address = ", public_key_address); user_id = 1; - sign_url = `${eigen_service}/user/${user_id}/tss/sign`; + sign_url = `${eigen_service}/tss/sign`; res = await axios.post( sign_url, querystring.stringify({ digest: digest.slice(2), user_address: public_key_address, + user_id: user_id, }), { headers: { @@ -112,7 +138,7 @@ async function sign(m, key_store, delay, public_key_address) { }, } ); - console.log(res); + console.log(res.data); await delay_ms(1000); let context = await m.gg18_sign_client_new_context( @@ -125,120 +151,160 @@ async function sign(m, key_store, delay, public_key_address) { console.log("sign new context: ", context); round = 0; - res = await axios.post(sign_url, querystring.stringify({ round: round }), { - headers: { - "Content-Type": "application/x-www-form-urlencoded", - }, - }); - console.log(res); + res = await axios.post( + sign_url, + querystring.stringify({ user_id: user_id, round: round }), + { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + } + ); + console.log(res.data); await delay_ms(1000); context = await m.gg18_sign_client_round0(context, delay); console.log("sign round0: "); round = 1; - res = await axios.post(sign_url, querystring.stringify({ round: round }), { - headers: { - "Content-Type": "application/x-www-form-urlencoded", - }, - }); - console.log(res); + res = await axios.post( + sign_url, + querystring.stringify({ user_id: user_id, round: round }), + { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + } + ); + console.log(res.data); await delay_ms(1000); context = await m.gg18_sign_client_round1(context, delay); console.log("sign round1: "); round = 2; - res = await axios.post(sign_url, querystring.stringify({ round: round }), { - headers: { - "Content-Type": "application/x-www-form-urlencoded", - }, - }); - console.log(res); + res = await axios.post( + sign_url, + querystring.stringify({ user_id: user_id, round: round }), + { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + } + ); + console.log(res.data); await delay_ms(1000); context = await m.gg18_sign_client_round2(context, delay); console.log("sign round2: "); round = 3; - res = await axios.post(sign_url, querystring.stringify({ round: round }), { - headers: { - "Content-Type": "application/x-www-form-urlencoded", - }, - }); - console.log(res); + res = await axios.post( + sign_url, + querystring.stringify({ user_id: user_id, round: round }), + { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + } + ); + console.log(res.data); await delay_ms(1000); context = await m.gg18_sign_client_round3(context, delay); console.log("sign round3: "); round = 4; - res = await axios.post(sign_url, querystring.stringify({ round: round }), { - headers: { - "Content-Type": "application/x-www-form-urlencoded", - }, - }); - console.log(res); + res = await axios.post( + sign_url, + querystring.stringify({ user_id: user_id, round: round }), + { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + } + ); + console.log(res.data); await delay_ms(1000); context = await m.gg18_sign_client_round4(context, delay); console.log("sign round4: "); round = 5; - res = await axios.post(sign_url, querystring.stringify({ round: round }), { - headers: { - "Content-Type": "application/x-www-form-urlencoded", - }, - }); - console.log(res); + res = await axios.post( + sign_url, + querystring.stringify({ user_id: user_id, round: round }), + { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + } + ); + console.log(res.data); await delay_ms(1000); context = await m.gg18_sign_client_round5(context, delay); console.log("sign round5: "); round = 6; - res = await axios.post(sign_url, querystring.stringify({ round: round }), { - headers: { - "Content-Type": "application/x-www-form-urlencoded", - }, - }); - console.log(res); + res = await axios.post( + sign_url, + querystring.stringify({ user_id: user_id, round: round }), + { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + } + ); + console.log(res.data); await delay_ms(1000); context = await m.gg18_sign_client_round6(context, delay); console.log("sign round6: "); round = 7; - res = await axios.post(sign_url, querystring.stringify({ round: round }), { - headers: { - "Content-Type": "application/x-www-form-urlencoded", - }, - }); - console.log(res); + res = await axios.post( + sign_url, + querystring.stringify({ user_id: user_id, round: round }), + { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + } + ); + console.log(res.data); await delay_ms(1000); context = await m.gg18_sign_client_round7(context, delay); console.log("sign round7: "); round = 8; - res = await axios.post(sign_url, querystring.stringify({ round: round }), { - headers: { - "Content-Type": "application/x-www-form-urlencoded", - }, - }); - console.log(res); + res = await axios.post( + sign_url, + querystring.stringify({ user_id: user_id, round: round }), + { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + } + ); + console.log(res.data); await delay_ms(1000); context = await m.gg18_sign_client_round8(context, delay); console.log("sign round8: "); round = 9; - res = await axios.post(sign_url, querystring.stringify({ round: round }), { - headers: { - "Content-Type": "application/x-www-form-urlencoded", - }, - }); - console.log(res); + res = await axios.post( + sign_url, + querystring.stringify({ user_id: user_id, round: round }), + { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + } + ); + console.log(res.data); await delay_ms(1000); sign_json = await m.gg18_sign_client_round9(context, delay); From aea47f7fa423431883e8d52c6b3faf5cfe6cd6d7 Mon Sep 17 00:00:00 2001 From: ibmp33 <2285673866@qq.com> Date: Thu, 10 Nov 2022 20:55:26 +0800 Subject: [PATCH 3/3] chore: add console_time --- scripts/run_with_eigen_service.js | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/scripts/run_with_eigen_service.js b/scripts/run_with_eigen_service.js index 6c4f7cb..f0e42a0 100644 --- a/scripts/run_with_eigen_service.js +++ b/scripts/run_with_eigen_service.js @@ -36,6 +36,7 @@ async function keygen(m, delay) { console.log("keygen new context: "); round = 1; + console.time(1); res = await axios.post( keygen_url, querystring.stringify({ user_id: user_id, round: round }), @@ -50,8 +51,10 @@ async function keygen(m, delay) { context = await m.gg18_keygen_client_round1(context, delay); console.log("keygen round1:"); + console.timeEnd(1); round = 2; + console.time(2); res = await axios.post( keygen_url, querystring.stringify({ user_id: user_id, round: round }), @@ -66,8 +69,10 @@ async function keygen(m, delay) { context = await m.gg18_keygen_client_round2(context, delay); console.log("keygen round2: "); + console.timeEnd(2); round = 3; + console.time(3); res = await axios.post( keygen_url, querystring.stringify({ user_id: user_id, round: round }), @@ -82,8 +87,10 @@ async function keygen(m, delay) { context = await m.gg18_keygen_client_round3(context, delay); console.log("keygen round3: "); + console.timeEnd(3); round = 4; + console.time(4); res = await axios.post( keygen_url, querystring.stringify({ user_id: user_id, round: round }), @@ -98,8 +105,10 @@ async function keygen(m, delay) { context = await m.gg18_keygen_client_round4(context, delay); console.log("keygen round4: "); + console.timeEnd(4); round = 5; + console.time(5); res = await axios.post( keygen_url, querystring.stringify({ user_id: user_id, round: round }), @@ -113,6 +122,7 @@ async function keygen(m, delay) { await delay_ms(1000); keygen_json = await m.gg18_keygen_client_round5(context, delay); + console.timeEnd(5); console.log("keygen json: ", keygen_json); context = JSON.parse(context); @@ -125,6 +135,7 @@ async function sign(m, key_store, delay, public_key_address) { console.log("public_key_address = ", public_key_address); user_id = 1; sign_url = `${eigen_service}/tss/sign`; + console.time(11); res = await axios.post( sign_url, querystring.stringify({ @@ -148,9 +159,11 @@ async function sign(m, key_store, delay, public_key_address) { key_store, digest.slice(2) ); + console.timeEnd(11); console.log("sign new context: ", context); round = 0; + console.time(0); res = await axios.post( sign_url, querystring.stringify({ user_id: user_id, round: round }), @@ -164,9 +177,11 @@ async function sign(m, key_store, delay, public_key_address) { await delay_ms(1000); context = await m.gg18_sign_client_round0(context, delay); + console.timeEnd(0); console.log("sign round0: "); round = 1; + console.time(1); res = await axios.post( sign_url, querystring.stringify({ user_id: user_id, round: round }), @@ -180,9 +195,11 @@ async function sign(m, key_store, delay, public_key_address) { await delay_ms(1000); context = await m.gg18_sign_client_round1(context, delay); + console.timeEnd(1); console.log("sign round1: "); round = 2; + console.time(2); res = await axios.post( sign_url, querystring.stringify({ user_id: user_id, round: round }), @@ -196,9 +213,11 @@ async function sign(m, key_store, delay, public_key_address) { await delay_ms(1000); context = await m.gg18_sign_client_round2(context, delay); + console.timeEnd(2); console.log("sign round2: "); round = 3; + console.time(3); res = await axios.post( sign_url, querystring.stringify({ user_id: user_id, round: round }), @@ -212,9 +231,11 @@ async function sign(m, key_store, delay, public_key_address) { await delay_ms(1000); context = await m.gg18_sign_client_round3(context, delay); + console.timeEnd(3); console.log("sign round3: "); round = 4; + console.time(4); res = await axios.post( sign_url, querystring.stringify({ user_id: user_id, round: round }), @@ -228,9 +249,11 @@ async function sign(m, key_store, delay, public_key_address) { await delay_ms(1000); context = await m.gg18_sign_client_round4(context, delay); + console.timeEnd(4); console.log("sign round4: "); round = 5; + console.time(5); res = await axios.post( sign_url, querystring.stringify({ user_id: user_id, round: round }), @@ -244,9 +267,11 @@ async function sign(m, key_store, delay, public_key_address) { await delay_ms(1000); context = await m.gg18_sign_client_round5(context, delay); + console.timeEnd(5); console.log("sign round5: "); round = 6; + console.time(6); res = await axios.post( sign_url, querystring.stringify({ user_id: user_id, round: round }), @@ -260,9 +285,11 @@ async function sign(m, key_store, delay, public_key_address) { await delay_ms(1000); context = await m.gg18_sign_client_round6(context, delay); + console.timeEnd(6); console.log("sign round6: "); round = 7; + console.time(7); res = await axios.post( sign_url, querystring.stringify({ user_id: user_id, round: round }), @@ -276,9 +303,11 @@ async function sign(m, key_store, delay, public_key_address) { await delay_ms(1000); context = await m.gg18_sign_client_round7(context, delay); + console.timeEnd(7); console.log("sign round7: "); round = 8; + console.time(8); res = await axios.post( sign_url, querystring.stringify({ user_id: user_id, round: round }), @@ -292,9 +321,11 @@ async function sign(m, key_store, delay, public_key_address) { await delay_ms(1000); context = await m.gg18_sign_client_round8(context, delay); + console.timeEnd(8); console.log("sign round8: "); round = 9; + console.time(9); res = await axios.post( sign_url, querystring.stringify({ user_id: user_id, round: round }), @@ -308,6 +339,7 @@ async function sign(m, key_store, delay, public_key_address) { await delay_ms(1000); sign_json = await m.gg18_sign_client_round9(context, delay); + console.timeEnd(9); console.log("keysign json: ", sign_json); return sign_json; }