diff --git a/packages/testcases/src.ts/index.ts b/packages/testcases/src.ts/index.ts index dffac0b0a..2243a6e10 100644 --- a/packages/testcases/src.ts/index.ts +++ b/packages/testcases/src.ts/index.ts @@ -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 - }; - - export type Nameprep = { - comment: string; - input: Array; - output: Array; - 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'); @@ -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(tag: string): T { let filename = path.resolve(__dirname, '../testcases', tag + '.json.gz'); return JSON.parse(zlib.gunzipSync(fs.readFileSync(filename)).toString()); } diff --git a/packages/testcases/src.ts/testcases.ts b/packages/testcases/src.ts/testcases.ts new file mode 100644 index 000000000..4dfd78ef4 --- /dev/null +++ b/packages/testcases/src.ts/testcases.ts @@ -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 +}; + +export interface Nameprep { + comment: string; + input: Array; + output: Array; + 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>; + data: Record; + encoded: string; + digest: string; + + // Only random tests + type?: string; + seed?: string; + + // Not all testcases have these + privateKey?: string; + signature?: string; +}; + diff --git a/packages/testcases/testcases/eip712.json.gz b/packages/testcases/testcases/eip712.json.gz new file mode 100644 index 000000000..1e385a1b5 Binary files /dev/null and b/packages/testcases/testcases/eip712.json.gz differ diff --git a/packages/tests/src.ts/test-utils.ts b/packages/tests/src.ts/test-utils.ts index 2e77544af..469ac9fb2 100644 --- a/packages/tests/src.ts/test-utils.ts +++ b/packages/tests/src.ts/test-utils.ts @@ -732,17 +732,17 @@ 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"; @@ -750,3 +750,29 @@ describe("Web Fetch", function() { }); }); */ + +describe("EIP-712", function() { + const tests = loadTests>("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"); + }); + }); +}); +