diff --git a/src.ts/_tests/test-crypto.ts b/src.ts/_tests/test-crypto.ts index b8b1b17aa..21541c1fe 100644 --- a/src.ts/_tests/test-crypto.ts +++ b/src.ts/_tests/test-crypto.ts @@ -98,141 +98,3 @@ describe("test hmac", function() { }); }); }); - -/* -describe("test registration", function() { - let hijack = ""; - function getHijack(algo: string) { - return function(...args: Array) { - hijack = `hijacked ${ algo }: ${ JSON.stringify(args) }`; - return "0x42"; - } - } - - it("hijacks keccak256", function() { - const initial = keccak256("0x"); - - keccak256.register(getHijack("kecak256")); - assert.equal(keccak256("0x"), "0x42"); - assert.equal(hijack, 'hijacked kecak256: [{}]'); - - keccak256.register(keccak256._); - assert.equal(keccak256("0x"), initial); - - keccak256.lock(); - - assert.throws(function() { - keccak256.register(getHijack("test")); - }, function(error) { - return (error.message === "keccak256 is locked"); - }); - }); - - it("hijacks sha256", function() { - const initial = sha256("0x"); - - sha256.register(getHijack("sha256")); - assert.equal(sha256("0x"), "0x42"); - assert.equal(hijack, 'hijacked sha256: [{}]'); - - sha256.register(sha256._); - assert.equal(sha256("0x"), initial); - - sha256.lock(); - - assert.throws(function() { - sha256.register(getHijack("test")); - }, function(error) { - return (error.message === "sha256 is locked"); - }); - }); - - it("hijacks sha512", function() { - const initial = sha512("0x"); - - sha512.register(getHijack("sha512")); - assert.equal(sha512("0x"), "0x42"); - assert.equal(hijack, 'hijacked sha512: [{}]'); - - sha512.register(sha512._); - assert.equal(sha512("0x"), initial); - - sha512.lock(); - - assert.throws(function() { - sha512.register(getHijack("test")); - }, function(error) { - return (error.message === "sha512 is locked"); - }); - }); - - it("hijacks pbkdf2", function() { - const initial = pbkdf2("0x", "0x", 1024, 32, "sha256"); - - pbkdf2.register(getHijack("pbkdf2")); - assert.equal(pbkdf2("0x", "0x", 1024, 32, "sha256"), "0x42"); - assert.equal(hijack, 'hijacked pbkdf2: [{},{},1024,32,"sha256"]'); - - pbkdf2.register(pbkdf2._); - assert.equal(pbkdf2("0x", "0x", 1024, 32, "sha256"), initial); - - pbkdf2.lock(); - - assert.throws(function() { - pbkdf2.register(getHijack("test")); - }, function(error) { - return (error.message === "pbkdf2 is locked"); - }); - }); - - it("hijacks scryptSync", function() { - - function getHijack(...args: Array) { - hijack = `hijacked scryptSync: ${ JSON.stringify(args) }`; - return new Uint8Array([ 0x42 ]); - } - - const initial = scryptSync("0x", "0x", 1024, 8, 1, 32); - - scryptSync.register(getHijack); - assert.equal(scryptSync("0x", "0x", 1024, 8, 1, 32), "0x42"); - assert.equal(hijack, 'hijacked scryptSync: [{},{},1024,8,1,32]'); - - scryptSync.register(scryptSync._); - assert.equal(scryptSync("0x", "0x", 1024, 8, 1, 32), initial); - - scryptSync.lock(); - - assert.throws(function() { - scryptSync.register(getHijack); - }, function(error) { - return (error.message === "scryptSync is locked"); - }); - }); - - it("hijacks scrypt", async function() { - function getHijack(...args: Array) { - hijack = `hijacked scrypt: ${ JSON.stringify(args) }`; - return Promise.resolve(new Uint8Array([ 0x42 ])); - } - - const initial = await scrypt("0x", "0x", 1024, 8, 1, 32); - - scrypt.register(getHijack); - assert.equal(await scrypt("0x", "0x", 1024, 8, 1, 32), "0x42"); - assert.equal(hijack, 'hijacked scrypt: [{},{},1024,8,1,32,null]'); - - scrypt.register(scrypt._); - assert.equal(await scrypt("0x", "0x", 1024, 8, 1, 32), initial); - - scrypt.lock(); - - assert.throws(function() { - scrypt.register(getHijack); - }, function(error) { - return (error.message === "scrypt is locked"); - }); - }); - -}); -*/ diff --git a/src.ts/_tests/test-hash.ts b/src.ts/_tests/test-hash.ts index f8e4d314d..41605996f 100644 --- a/src.ts/_tests/test-hash.ts +++ b/src.ts/_tests/test-hash.ts @@ -1,7 +1,15 @@ -/* import assert from "assert"; + +import { + hashMessage, + solidityPackedKeccak256, solidityPackedSha256 +} from "../index.js"; + import { loadTests } from "./utils.js" -import type { TestCaseNamehash } from "./types.js"; + +import type { TestCaseSolidityHash } from "./types.js"; + +/* import { dnsEncode, isValidName, namehash } from "../index.js"; @@ -108,3 +116,45 @@ describe("Tests DNS Names", function() { }); */ + +describe("Test EIP-191 Personal Message Hash", function() { + const tests = [ + { + test: "hello-world", + message: "Hello World", + hash: "0xa1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2" + }, + { + test: "binary-message", + message: new Uint8Array([ 0x42, 0x43 ]), + hash: "0x0d3abc18ec299cf9b42ba439ac6f7e3e6ec9f5c048943704e30fc2d9c7981438" + }, + { + test: "hex-looking-string", + message: "0x4243", + hash: "0x6d91b221f765224b256762dcba32d62209cf78e9bebb0a1b758ca26c76db3af4" + } + ]; + + for (const test of tests) { + it(`tests hashMessage: ${ test.test }`, function() { + assert.equal(hashMessage(test.message), test.hash); + }); + } +}) + +describe("Test Solidity Hash functions", function() { + const tests = loadTests("solidity-hashes"); + + for (const test of tests) { + it(`computes the solidity keccak256: ${ test.name }`, function() { + assert.equal(solidityPackedKeccak256(test.types, test.values), test.keccak256); + }); + } + + for (const test of tests) { + it(`computes the solidity sha256: ${ test.name }`, function() { + assert.equal(solidityPackedSha256(test.types, test.values), test.sha256); + }); + } +}) diff --git a/src.ts/_tests/test-utils-misc.ts b/src.ts/_tests/test-utils-misc.ts new file mode 100644 index 000000000..ff7ef5772 --- /dev/null +++ b/src.ts/_tests/test-utils-misc.ts @@ -0,0 +1,33 @@ +import assert from "assert"; + +import { + decodeBase64, encodeBase64, + toUtf8Bytes +} from "../index.js"; + + +describe("Base64 Coding", function() { + const tests = [ + { + name: "wikipedia", + plaintext: toUtf8Bytes("Many hands make light work."), + encoded: "TWFueSBoYW5kcyBtYWtlIGxpZ2h0IHdvcmsu" + } + ]; + + for (const test of tests) { + it(`encodes base64: ${ test.name }`, function() { + assert.equal(encodeBase64(test.plaintext), test.encoded); + }); + } + + for (const test of tests) { + it(`decodes base64: ${ test.name }`, function() { + const decoded = decodeBase64(test.encoded); + assert.equal(decoded.length, test.plaintext.length, "data.length"); + for (let i = 0; i < decoded.length; i++) { + assert.equal(decoded[i], test.plaintext[i]); + } + }); + } +}) diff --git a/src.ts/_tests/types.ts b/src.ts/_tests/types.ts index 617cb4037..be9d18b58 100644 --- a/src.ts/_tests/types.ts +++ b/src.ts/_tests/types.ts @@ -138,6 +138,14 @@ export interface TestCaseTypedData { signature?: string; } +export interface TestCaseSolidityHash { + name: string; + types: Array; + keccak256: string; + ripemd160: string; + sha256: string; + values: Array +} ///////////////////////////// // rlp diff --git a/testcases/solidity-hashes.json.gz b/testcases/solidity-hashes.json.gz new file mode 100644 index 000000000..07b379a3a Binary files /dev/null and b/testcases/solidity-hashes.json.gz differ