mirror of
https://github.com/fluencelabs/eip712-validation-node
synced 2025-05-02 11:52:14 +00:00
update aqua
This commit is contained in:
parent
509e5f4ead
commit
a1c8244cfa
@ -8,18 +8,17 @@ data DBResponse:
|
||||
snapshot_id: u64
|
||||
|
||||
service ProVoValidation("eip_validation"):
|
||||
eip_validation(eip_str: string, peer_id: string)
|
||||
eip712_validation(eip_str: string, peer_id: string) -> PVResponse
|
||||
|
||||
service DataProvider(""):
|
||||
get_records()
|
||||
get_record(snapshot_id: u64)
|
||||
get_records() -> []DBResponse
|
||||
get_record(snapshot_id: u64) -> DBResponse
|
||||
|
||||
|
||||
func validate(relay: string, peer:string, eip712_json: string) -> PVResponse {
|
||||
func validate(relay: string, peer:string, eip712_json: string) -> PVResponse:
|
||||
on peer via relay:
|
||||
result <- ProVoValidation.eip_validation(eip712_json, peer)
|
||||
result <- ProVoValidation.eip712_validation(eip712_json, peer)
|
||||
<- result
|
||||
}
|
||||
|
||||
|
||||
func get_record(relay:string, peer:string, snapshot_id: u64) -> DBResponse:
|
||||
|
@ -36,7 +36,8 @@ function check_signature(eip_obj: any): boolean {
|
||||
}
|
||||
|
||||
|
||||
export async function eip_validation(eip_str: string, peer_id: string): Promise<Response> {
|
||||
// export async function eip_validation(eip_str: string, peer_id: string): Promise<Response> {
|
||||
export function eip_validation(eip_str: string, peer_id: string): Response {
|
||||
|
||||
const eip_obj = JSON.parse(eip_str);
|
||||
|
||||
|
85
src/index.ts
85
src/index.ts
@ -1,18 +1,11 @@
|
||||
// import { Fluence } from "@fluencelabs/fluence";
|
||||
// import { krasnodar } from "@fluencelabs/fluence-network-environment";
|
||||
// import { registerCalc, CalcDef } from "./_aqua/calc";
|
||||
|
||||
import { Fluence } from "@fluencelabs/fluence";
|
||||
import { krasnodar } from "@fluencelabs/fluence-network-environment";
|
||||
import { registerProVoValidation, ProVoValidationDef } from "./_aqua/snapshot";
|
||||
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 } from './local_db';
|
||||
|
||||
var sqlite3 = require('sqlite3').verbose();
|
||||
|
||||
|
||||
|
||||
|
||||
const DB_PATH = './data/snapshot.db';
|
||||
|
||||
function create_wallet(): ethers.Wallet {
|
||||
return ethers.Wallet.createRandom();
|
||||
@ -22,43 +15,53 @@ function sign_response(wallet: ethers.Wallet, response: Response): Promise<strin
|
||||
const signed_msg = wallet.signMessage(JSON.stringify(response));
|
||||
return signed_msg;
|
||||
}
|
||||
class EIPValidator implements ProVoValidationDef {
|
||||
|
||||
eip712_validation(eip712_json: string) {
|
||||
// todo: need to fix this to use local peer key
|
||||
const wallet = create_wallet();
|
||||
let response = eip_validation(eip712_json, wallet.address);
|
||||
|
||||
const resp_str = JSON.stringify(response);
|
||||
console.log("eip validation response: ", resp_str);
|
||||
|
||||
const signed_response = 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);
|
||||
console.log(resp_str);
|
||||
return response; //, signed_response];
|
||||
}
|
||||
}
|
||||
|
||||
class EIPValidator implements ProVoValidationDef {
|
||||
|
||||
get_record() {
|
||||
// todo: add pagination
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
get_records(snapshot_id) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
async function main() {
|
||||
// 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);
|
||||
await Fluence.start({
|
||||
connectTo: krasnodar[0],
|
||||
});
|
||||
|
||||
registerProVoValidation(new EIPValidator());
|
||||
|
||||
|
||||
await Fluence.stop();
|
||||
|
||||
// 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);
|
||||
|
||||
// return (resp_str, signed_response);
|
||||
|
||||
var db = get_db(DB_PATH);
|
||||
const _table = await create_table(db);
|
||||
console.log(_table);
|
||||
|
||||
const _insert = insert_event(db, JSON.parse(eip712_doc), response, signed_response);
|
||||
console.log(_insert);
|
||||
|
||||
// db.close();
|
||||
}
|
||||
|
||||
main();
|
||||
|
@ -5,6 +5,7 @@ import * as sqlite from 'sqlite3';
|
||||
const sqlite3 = sqlite.verbose();
|
||||
import { Response } from './eip_processor';
|
||||
|
||||
const DB_PATH = './data/snapshot.db';
|
||||
|
||||
export interface DBRecord {
|
||||
snapshot_id: number;
|
||||
@ -19,7 +20,7 @@ export interface DBRecord {
|
||||
}
|
||||
|
||||
// db handler
|
||||
export function get_db(db_path: any): sqlite.Database {
|
||||
export function get_db(db_path: string): sqlite.Database {
|
||||
var db_path = db_path;
|
||||
|
||||
if (db_path === null) {
|
||||
@ -86,6 +87,45 @@ export function insert_event(db: any, eip_obj: any, response_obj: Response, sign
|
||||
}
|
||||
|
||||
|
||||
// export function select_event(snapshot_id: number): DBRecord {
|
||||
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=?'
|
||||
db.get(stmt, [snapshot_id], (err, row) => {
|
||||
db.close();
|
||||
if (err) {
|
||||
return console.error(err.message);
|
||||
}
|
||||
return row
|
||||
? console.log(row)
|
||||
: console.log(`No record found for snapshot id: ${snapshot_id}`);
|
||||
});
|
||||
// db.close();
|
||||
};
|
||||
|
||||
// export function select_events(): Array<DBRecord> {
|
||||
export function select_events(): any {
|
||||
var db = get_db(DB_PATH);
|
||||
// todo: add pagination
|
||||
const stmt = 'select * from from snapshot limit ?';
|
||||
var response_arr: Array<DBRecord>;
|
||||
db.all(stmt, [100], (err, rows) => {
|
||||
if (err) {
|
||||
// todo: no good, change that.
|
||||
return [];
|
||||
}
|
||||
for (var row of rows) {
|
||||
const _row: DBRecord = row;
|
||||
response_arr.push(_row);
|
||||
};
|
||||
return response_arr;
|
||||
});
|
||||
|
||||
db.close();
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
|
||||
var db = new sqlite3.Database(DB_PATH);
|
||||
|
Loading…
x
Reference in New Issue
Block a user