Added EIP-712 test cases (#687).
This commit is contained in:
parent
be4e2164e6
commit
15893537c3
@ -7,104 +7,8 @@ import zlib from 'browserify-zlib';
|
|||||||
import { randomBytes, randomHexString, randomNumber } from "./random";
|
import { randomBytes, randomHexString, randomNumber } from "./random";
|
||||||
export { randomBytes, randomHexString, randomNumber };
|
export { randomBytes, randomHexString, randomNumber };
|
||||||
|
|
||||||
export module TestCase {
|
import * as TestCase from "./testcases";
|
||||||
export type BigNumber = {
|
export { TestCase };
|
||||||
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;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function saveTests(tag: string, data: any) {
|
export function saveTests(tag: string, data: any) {
|
||||||
//let filename = path.resolve(__dirname, 'testcases', tag + '.json.gz');
|
//let filename = path.resolve(__dirname, 'testcases', tag + '.json.gz');
|
||||||
@ -115,7 +19,7 @@ export function saveTests(tag: string, data: any) {
|
|||||||
console.log('Save testcase: ' + filename);
|
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');
|
let filename = path.resolve(__dirname, '../testcases', tag + '.json.gz');
|
||||||
return JSON.parse(zlib.gunzipSync(fs.readFileSync(filename)).toString());
|
return JSON.parse(zlib.gunzipSync(fs.readFileSync(filename)).toString());
|
||||||
}
|
}
|
||||||
|
130
packages/testcases/src.ts/testcases.ts
Normal file
130
packages/testcases/src.ts/testcases.ts
Normal file
@ -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;
|
||||||
|
};
|
||||||
|
|
BIN
packages/testcases/testcases/eip712.json.gz
Normal file
BIN
packages/testcases/testcases/eip712.json.gz
Normal file
Binary file not shown.
@ -732,17 +732,17 @@ describe("Logger", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
/*
|
|
||||||
describe("Base58 Coder", function() {
|
describe("Base58 Coder", function() {
|
||||||
it("decodes", 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() {
|
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() {
|
describe("Web Fetch", function() {
|
||||||
it("fetches JSON", async function() {
|
it("fetches JSON", async function() {
|
||||||
const url = "https:/\/api.etherscan.io/api?module=stats&action=ethprice&apikey=9D13ZE7XSBTJ94N9BNJ2MA33VMAY2YPIRB";
|
const url = "https:/\/api.etherscan.io/api?module=stats&action=ethprice&apikey=9D13ZE7XSBTJ94N9BNJ2MA33VMAY2YPIRB";
|
||||||
@ -750,3 +750,29 @@ describe("Web Fetch", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
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");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user