Skip to content

Commit

Permalink
Merge pull request #37 from DelphinusLab/trial
Browse files Browse the repository at this point in the history
add sketch of submit mode auto
  • Loading branch information
xgaozoyoe authored Jan 11, 2025
2 parents 374349e + 26eb2e4 commit 8d593f0
Show file tree
Hide file tree
Showing 5 changed files with 257 additions and 103 deletions.
4 changes: 4 additions & 0 deletions ts/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ export const get_contract_addr = () => {
}
}

export const get_chain_id = () => {
return 16;
}

export const get_user_addr = () => {
if (process.env.USER_ADDRESS) {
return process.env.USER_ADDRESS;
Expand Down
29 changes: 9 additions & 20 deletions ts/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,18 @@ export class U8ArrayUtil {
}
}

export class NumberUtil {
bn: BN;
constructor(num: number) {
this.bn = new BN(num);
}
toBN(){
let bns = new Array<BN>();
let bnStr = this.bn.toString("hex", 64)
for (let i = 0; i < bnStr.length; i += 16) {
const chunk = bnStr.slice(i, i + 16);
let a = new BN(chunk, 'hex', 'be');
bns.push(a);
}
return bns;
}
toU64StringArray() {
return this.toBN().map((x) => x.toString(10));
}
}

export function merkleRootToBeHexString(root: BigUint64Array) {
let bigint = root[3] + (root[2]<<64n) + (root[1]<<128n) + (root[0]<<192n);
let bnStr = bigint.toString(10);
let bn = new BN(bnStr, 10);
return '0x' + bn.toString("hex", 64);
}

export function hexStringToMerkleRoot(hexStr: string) {
let merkleRootBN = BigInt(hexStr);
let root = [0n, 0n, 0n, 0n];
for (let i=0; i<4; i++) {
root[i] = (merkleRootBN >> BigInt(64 * (3-i))) % (1n << 64n);
}
return root;
}
2 changes: 1 addition & 1 deletion ts/src/reproduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import BN from "bn.js";
import { ServiceHelper, get_contract_addr, modelBundle, get_user_private_account } from "./config.js";
import abiData from './Proxy.json' assert { type: 'json' };
import {ZkWasmUtil, PaginationResult, QueryParams, Task, VerifyProofParams} from "zkwasm-service-helper";
import { U8ArrayUtil, NumberUtil } from './lib.js';
import { U8ArrayUtil } from './lib.js';

import * as fs from 'fs';

Expand Down
45 changes: 44 additions & 1 deletion ts/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { getMerkleArray } from "./contract.js";
import { ZkWasmUtil } from "zkwasm-service-helper";
import dotenv from 'dotenv';
import mongoose from 'mongoose';
import {merkleRootToBeHexString} from "./lib.js";
import {hexStringToMerkleRoot, merkleRootToBeHexString} from "./lib.js";
import {sha256} from "ethers";

// Load environment variables from .env file
Expand Down Expand Up @@ -401,6 +401,18 @@ export class Service {
success: true,
data: jstr
});
}
} catch (error) {
console.error('Error adding job to the queue:', error);
res.status(500).send('Failed to add job to the queue');
}
});

app.post('/query', async (req, res) => {
const value = req.body;
if (!value) {
return res.status(400).send('Value is required');
}

} catch (error) {
res.status(500).send('Get Status Error');
Expand All @@ -416,6 +428,37 @@ export class Service {
}


app.get('/prooftask/:root', async (req, res) => {
try {
let merkleRootString = req.params.root;
let merkleRoot = new BigUint64Array(hexStringToMerkleRoot(merkleRootString));
let record = await modelBundle.findOne({ merkleRoot: merkleRoot});
if (record) {
return res.status(201).json(record);
} else {
throw Error("TaskNotFound");
}
} catch (err) {
// job not tracked
console.log(err);
res.status(500).json({ message: (err as Error).toString()});
}
});


app.post('/config', async (req, res) => {
try {
let jstr = application.get_config();
res.status(201).send({
success: true,
data: jstr
});

} catch (error) {
res.status(500).send('Get Status Error');
}
});

function signature_to_u64array(value: any) {
const msg = new LeHexBN(value.msg).toU64Array();
const pkx = new LeHexBN(value.pkx).toU64Array();
Expand Down
Loading

0 comments on commit 8d593f0

Please sign in to comment.