mirror of
https://github.com/fluencelabs/eip712-validation-node
synced 2025-06-30 15:11:46 +00:00
add sqlite
This commit is contained in:
BIN
data/snapshot.db
BIN
data/snapshot.db
Binary file not shown.
16
src/index.ts
16
src/index.ts
@ -12,6 +12,7 @@ import { base64 } from "ethers/lib/utils";
|
|||||||
// Arbitrary secret key that could be read from file, CLI arg or db
|
// Arbitrary secret key that could be read from file, CLI arg or db
|
||||||
// We derive both the PeerId and the (ethers) wallet from this key
|
// We derive both the PeerId and the (ethers) wallet from this key
|
||||||
const SecretKey = "0x0123456789012345678901234567890123456789012345678901234567890123";
|
const SecretKey = "0x0123456789012345678901234567890123456789012345678901234567890123";
|
||||||
|
const DB_PATH = "./data/snapshot.db";
|
||||||
|
|
||||||
|
|
||||||
function create_wallet(sk: string): ethers.Wallet {
|
function create_wallet(sk: string): ethers.Wallet {
|
||||||
@ -41,6 +42,7 @@ class EIPValidator implements ProVoValidationDef {
|
|||||||
|
|
||||||
// console.log(resp_str);
|
// console.log(resp_str);
|
||||||
let obj = { "signature": signed_response, "validation": response };
|
let obj = { "signature": signed_response, "validation": response };
|
||||||
|
|
||||||
return JSON.stringify(obj);
|
return JSON.stringify(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,6 +66,12 @@ class EIPValidator implements ProVoValidationDef {
|
|||||||
// console.log(resp_str);
|
// console.log(resp_str);
|
||||||
|
|
||||||
let obj = { "signature": signed_response, "validation": response };
|
let obj = { "signature": signed_response, "validation": response };
|
||||||
|
|
||||||
|
// commit to local sqlite
|
||||||
|
let db = get_db(DB_PATH);
|
||||||
|
await create_table(db);
|
||||||
|
await insert_event(db, JSON.parse(eip_json), response, signed_response);
|
||||||
|
|
||||||
return JSON.stringify(obj);
|
return JSON.stringify(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,9 +83,11 @@ class DataProvider implements DataProviderDef {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get_records() {
|
async get_records(): Promise<any> {
|
||||||
// todo: add pagination
|
// todo: add pagination
|
||||||
return select_events();
|
const result = await select_events();
|
||||||
|
console.log("get records: ", result);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,7 +106,7 @@ async function main() {
|
|||||||
console.log("wallet pk: ", wallet.publicKey);
|
console.log("wallet pk: ", wallet.publicKey);
|
||||||
|
|
||||||
const skBytes: Uint8Array = ethers.utils.arrayify(SecretKey);
|
const skBytes: Uint8Array = ethers.utils.arrayify(SecretKey);
|
||||||
console.log("arraify: ", skBytes);
|
console.log("arrayify: ", skBytes);
|
||||||
|
|
||||||
await startFluencePeer(skBytes);
|
await startFluencePeer(skBytes);
|
||||||
|
|
||||||
|
@ -63,7 +63,7 @@ export async function create_table(db: sqlite.Database) {
|
|||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function insert_event(db: any, eip_obj: any, response_obj: Response, signed_msg: string) {
|
export async function insert_event(db: sqlite.Database, eip_obj: any, response_obj: Response, signed_msg: string) {
|
||||||
const stmt = `insert into snapshot values (?, ?, ?, ?, ?, ?, ?, ?, ?)`;
|
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];
|
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) => {
|
let promise = new Promise<void>((resolve, reject) => db.run(stmt, ins_vals, (res: sqlite.RunResult, err: Error) => {
|
||||||
@ -93,7 +93,7 @@ export function select_event(snapshot_id: number): any {
|
|||||||
? console.log(row)
|
? console.log(row)
|
||||||
: console.log(`No record found for snapshot id: ${snapshot_id}`);
|
: console.log(`No record found for snapshot id: ${snapshot_id}`);
|
||||||
});
|
});
|
||||||
// db.close();
|
db.close();
|
||||||
};
|
};
|
||||||
|
|
||||||
// export function select_events(): Array<DBRecord> {
|
// export function select_events(): Array<DBRecord> {
|
||||||
@ -102,23 +102,28 @@ export function select_events(): any {
|
|||||||
// todo: add pagination
|
// todo: add pagination
|
||||||
const stmt = 'select * from snapshot limit ?';
|
const stmt = 'select * from snapshot limit ?';
|
||||||
console.log("select events stmt: ", stmt);
|
console.log("select events stmt: ", stmt);
|
||||||
var response_arr: Array<DBRecord>;
|
var response_arr = new Array<DBRecord>();
|
||||||
db.all(stmt, [100], (err, rows) => {
|
const select = db.all(stmt, [100], (err, rows) => {
|
||||||
// db.close();
|
console.log("number of rows: ", rows.length);
|
||||||
if (err) {
|
if (err) {
|
||||||
// todo: no good, change that.
|
// todo: no good, change that.
|
||||||
console.error(err.message);
|
console.error(err.message);
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
for (var row of rows) {
|
for (var row of rows) {
|
||||||
const _row: DBRecord = row;
|
const _row: DBRecord = row;
|
||||||
console.log("row: ", row);
|
response_arr.push(_row);
|
||||||
// response_arr.push(_row);
|
|
||||||
};
|
|
||||||
return response_arr;
|
|
||||||
});
|
|
||||||
|
|
||||||
|
};
|
||||||
|
console.log("len resp array in db all: ", response_arr.length);
|
||||||
|
return response_arr;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// console.log("select: ", select);
|
||||||
// db.close();
|
// db.close();
|
||||||
|
// console.log("len resp array out of db all: ", response_arr);
|
||||||
|
// return response_arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user