Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/hebao_v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
_[hebao]_(荷包)means wallet in China -- see https://www.pinterest.com/pin/373376625330954965 for examples.

# Build

```
yarn install
yarn compile
```

# Run test

```
yarn test
```

# Deploy contract to arbitrum:

```
npx hardhat run --network arbitrum scripts/deploy-arbitrum.ts
```
4 changes: 3 additions & 1 deletion packages/hebao_v2/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ export default {
gasMultiplier: 1,
timeout: 20000,
httpHeaders: undefined,
accounts: loadTestAccounts().map(item => item.privateKey).slice()
accounts: loadTestAccounts()
.map(item => item.privateKey)
.slice()
}
},

Expand Down
15 changes: 7 additions & 8 deletions packages/hebao_v2/script/deploy-arbitrum.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
const hre = require("hardhat");
const ethers = hre.ethers;
import {
newWalletImpl,
newWalletFactoryContract
} from "../test/commons";
import { newWalletImpl, newWalletFactoryContract } from "../test/commons";
import { signCreateWallet } from "../test/helper/signatureUtils";
import BN = require("bn.js");

Expand Down Expand Up @@ -37,17 +34,19 @@ async function newWallet() {
signature: Buffer.from(signature.txSignature.slice(2), "hex")
};

const walletFactory = await (await ethers.getContractFactory(
"WalletFactory"
)).attach(walletFactoryAddress);
const walletFactory = await (
await ethers.getContractFactory("WalletFactory")
).attach(walletFactoryAddress);

const walletAddrComputed = await walletFactory.computeWalletAddress(
ownerAddr,
salt
);
console.log("walletAddrcomputed:", walletAddrComputed);

const tx = await walletFactory.createWallet(walletConfig, salt, { gasLimit:10000000 });
const tx = await walletFactory.createWallet(walletConfig, salt, {
gasLimit: 10000000
});
console.log("tx:", tx);
const receipt = await tx.wait();
console.log("receipt:", receipt);
Expand Down
6 changes: 3 additions & 3 deletions packages/hebao_v2/test/callContract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ describe("wallet", () => {
account2
);
LRC = await (await ethers.getContractFactory("LRC")).deploy();
TestContract = await (await ethers.getContractFactory(
"TestTargetContract"
)).deploy();
TestContract = await (
await ethers.getContractFactory("TestTargetContract")
).deploy();
});

describe("callContract", () => {
Expand Down
246 changes: 132 additions & 114 deletions packages/hebao_v2/test/commons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,61 @@ import BN = require("bn.js");
import { signCreateWallet } from "./helper/signatureUtils";

export async function newWalletImpl() {
const ERC1271Lib = await (await ethers.getContractFactory(
"ERC1271Lib"
)).deploy();
const ERC1271Lib = await (
await ethers.getContractFactory("ERC1271Lib")
).deploy();
const ERC20Lib = await (await ethers.getContractFactory("ERC20Lib")).deploy();
const GuardianLib = await (await ethers.getContractFactory(
"GuardianLib"
)).deploy();
const InheritanceLib = await (await ethers.getContractFactory(
"InheritanceLib"
)).deploy();
const LockLib = await (await ethers.getContractFactory("LockLib", {
libraries: {
GuardianLib: GuardianLib.address
}
})).deploy();
const MetaTxLib = await (await ethers.getContractFactory("MetaTxLib", {
libraries: {
ERC20Lib: ERC20Lib.address
}
})).deploy();
const GuardianLib = await (
await ethers.getContractFactory("GuardianLib")
).deploy();
const InheritanceLib = await (
await ethers.getContractFactory("InheritanceLib")
).deploy();
const LockLib = await (
await ethers.getContractFactory("LockLib", {
libraries: {
GuardianLib: GuardianLib.address
}
})
).deploy();
const MetaTxLib = await (
await ethers.getContractFactory("MetaTxLib", {
libraries: {
ERC20Lib: ERC20Lib.address
}
})
).deploy();
const QuotaLib = await (await ethers.getContractFactory("QuotaLib")).deploy();
const RecoverLib = await (await ethers.getContractFactory("RecoverLib", {
libraries: {
GuardianLib: GuardianLib.address
}
})).deploy();
const UpgradeLib = await (await ethers.getContractFactory(
"UpgradeLib"
)).deploy();
const WhitelistLib = await (await ethers.getContractFactory(
"WhitelistLib"
)).deploy();
const RecoverLib = await (
await ethers.getContractFactory("RecoverLib", {
libraries: {
GuardianLib: GuardianLib.address
}
})
).deploy();
const UpgradeLib = await (
await ethers.getContractFactory("UpgradeLib")
).deploy();
const WhitelistLib = await (
await ethers.getContractFactory("WhitelistLib")
).deploy();

const smartWallet = await (await ethers.getContractFactory("SmartWallet", {
libraries: {
ERC1271Lib: ERC1271Lib.address,
ERC20Lib: ERC20Lib.address,
GuardianLib: GuardianLib.address,
InheritanceLib: InheritanceLib.address,
LockLib: LockLib.address,
MetaTxLib: MetaTxLib.address,
QuotaLib: QuotaLib.address,
RecoverLib: RecoverLib.address,
UpgradeLib: UpgradeLib.address,
WhitelistLib: WhitelistLib.address
}
})).deploy(ethers.constants.AddressZero);
const smartWallet = await (
await ethers.getContractFactory("SmartWallet", {
libraries: {
ERC1271Lib: ERC1271Lib.address,
ERC20Lib: ERC20Lib.address,
GuardianLib: GuardianLib.address,
InheritanceLib: InheritanceLib.address,
LockLib: LockLib.address,
MetaTxLib: MetaTxLib.address,
QuotaLib: QuotaLib.address,
RecoverLib: RecoverLib.address,
UpgradeLib: UpgradeLib.address,
WhitelistLib: WhitelistLib.address
}
})
).deploy(ethers.constants.AddressZero);

return smartWallet;
}
Expand All @@ -63,62 +71,70 @@ export async function newWalletFactoryContract(deployer?: string) {
let smartWallet: Contract;
let walletFactory: Contract;

testPriceOracle = await (await ethers.getContractFactory(
"TestPriceOracle"
)).deploy();
testPriceOracle = await (
await ethers.getContractFactory("TestPriceOracle")
).deploy();

const ERC1271Lib = await (await ethers.getContractFactory(
"ERC1271Lib"
)).deploy();
const ERC1271Lib = await (
await ethers.getContractFactory("ERC1271Lib")
).deploy();
const ERC20Lib = await (await ethers.getContractFactory("ERC20Lib")).deploy();
const GuardianLib = await (await ethers.getContractFactory(
"GuardianLib"
)).deploy();
const InheritanceLib = await (await ethers.getContractFactory(
"InheritanceLib"
)).deploy();
const LockLib = await (await ethers.getContractFactory("LockLib", {
libraries: {
GuardianLib: GuardianLib.address
}
})).deploy();
const MetaTxLib = await (await ethers.getContractFactory("MetaTxLib", {
libraries: {
ERC20Lib: ERC20Lib.address
}
})).deploy();
const GuardianLib = await (
await ethers.getContractFactory("GuardianLib")
).deploy();
const InheritanceLib = await (
await ethers.getContractFactory("InheritanceLib")
).deploy();
const LockLib = await (
await ethers.getContractFactory("LockLib", {
libraries: {
GuardianLib: GuardianLib.address
}
})
).deploy();
const MetaTxLib = await (
await ethers.getContractFactory("MetaTxLib", {
libraries: {
ERC20Lib: ERC20Lib.address
}
})
).deploy();
const QuotaLib = await (await ethers.getContractFactory("QuotaLib")).deploy();
const RecoverLib = await (await ethers.getContractFactory("RecoverLib", {
libraries: {
GuardianLib: GuardianLib.address
}
})).deploy();
const UpgradeLib = await (await ethers.getContractFactory(
"UpgradeLib"
)).deploy();
const WhitelistLib = await (await ethers.getContractFactory(
"WhitelistLib"
)).deploy();
const RecoverLib = await (
await ethers.getContractFactory("RecoverLib", {
libraries: {
GuardianLib: GuardianLib.address
}
})
).deploy();
const UpgradeLib = await (
await ethers.getContractFactory("UpgradeLib")
).deploy();
const WhitelistLib = await (
await ethers.getContractFactory("WhitelistLib")
).deploy();

smartWallet = await (await ethers.getContractFactory("SmartWallet", {
libraries: {
ERC1271Lib: ERC1271Lib.address,
ERC20Lib: ERC20Lib.address,
GuardianLib: GuardianLib.address,
InheritanceLib: InheritanceLib.address,
LockLib: LockLib.address,
MetaTxLib: MetaTxLib.address,
QuotaLib: QuotaLib.address,
RecoverLib: RecoverLib.address,
UpgradeLib: UpgradeLib.address,
WhitelistLib: WhitelistLib.address
}
})).deploy(ethers.constants.AddressZero /*testPriceOracle.address*/);
smartWallet = await (
await ethers.getContractFactory("SmartWallet", {
libraries: {
ERC1271Lib: ERC1271Lib.address,
ERC20Lib: ERC20Lib.address,
GuardianLib: GuardianLib.address,
InheritanceLib: InheritanceLib.address,
LockLib: LockLib.address,
MetaTxLib: MetaTxLib.address,
QuotaLib: QuotaLib.address,
RecoverLib: RecoverLib.address,
UpgradeLib: UpgradeLib.address,
WhitelistLib: WhitelistLib.address
}
})
).deploy(ethers.constants.AddressZero /*testPriceOracle.address*/);
console.log("smartWallet address:", smartWallet.address);

walletFactory = await (await ethers.getContractFactory(
"WalletFactory"
)).deploy(smartWallet.address);
walletFactory = await (
await ethers.getContractFactory("WalletFactory")
).deploy(smartWallet.address);

await walletFactory.deployed();

Expand Down Expand Up @@ -171,29 +187,28 @@ export async function newWallet(
// const allEvents = await getAllEvent(walletFactory, tx.blockNumber);
// console.log(allEvents);

const smartWallet = await (await ethers.getContractFactory("SmartWallet", {
libraries: {
ERC1271Lib: ethers.constants.AddressZero,
ERC20Lib: ethers.constants.AddressZero,
GuardianLib: ethers.constants.AddressZero,
InheritanceLib: ethers.constants.AddressZero,
LockLib: ethers.constants.AddressZero,
MetaTxLib: ethers.constants.AddressZero,
QuotaLib: ethers.constants.AddressZero,
RecoverLib: ethers.constants.AddressZero,
UpgradeLib: ethers.constants.AddressZero,
WhitelistLib: ethers.constants.AddressZero
}
})).attach(walletAddrComputed);
const smartWallet = await (
await ethers.getContractFactory("SmartWallet", {
libraries: {
ERC1271Lib: ethers.constants.AddressZero,
ERC20Lib: ethers.constants.AddressZero,
GuardianLib: ethers.constants.AddressZero,
InheritanceLib: ethers.constants.AddressZero,
LockLib: ethers.constants.AddressZero,
MetaTxLib: ethers.constants.AddressZero,
QuotaLib: ethers.constants.AddressZero,
RecoverLib: ethers.constants.AddressZero,
UpgradeLib: ethers.constants.AddressZero,
WhitelistLib: ethers.constants.AddressZero
}
})
).attach(walletAddrComputed);

// console.log("SmartWallet:", smartWallet);
return smartWallet;
}

export async function getAllEvent(
contract: any,
fromBlock: number
) {
export async function getAllEvent(contract: any, fromBlock: number) {
const events = await contract.queryFilter(
{ address: contract.address },
fromBlock
Expand Down Expand Up @@ -250,9 +265,12 @@ export async function getContractABI(contractName: string) {
});
}

export function sortSignersAndSignatures(signers: string[], signatures: Buffer[]) {
export function sortSignersAndSignatures(
signers: string[],
signatures: Buffer[]
) {
const sigMap = new Map();
signers.forEach(function(signer, i){
signers.forEach(function(signer, i) {
sigMap.set(signer, signatures[i]);
});

Expand Down
17 changes: 4 additions & 13 deletions packages/hebao_v2/test/helper/signatureUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,13 @@ export function signChangeMasterCopy(
signer: string
) {
const domainSeprator = eip712.hash("LoopringWallet", "2.0.0", masterCopy);
const TYPE_STR = "changeMasterCopy(address wallet,uint256 validUntil,address masterCopy)";
const TYPE_STR =
"changeMasterCopy(address wallet,uint256 validUntil,address masterCopy)";
const CREATE_WALLET_TYPEHASH = ethUtil.keccak(Buffer.from(TYPE_STR));

const encodedRequest = ethAbi.encodeParameters(
[
"bytes32",
"address",
"uint256",
"address"
],
[
CREATE_WALLET_TYPEHASH,
walletAddress,
validUntil,
newMasterCopy
]
["bytes32", "address", "uint256", "address"],
[CREATE_WALLET_TYPEHASH, walletAddress, validUntil, newMasterCopy]
);

const hash = eip712.hashPacked(domainSeprator, encodedRequest);
Expand Down
Loading