-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added EIP-712 multi-dimensional array support (#687).
- Loading branch information
Showing
2 changed files
with
62 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,19 @@ | ||
"use strict"; | ||
|
||
import { Bytes, concat, hexlify } from "@ethersproject/bytes"; | ||
import { nameprep, toUtf8Bytes } from "@ethersproject/strings"; | ||
import { keccak256 } from "@ethersproject/keccak256"; | ||
|
||
import { Logger } from "@ethersproject/logger"; | ||
import { version } from "./_version"; | ||
const logger = new Logger(version); | ||
import { id } from "./id"; | ||
import { isValidName, namehash } from "./namehash"; | ||
import { hashMessage, messagePrefix } from "./message"; | ||
|
||
import { TypedDataEncoder as _TypedDataEncoder } from "./typed-data"; | ||
|
||
import { id } from "./id"; | ||
|
||
export { | ||
_TypedDataEncoder, | ||
id, | ||
|
||
id | ||
} | ||
|
||
/////////////////////////////// | ||
namehash, | ||
isValidName, | ||
|
||
const Zeros = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); | ||
const Partition = new RegExp("^((.*)\\.)?([^.]+)$"); | ||
|
||
export function isValidName(name: string): boolean { | ||
try { | ||
const comps = name.split("."); | ||
for (let i = 0; i < comps.length; i++) { | ||
if (nameprep(comps[i]).length === 0) { | ||
throw new Error("empty") | ||
} | ||
} | ||
return true; | ||
} catch (error) { } | ||
return false; | ||
} | ||
messagePrefix, | ||
hashMessage, | ||
|
||
export function namehash(name: string): string { | ||
/* istanbul ignore if */ | ||
if (typeof(name) !== "string") { | ||
logger.throwArgumentError("invalid address - " + String(name), "name", name); | ||
} | ||
|
||
let result: string | Uint8Array = Zeros; | ||
while (name.length) { | ||
const partition = name.match(Partition); | ||
const label = toUtf8Bytes(nameprep(partition[3])); | ||
result = keccak256(concat([result, keccak256(label)])); | ||
|
||
name = partition[2] || ""; | ||
} | ||
|
||
return hexlify(result); | ||
} | ||
|
||
|
||
export const messagePrefix = "\x19Ethereum Signed Message:\n"; | ||
|
||
export function hashMessage(message: Bytes | string): string { | ||
if (typeof(message) === "string") { message = toUtf8Bytes(message); } | ||
return keccak256(concat([ | ||
toUtf8Bytes(messagePrefix), | ||
toUtf8Bytes(String(message.length)), | ||
message | ||
])); | ||
_TypedDataEncoder, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters