Skip to content

Commit

Permalink
fix contract bug
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLastHobbit committed Jun 20, 2024
1 parent 54761a1 commit be8b9fc
Show file tree
Hide file tree
Showing 33 changed files with 3,980 additions and 292 deletions.
Binary file modified .DS_Store
Binary file not shown.
5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"hosho.solidity-debugger"
]
}
199 changes: 199 additions & 0 deletions broadcast/Wallet.s.sol/11155111/run-1718890749.json

Large diffs are not rendered by default.

142 changes: 71 additions & 71 deletions broadcast/Wallet.s.sol/11155111/run-latest.json

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions cache/Wallet.s.sol/11155111/run-1718890749.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"transactions": [
{
"rpc": "https://sepolia.infura.io/v3/dbe77fbac5b8494e8f03b1099638abfd"
},
{
"rpc": "https://sepolia.infura.io/v3/dbe77fbac5b8494e8f03b1099638abfd"
},
{
"rpc": "https://sepolia.infura.io/v3/dbe77fbac5b8494e8f03b1099638abfd"
},
{
"rpc": "https://sepolia.infura.io/v3/dbe77fbac5b8494e8f03b1099638abfd"
}
]
}
2 changes: 1 addition & 1 deletion cache/solidity-files-cache.json

Large diffs are not rendered by default.

Binary file added frontend/.DS_Store
Binary file not shown.
9 changes: 5 additions & 4 deletions frontend/wallet/src/components/Register.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState } from 'react';
import { ethers } from 'ethers';
import { Link } from 'react-router-dom';
import Button from '../components/Button';
import { signUOP, getHash, createAccount, getCommitment, createWallet, getSalt, getWalletAddress, createPackedUserOperation } from '../util/wallet.js';
import { encodeCommitment,signUOP, getHash, createAccount, getCommitment, createWallet, getSalt, getWalletAddress, createPackedUserOperation } from '../util/wallet.js';
import axios from 'axios';

function Register() {
Expand All @@ -29,9 +29,10 @@ function Register() {
const wallet = await createWallet();
console.log("wallet:", wallet.address);
const salt = await getSalt(email);
const commitment = await getCommitment(email);
console.log("salt:", salt.toString());
console.log("commitment:", commitment);
const uncodecommitment = await getCommitment(email);
const commitment = encodeCommitment(uncodecommitment[0]);
// console.log("salt:", salt.toString());
console.log("commitment2:", commitment);
const walletAddress = await getWalletAddress(wallet.address, salt, commitment);
console.log("walletAddress:", walletAddress);

Expand Down
59 changes: 9 additions & 50 deletions frontend/wallet/src/contracts/wallet.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,9 @@
"inputs": [],
"outputs": [
{
"name": "m",
"type": "string",
"internalType": "string"
},
{
"name": "r",
"type": "uint256",
"internalType": "uint256"
"name": "",
"type": "bytes",
"internalType": "bytes"
}
],
"stateMutability": "view"
Expand Down Expand Up @@ -338,20 +333,8 @@
},
{
"name": "_commitment",
"type": "tuple",
"internalType": "struct PedersenCommitment.Commitment",
"components": [
{
"name": "m",
"type": "string",
"internalType": "string"
},
{
"name": "r",
"type": "uint256",
"internalType": "uint256"
}
]
"type": "bytes",
"internalType": "bytes"
}
],
"outputs": [],
Expand Down Expand Up @@ -579,20 +562,8 @@
"inputs": [
{
"name": "_commitment",
"type": "tuple",
"internalType": "struct PedersenCommitment.Commitment",
"components": [
{
"name": "m",
"type": "string",
"internalType": "string"
},
{
"name": "r",
"type": "uint256",
"internalType": "uint256"
}
]
"type": "bytes",
"internalType": "bytes"
}
],
"outputs": [],
Expand Down Expand Up @@ -849,21 +820,9 @@
},
{
"name": "commitment",
"type": "tuple",
"type": "bytes",
"indexed": true,
"internalType": "struct PedersenCommitment.Commitment",
"components": [
{
"name": "m",
"type": "string",
"internalType": "string"
},
{
"name": "r",
"type": "uint256",
"internalType": "uint256"
}
]
"internalType": "bytes"
}
],
"anonymous": false
Expand Down
60 changes: 30 additions & 30 deletions frontend/wallet/src/contracts/walletfactory.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@
],
"stateMutability": "nonpayable"
},
{
"type": "function",
"name": "create",
"inputs": [
{
"name": "owner",
"type": "address",
"internalType": "address"
},
{
"name": "salt",
"type": "uint256",
"internalType": "uint256"
}
],
"outputs": [
{
"name": "",
"type": "address",
"internalType": "contract Wallet"
}
],
"stateMutability": "nonpayable"
},
{
"type": "function",
"name": "createAccount",
Expand All @@ -30,21 +54,9 @@
"internalType": "uint256"
},
{
"name": "emailHash",
"type": "tuple",
"internalType": "struct PedersenCommitment.Commitment",
"components": [
{
"name": "m",
"type": "string",
"internalType": "string"
},
{
"name": "r",
"type": "uint256",
"internalType": "uint256"
}
]
"name": "commitment",
"type": "bytes",
"internalType": "bytes"
}
],
"outputs": [
Expand Down Expand Up @@ -84,21 +96,9 @@
"internalType": "uint256"
},
{
"name": "emailHash",
"type": "tuple",
"internalType": "struct PedersenCommitment.Commitment",
"components": [
{
"name": "m",
"type": "string",
"internalType": "string"
},
{
"name": "r",
"type": "uint256",
"internalType": "uint256"
}
]
"name": "commitment",
"type": "bytes",
"internalType": "bytes"
}
],
"outputs": [
Expand Down
106 changes: 106 additions & 0 deletions frontend/wallet/src/util/shamir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
const prime = 257; // 使用的素数

// Encrypt function
function encrypt(secret, n, num) {
if (secret.length === 0 || n <= 0) {
throw new Error('illegal input data');
}

if (num < n) {
throw new Error('num has to be greater than n');
}

const result = initArray(num, secret.length);

for (let j = 0; j < secret.length; j++) {
const f = getPolynomial(n, secret[j]);
for (let i = 0; i < num; i++) {
if (j === 0) {
result[i][0] = i + 1;
}
result[i][j + 1] = f(i + 1);
}
}

const temp = decrypt(result, n);
for (let i = 0; i < secret.length; i++) {
if (temp[i] !== secret[i]) {
return encrypt(secret, n, num);
}
}

return result;
}

// Decrypt function
function decrypt(shares, n) {
if (shares.length === 0) {
throw new Error('illegal input data');
}

const x = Array.from({ length: n }, (_, i) => i + 1);
const yss = initInt64Array(shares[0].length - 1, n);

for (let i = 0; i < n; i++) {
for (let j = 1; j < shares[i].length; j++) {
yss[j - 1][i] = shares[i][j];
}
}

const result = [];

for (const ys of yss) {
const secret = lagrange(0, x, ys);
let tempSecret = secret % prime;
if (tempSecret < 0) {
tempSecret += prime;
}
result.push(tempSecret);
}

return new Uint8Array(result);
}

function initArray(a, b) {
return Array.from({ length: a }, () => new Uint8Array(b + 1));
}

function getPolynomial(n, secretMsg) {
const coefficients = Array.from({ length: n - 1 }, () => {
let temp;
do {
temp = Math.floor(Math.random() * 256);
} while (temp === 0);
return temp;
});

return (x) => {
let count = 0;
for (let i = 0; i < coefficients.length; i++) {
count += coefficients[i] * Math.pow(x, n - 1 - i);
}
count += secretMsg;
return count % prime;
};
}

function initInt64Array(a, b) {
return Array.from({ length: a }, () => Array(b).fill(0));
}

// Lagrange interpolation
function lagrange(x, xs, ys) {
let l = 0;
for (let i = 0; i < xs.length; i++) {
let term = ys[i];
for (let j = 0; j < xs.length; j++) {
if (i !== j) {
term *= (x - xs[j]) / (xs[i] - xs[j]);
}
}
l += term;
}
return l;
}

module.exports = { encrypt, decrypt };
25 changes: 16 additions & 9 deletions frontend/wallet/src/util/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const rpcUrl = 'https://sepolia.infura.io/v3/dbe77fbac5b8494e8f03b1099638abfd';
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);

// const WALLET_ADDRESS = "0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9";
const FACTORY_ADDRESS = "0x85e1218B83e0bF7a7519d98dF5B73C4F8f19412C";
const FACTORY_ADDRESS = "0xB71aa8d44E43D8a28E64fcBd6b651e0dbc0bdb4E";
const ZKTOOL_ADDRESS = "0xCBa2Be4eCEa8c15F6FC4fd31C5fa85Bf0377291e"
const ENTRYPOINT_ADDRESS = "0xF988D980A36c3E8da79AB91B4562fD81adA7ECE3";
const ENTRYPOINT_ADDRESS = "0x1A5C9969F47Ef041c3A359ae4ae9fd9E70eA5653";

// 因为前端只调只读函数不发送交易,所以不需要signer
const factory = new ethers.Contract(FACTORY_ADDRESS, FACTORY_ABI, provider);
Expand All @@ -31,23 +31,30 @@ export async function createWallet() {

// 创建钱包所需要的initcode
export async function createAccount(owner, salt, emailcommitment) {
const commitment = emailcommitment[0];
let initCode =
FACTORY_ADDRESS +
factory.interface
.encodeFunctionData("createAccount", [
owner,
salt,
{
m: commitment.m,
r: commitment.r
}
emailcommitment
])
.slice(2);
console.log("initCode: " + initCode);
return initCode;
}

export function encodeCommitment(commitment) {
// 使用 ethers.js 的 defaultAbiCoder 进行编码
console.log("commitment: " + commitment);
const abiCoder = ethers.utils.defaultAbiCoder;
console.log("2222");
return abiCoder.encode(
["string", "uint256"],
[commitment.m, commitment.r]
);
}

export async function getCommitment(email) {
// 验证 email 是有效字符串
// if (typeof email !== 'string' || !email.includes('@')) {
Expand Down Expand Up @@ -124,8 +131,8 @@ export async function getHash(uop) {

// 前端获取钱包地址
export async function getWalletAddress(owner, salt, emailcommitment) {
console.log("getWalletAddress:", owner, salt, emailcommitment[0]);
const walletAddress = await factory.callStatic.getAddress(owner, salt, emailcommitment[0]);
console.log("getWalletAddress:", owner, salt, emailcommitment);
const walletAddress = await factory.callStatic.getAddress(owner, salt, emailcommitment);
return walletAddress;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/account-abstraction/contracts/core/BasePaymaster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import "./UserOperationLib.sol";
* provides helper methods for staking.
* Validates that the postOp is called only by the entryPoint.
*/
abstract contract BasePaymaster is IPaymaster, Ownable {
abstract contract Base is IPaymaster, Ownable {
IEntryPoint public immutable entryPoint;

uint256 internal constant PAYMASTER_VALIDATION_GAS_OFFSET = UserOperationLib.PAYMASTER_VALIDATION_GAS_OFFSET;
Expand Down
1 change: 1 addition & 0 deletions lib/account-abstraction/contracts/core/EntryPoint.sol
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ contract EntryPoint is IEntryPoint, StakeManager, NonceManager, ReentrancyGuard,
function getUserOpHash(
PackedUserOperation calldata userOp
) public view returns (bytes32) {
console.log("11111111111111111111111111111111111111111111111");
return
keccak256(abi.encode(userOp.hash(), address(this), block.chainid));
}
Expand Down
Loading

0 comments on commit be8b9fc

Please sign in to comment.