ethers.js/packages/testcases/generation/lib/eip712-test.js

129 lines
4.7 KiB
JavaScript
Raw Permalink Normal View History

2020-10-18 23:19:16 -04:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const eth_sig_util_1 = require("eth-sig-util");
const abi_test_1 = require("./abi-test");
const test_1 = require("./test");
function fill(testcase) {
const domainType = [];
if (testcase.domain.name != null) {
domainType.push({ name: "name", type: "string" });
}
if (testcase.domain.version != null) {
domainType.push({ name: "version", type: "string" });
}
if (testcase.domain.chainId != null) {
domainType.push({ name: "chainId", type: "uint256" });
}
if (testcase.domain.verifyingContract != null) {
domainType.push({ name: "verifyingContract", type: "address" });
}
if (testcase.domain.salt != null) {
domainType.push({ name: "salt", type: "bytes32" });
}
const typesWithDomain = {
EIP712Domain: domainType
};
for (const key in testcase.types) {
typesWithDomain[key] = testcase.types[key];
}
testcase.encoded = "0x" + eth_sig_util_1.TypedDataUtils.encodeData(testcase.primaryType, testcase.data, testcase.types).toString("hex");
testcase.digest = "0x" + eth_sig_util_1.TypedDataUtils.sign({
types: typesWithDomain,
domain: testcase.domain,
primaryType: testcase.primaryType,
message: testcase.data
}, true).toString("hex");
return testcase;
}
class Eip712Test extends abi_test_1.AbstractAbiTest {
generateTest() {
const type = this.randomType("tuple");
const types = {};
function spelunk(type) {
if (type.struct) {
types[type.struct.split("[")[0]] = type.components.map((t) => {
spelunk(t);
return { name: t.name, type: (t.struct || t.type) };
});
;
}
}
spelunk(type);
const primaryType = type.struct;
const data = type.create();
const domain = {};
if (this.randomChoice([false, true])) {
domain.name = this.randomString(1, 64);
}
if (this.randomChoice([false, true])) {
domain.version = [
this.randomInteger(0, 50),
this.randomInteger(0, 50),
this.randomInteger(0, 50),
].join(".");
}
if (this.randomChoice([false, true])) {
domain.chainId = this.randomInteger(0, 1337);
}
if (this.randomChoice([false, true])) {
domain.verifyingContract = this.randomAddress();
}
if (this.randomChoice([false, true])) {
domain.salt = this.randomHexString(32);
}
return fill({
domain,
type: type.type,
seed: this.seed,
primaryType, types, data
});
}
}
exports.Eip712Test = Eip712Test;
if (require.main === module) {
const tests = [];
for (let i = 0; i < 1024; i++) {
const test = new Eip712Test(String(i));
tests.push(test.generateTest());
}
tests.sort((a, b) => (a.type.length - b.type.length));
tests.forEach((t, i) => { t.name = `random-${i}`; });
tests.push({
name: "EIP712 example",
domain: {
name: 'Ether Mail',
version: '1',
chainId: 1,
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC'
},
primaryType: "Mail",
types: {
Person: [
{ name: 'name', type: 'string' },
{ name: 'wallet', type: 'address' }
],
Mail: [
{ name: 'from', type: 'Person' },
{ name: 'to', type: 'Person' },
{ name: 'contents', type: 'string' }
]
},
data: {
from: {
name: 'Cow',
wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826'
},
to: {
name: 'Bob',
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB'
},
contents: 'Hello, Bob!'
},
encoded: "0xa0cedeb2dc280ba39b857546d74f5549c3a1d7bdc2dd96bf881f76108e23dac2fc71e5fa27ff56c350aa531bc129ebdf613b772b6604664f5d8dbe21b85eb0c8cd54f074a4af31b4411ff6a60c9719dbd559c221c8ac3492d9d872b041d703d1b5aadf3154a261abdd9086fc627b61efca26ae5702701d05cd2305f7c52a2fc8",
digest: "0xbe609aee343fb3c4b28e1df9e632fca64fcfaede20f02e86244efddf30957bd2",
privateKey: "0xc85ef7d79691fe79573b1a7064c19c1a9819ebdbd1faaab1a8ec92344438aaf4",
signature: "0x4355c47d63924e8a72e509b65029052eb6c299d53a04e167c5775fd466751c9d07299936d304c153f6443dfa05f40ff007d72911b6f72307f996231605b915621c"
});
test_1.saveTests("eip712", tests);
}