Skip to content

Commit

Permalink
Added EIP-712 test cases (#687).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Oct 19, 2020
1 parent be4e216 commit 1589353
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 102 deletions.
102 changes: 3 additions & 99 deletions packages/testcases/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,104 +7,8 @@ import zlib from 'browserify-zlib';
import { randomBytes, randomHexString, randomNumber } from "./random";
export { randomBytes, randomHexString, randomNumber };

export module TestCase {
export type BigNumber = {
testcase: string;
value: string | number;
expectedValue: string;
};

export type Hash = {
data: string;
keccak256: string;
sha256: string;
sha512: string;
};

export type HDWalletNode = {
path: string;
address: string;
privateKey: string;
};

export type HDWallet = {
name: string;

seed: string;
locale: string;
password?: string;
entropy: string;
mnemonic: string;

hdnodes: Array<HDWalletNode>
};

export type Nameprep = {
comment: string;
input: Array<number>;
output: Array<number>;
rc?: string;
flags?: string;
};

export type Wallet = {
name: string;
type: "crowdsale" | "secret-storage";
hasAddress: boolean;
address: string;
privateKey: string;
mnemonic?: string;
password?: string;
json: string;
};

export type Wordlist = {
locale: string;
content: string;
};

export type Unit = {
name: string

ether: string,
ether_format: string,

wei: string,

kwei?: string,
mwei?: string,
gwei?: string,
szabo?: string,
finney?: string,
satoshi?: string

kwei_format?: string,
mwei_format?: string,
gwei_format?: string,
szabo_format?: string,
finney_format?: string,
satoshi_format?: string
}

export type SignedTransaction = {
name: string;
accountAddress: string;
privateKey: string;

signedTransaction: string
unsignedTransaction: string;

signedTransactionChainId5: string
unsignedTransactionChainId5: string;

nonce: number;
gasLimit: string;
gasPrice: string;
to: string;
value: string;
data: string;
};
}
import * as TestCase from "./testcases";
export { TestCase };

export function saveTests(tag: string, data: any) {
//let filename = path.resolve(__dirname, 'testcases', tag + '.json.gz');
Expand All @@ -115,7 +19,7 @@ export function saveTests(tag: string, data: any) {
console.log('Save testcase: ' + filename);
}

export function loadTests(tag: string): any {
export function loadTests<T = any>(tag: string): T {
let filename = path.resolve(__dirname, '../testcases', tag + '.json.gz');
return JSON.parse(zlib.gunzipSync(fs.readFileSync(filename)).toString());
}
Expand Down
130 changes: 130 additions & 0 deletions packages/testcases/src.ts/testcases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@

/*
export interface TestCase {
name: string;
};
*/

export interface BigNumber {
testcase: string;
value: string | number;
expectedValue: string;
};

export interface Hash {
data: string;
keccak256: string;
sha256: string;
sha512: string;
};

export interface HDWalletNode {
path: string;
address: string;
privateKey: string;
};

export interface HDWallet {
name: string;

seed: string;
locale: string;
password?: string;
entropy: string;
mnemonic: string;

hdnodes: Array<HDWalletNode>
};

export interface Nameprep {
comment: string;
input: Array<number>;
output: Array<number>;
rc?: string;
flags?: string;
};

export interface Wallet {
name: string;

type: "crowdsale" | "secret-storage";
hasAddress: boolean;
address: string;
privateKey: string;
mnemonic?: string;
password?: string;
json: string;
};

export interface Wordlist {
locale: string;
content: string;
};

export interface Unit {
name: string;

ether: string,
ether_format: string,
wei: string,

kwei?: string,
mwei?: string,
gwei?: string,
szabo?: string,
finney?: string,
satoshi?: string

kwei_format?: string,
mwei_format?: string,
gwei_format?: string,
szabo_format?: string,
finney_format?: string,
satoshi_format?: string
};

export interface SignedTransaction {
name: string;

accountAddress: string;
privateKey: string;

signedTransaction: string
unsignedTransaction: string;

signedTransactionChainId5: string
unsignedTransactionChainId5: string;

nonce: number;
gasLimit: string;
gasPrice: string;
to: string;
value: string;
data: string;
};

export interface Eip712 {
name: string;

domain: {
name: string;
version?: string;
chainId?: number;
verifyingContract?: string;
salt?: string;
},
primaryType: string;
types: Record<string, Array<{ name: string, type: string }>>;
data: Record<string, any>;
encoded: string;
digest: string;

// Only random tests
type?: string;
seed?: string;

// Not all testcases have these
privateKey?: string;
signature?: string;
};

Binary file added packages/testcases/testcases/eip712.json.gz
Binary file not shown.
32 changes: 29 additions & 3 deletions packages/tests/src.ts/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -732,21 +732,47 @@ describe("Logger", function() {
});
});

/*
describe("Base58 Coder", function() {
it("decodes", function() {
assert.equal(ethers.utils.Base58.decode("JxF12TrwUP45BMd"), "Hello World");
assert.equal(ethers.utils.toUtf8String(ethers.utils.base58.decode("JxF12TrwUP45BMd")), "Hello World");
});

it("encodes", function() {
assert.equal(ethers.utils.Base58.encode("Hello World"), "JxF12TrwUP45BMd");
assert.equal(ethers.utils.base58.encode(ethers.utils.toUtf8Bytes("Hello World")), "JxF12TrwUP45BMd");
});
});

/*
describe("Web Fetch", function() {
it("fetches JSON", async function() {
const url = "https:/\/api.etherscan.io/api?module=stats&action=ethprice&apikey=9D13ZE7XSBTJ94N9BNJ2MA33VMAY2YPIRB";
const getData = ethers.utils.fetchJson(url)
});
});
*/

describe("EIP-712", function() {
const tests = loadTests<Array<TestCase.Eip712>>("eip712");

tests.forEach((test) => {
it(`encoding ${ test.name }`, function() {
const encoder = ethers.utils._TypedDataEncoder.from(test.types);
assert.equal(encoder.primaryType, test.primaryType, "instance.primaryType");
assert.equal(encoder.encode(test.data), test.encoded, "instance.encode()");

//console.log(test);
assert.equal(ethers.utils._TypedDataEncoder.getPrimaryType(test.types), test.primaryType, "getPrimaryType");
assert.equal(ethers.utils._TypedDataEncoder.hash(test.domain, test.types, test.data), test.digest, "digest");
});
});

tests.forEach((test) => {
if (!test.privateKey) { return; }
it(`signing ${ test.name }`, async function() {
const wallet = new ethers.Wallet(test.privateKey);
const signature = await wallet._signTypedData(test.domain, test.types, test.data);
assert.equal(signature, test.signature, "signature");
});
});
});

0 comments on commit 1589353

Please sign in to comment.