This commit is contained in:
boneyard93501 2021-10-02 20:57:02 -05:00
parent f41f6286d7
commit 7a521c85b4
4 changed files with 100 additions and 32 deletions

View File

@ -22,6 +22,7 @@
"@types/sqlite3": "^3.1.7",
"ethers": "^5.4.7",
"ethers-eip712": "^0.2.0",
"got": "^11.8.2",
"sqlite3": "^4.2.0"
}
}

View File

@ -31,7 +31,6 @@ function check_signature(eip_obj: any): boolean {
// there may be an upper/lowercase hex difference
const sig_assert = (signing_addr == eip_obj.address);
console.log("sig assert: ", sig_assert);
return sig_assert;
}
@ -41,7 +40,7 @@ export function eip_validation(eip_str: string, peer_id: string): Response {
const eip_obj = JSON.parse(eip_str);
// verify eip document integrity -- not working
// verify eip document integrity -- not working due to missing fields
// const digest = TypedDataUtils.encodeDigest(eip_obj.data);
// const digest_hex = ethers.utils.hexlify(digest);

View File

@ -5,6 +5,7 @@ import { ethers } from "ethers";
import { TypedDataUtils } from 'ethers-eip712'; // https://github.com/0xsequence/ethers-eip712
import { eip_validation, Response } from "./eip_processor";
import { get_db, create_table, insert_event, DBRecord, select_events, select_event } from './local_db';
import got from 'got';
function create_wallet(): ethers.Wallet {
@ -50,17 +51,89 @@ class DataProvider implements DataProviderDef {
}
async function main() {
async function main_1() {
await Fluence.start({
connectTo: krasnodar[0],
});
// const peer_data = Fluence.getPeer();
// console.log("client data\n: ", peer_data);
console.log("application started");
console.log("peer id is: ", Fluence.getStatus().peerId);
console.log("relay is: ", Fluence.getStatus().relayPeerId);
registerProVoValidation(new EIPValidator());
registerDataProvider(new DataProvider);
await Fluence.stop();
// test
// await Fluence.stop();
}
async function main() {
/*
const DB_PATH = './data/snapshot.db';
// https://ipfs.fleek.co/ipfs/QmWGzSQFm57ohEq2ATw4UNHWmYU2HkMjtedcNLodYywpmS
// should come from Aqua
// note: I changed \" to \\" in order for JSON.parse to work
const eip712_doc = `{"address":"0xeF8305E140ac520225DAf050e2f71d5fBcC543e7","sig":"0xc0a90a0bf43c0b774570608bf0279143b366b7880798112b678b416a7500576b41e19f7b4eb457d58de29be3a201f700fafab1f02179da0faae653b7e8ecf82b1c","data":{"domain":{"name":"snapshot","version":"0.1.4"},"types":{"Proposal":[{"name":"from","type":"address"},{"name":"space","type":"string"},{"name":"timestamp","type":"uint64"},{"name":"type","type":"string"},{"name":"title","type":"string"},{"name":"body","type":"string"},{"name":"choices","type":"string[]"},{"name":"start","type":"uint64"},{"name":"end","type":"uint64"},{"name":"snapshot","type":"uint64"},{"name":"network","type":"string"},{"name":"strategies","type":"string"},{"name":"plugins","type":"string"},{"name":"metadata","type":"string"}]},"message":{"space":"fabien.eth","type":"single-choice","title":"This is a long title this is a long title this is a long title this is a long title this is a long title this is a long","body":"This is a long title this is a long title this is a long title title this is a long title this is a long title title this is a long title this is a long title title this is a long title this is a long title title this is a long title this is a long title title this is a long title this is a long title title this is a long title this is a long title title this is a long title this is a long title title this is a long title this is a long title title this is a long title this is a long title title this is a long title this is a long title title this is a long title this is a long title title this is a long title this is a long title title this is a long title this is a long title title this is a long title this is a long title.","choices":["Approve","Reject"],"start":1630472400,"end":1640926800,"snapshot":9278489,"network":"4","strategies":"[{\\"name\\":\\"ticket\\",\\"params\\":{\\"value\\":100,\\"symbol\\":\\"$\\"}}]","plugins":"{}","metadata":"{}","from":"0xeF8305E140ac520225DAf050e2f71d5fBcC543e7","timestamp":1631432106}}}`;
// todo: replace with actual peer pk and sk
const wallet = create_wallet();
const peer_id = wallet.address;
let response = await eip_validation(eip712_doc, peer_id);
// stringify, hexlify, hash and sign response
const resp_str = JSON.stringify(response);
console.log("eip validation response: ", resp_str);
// just sign the raw message string for a long message
const signed_response = await wallet.signMessage(resp_str);
console.log("signed response: ", signed_response);
// verify
const address = ethers.utils.verifyMessage(resp_str, signed_response);
console.log("verify signature. peer_id: ", peer_id, " verified addr: ", address, " equal: ", peer_id === address);
var db = get_db(DB_PATH);
const _table = await create_table(db);
console.log("table: ", _table);
var _insert = await insert_event(db, JSON.parse(eip712_doc), response, signed_response);
console.log("insert: ", _insert);
var _select = await select_events();
console.log("select all:\n", _select, "\n");
var _select = await select_event(9278489);
console.log("select one (9278489):\n", _select, "\n");
// db.close();
*/
const eip_doc: any = await got('https://ipfs.fleek.co/ipfs/QmWGzSQFm57ohEq2ATw4UNHWmYU2HkMjtedcNLodYywpmS').json();
console.log("data: ", typeof (eip_doc));
// console.log("data: ", eip_doc);
console.log("address: ", eip_doc.address);
console.log("signature: ", eip_doc.sig);
console.log("eip doc ", eip_doc.data);
var response = await eip_validation(JSON.stringify(eip_doc), "1234");
console.log(response);
}
main();

View File

@ -37,9 +37,10 @@ export function get_db(db_path: string): sqlite.Database {
}
export async function create_table(db: sqlite.Database) {
const stmt = `create table if not exists snapshot (
snapshot_id integer primary key,
snapshot_id integer unique,
event_address text,
event_signature text,
eip712_doc blob,
@ -51,7 +52,7 @@ export async function create_table(db: sqlite.Database) {
)`;
let promise = new Promise<void>((resolve, reject) => db.run(stmt, (res: sqlite.RunResult, err: Error) => {
console.dir(res);
// console.dir(res);
if (err !== null) {
resolve()
} else {
@ -60,30 +61,21 @@ export async function create_table(db: sqlite.Database) {
}));
return promise;
db.run(stmt);
}
export function insert_event(db: any, eip_obj: any, response_obj: Response, signed_msg: string) {
const stmt = `insert into snapshot values (?,?,?,?,?,?,?,?,?)`;
// const stmt = `insert into snapshot values (?)`;
var cursor = db.prepare(stmt);
var vals = [eip_obj.data.message.snapshot, eip_obj.address, eip_obj.sig, JSON.stringify(eip_obj.data), response_obj.peer_id, response_obj.timestamp, response_obj.eip_validation, response_obj.ts_validation, signed_msg];
// var vals = [1];
for (let v of vals) {
console.log(typeof (v));
}
cursor.run(stmt, vals, function (err: any) {
if (err) {
return console.log("insert error: ", err.message);
export async function insert_event(db: any, eip_obj: any, response_obj: Response, signed_msg: string) {
const stmt = `insert into snapshot values (?, ?, ?, ?, ?, ?, ?, ?, ?)`;
var ins_vals = [eip_obj.data.message.snapshot, eip_obj.address, eip_obj.sig, JSON.stringify(eip_obj.data), response_obj.peer_id, response_obj.timestamp, response_obj.eip_validation, response_obj.ts_validation, signed_msg];
let promise = new Promise<void>((resolve, reject) => db.run(stmt, ins_vals, (res: sqlite.RunResult, err: Error) => {
// console.dir(res);
if (err !== null) {
resolve()
} else {
reject(err)
}
// console.log(`A row has been inserted with row id: ${this.lastID}`);
// console.log(`A row has been inserted with row id: ${db.}`);
});
cursor.finalize();
}));
return promise;
}
@ -91,7 +83,7 @@ export function insert_event(db: any, eip_obj: any, response_obj: Response, sign
export function select_event(snapshot_id: number): any {
// todo: adding request log
var db = get_db(DB_PATH);
const stmt = 'select * from from snapshot where snapshot_id=?'
const stmt = 'select * from snapshot where snapshot_id=?'
db.get(stmt, [snapshot_id], (err, row) => {
db.close();
if (err) {
@ -108,17 +100,20 @@ export function select_event(snapshot_id: number): any {
export function select_events(): any {
var db = get_db(DB_PATH);
// todo: add pagination
const stmt = 'select * from from snapshot limit ?';
const stmt = 'select * from snapshot limit ?';
console.log("select events stmt: ", stmt);
var response_arr: Array<DBRecord>;
db.all(stmt, [100], (err, rows) => {
db.close();
// db.close();
if (err) {
// todo: no good, change that.
console.error(err.message);
return [];
}
for (var row of rows) {
const _row: DBRecord = row;
response_arr.push(_row);
console.log("row: ", row);
// response_arr.push(_row);
};
return response_arr;
});