2019-05-14 18:48:48 -04:00
|
|
|
"use strict";
|
2019-08-25 02:39:20 -04:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
2019-05-14 18:48:48 -04:00
|
|
|
///////////////////////////////
|
|
|
|
function isHexable(value) {
|
|
|
|
return !!(value.toHexString);
|
|
|
|
}
|
|
|
|
function addSlice(array) {
|
|
|
|
if (array.slice) {
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
array.slice = function () {
|
2019-11-20 18:57:38 +09:00
|
|
|
const args = Array.prototype.slice.call(arguments);
|
2019-05-14 18:48:48 -04:00
|
|
|
return addSlice(new Uint8Array(Array.prototype.slice.apply(array, args)));
|
|
|
|
};
|
|
|
|
return array;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function isBytesLike(value) {
|
2019-05-14 18:48:48 -04:00
|
|
|
return ((isHexString(value) && !(value.length % 2)) || isBytes(value));
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function isBytes(value) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (value == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (value.constructor === Uint8Array) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (typeof (value) === "string") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (value.length == null) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
for (let i = 0; i < value.length; i++) {
|
2019-11-20 18:57:38 +09:00
|
|
|
const v = value[i];
|
2019-05-14 18:48:48 -04:00
|
|
|
if (v < 0 || v >= 256 || (v % 1)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function arrayify(value, options) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (!options) {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
if (typeof (value) === "number") {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.checkSafeUint53(value, "invalid arrayify value");
|
2019-11-20 18:57:38 +09:00
|
|
|
const result = [];
|
2019-05-14 18:48:48 -04:00
|
|
|
while (value) {
|
|
|
|
result.unshift(value & 0xff);
|
2020-03-21 12:48:22 -04:00
|
|
|
value = parseInt(String(value / 256));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
if (result.length === 0) {
|
|
|
|
result.push(0);
|
|
|
|
}
|
|
|
|
return addSlice(new Uint8Array(result));
|
|
|
|
}
|
|
|
|
if (options.allowMissingPrefix && typeof (value) === "string" && value.substring(0, 2) !== "0x") {
|
|
|
|
value = "0x" + value;
|
|
|
|
}
|
|
|
|
if (isHexable(value)) {
|
|
|
|
value = value.toHexString();
|
|
|
|
}
|
|
|
|
if (isHexString(value)) {
|
2019-08-25 02:39:20 -04:00
|
|
|
let hex = value.substring(2);
|
2019-09-28 02:36:19 -04:00
|
|
|
if (hex.length % 2) {
|
|
|
|
if (options.hexPad === "left") {
|
|
|
|
hex = "0x0" + hex.substring(2);
|
|
|
|
}
|
|
|
|
else if (options.hexPad === "right") {
|
|
|
|
hex += "0";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logger.throwArgumentError("hex data is odd-length", "value", value);
|
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-11-20 18:57:38 +09:00
|
|
|
const result = [];
|
2019-08-25 02:39:20 -04:00
|
|
|
for (let i = 0; i < hex.length; i += 2) {
|
2019-05-14 18:48:48 -04:00
|
|
|
result.push(parseInt(hex.substring(i, i + 2), 16));
|
|
|
|
}
|
|
|
|
return addSlice(new Uint8Array(result));
|
|
|
|
}
|
|
|
|
if (isBytes(value)) {
|
|
|
|
return addSlice(new Uint8Array(value));
|
|
|
|
}
|
2019-08-02 02:10:58 -04:00
|
|
|
return logger.throwArgumentError("invalid arrayify value", "value", value);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function concat(items) {
|
2019-11-20 18:57:38 +09:00
|
|
|
const objects = items.map(item => arrayify(item));
|
|
|
|
const length = objects.reduce((accum, item) => (accum + item.length), 0);
|
|
|
|
const result = new Uint8Array(length);
|
2019-08-25 02:39:20 -04:00
|
|
|
objects.reduce((offset, object) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
result.set(object, offset);
|
|
|
|
return offset + object.length;
|
|
|
|
}, 0);
|
|
|
|
return addSlice(result);
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function stripZeros(value) {
|
|
|
|
let result = arrayify(value);
|
2019-05-14 18:48:48 -04:00
|
|
|
if (result.length === 0) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
// Find the first non-zero entry
|
2019-08-25 02:39:20 -04:00
|
|
|
let start = 0;
|
2019-05-14 18:48:48 -04:00
|
|
|
while (start < result.length && result[start] === 0) {
|
|
|
|
start++;
|
|
|
|
}
|
|
|
|
// If we started with zeros, strip them
|
|
|
|
if (start) {
|
|
|
|
result = result.slice(start);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function zeroPad(value, length) {
|
2019-05-14 18:48:48 -04:00
|
|
|
value = arrayify(value);
|
|
|
|
if (value.length > length) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("value out of range", "value", arguments[0]);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-11-20 18:57:38 +09:00
|
|
|
const result = new Uint8Array(length);
|
2019-05-14 18:48:48 -04:00
|
|
|
result.set(value, length - value.length);
|
|
|
|
return addSlice(result);
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function isHexString(value, length) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (typeof (value) !== "string" || !value.match(/^0x[0-9A-Fa-f]*$/)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (length && value.length !== 2 + 2 * length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
const HexCharacters = "0123456789abcdef";
|
|
|
|
export function hexlify(value, options) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (!options) {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
if (typeof (value) === "number") {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.checkSafeUint53(value, "invalid hexlify value");
|
2019-08-25 02:39:20 -04:00
|
|
|
let hex = "";
|
2019-05-14 18:48:48 -04:00
|
|
|
while (value) {
|
|
|
|
hex = HexCharacters[value & 0x0f] + hex;
|
|
|
|
value = Math.floor(value / 16);
|
|
|
|
}
|
|
|
|
if (hex.length) {
|
|
|
|
if (hex.length % 2) {
|
|
|
|
hex = "0" + hex;
|
|
|
|
}
|
|
|
|
return "0x" + hex;
|
|
|
|
}
|
|
|
|
return "0x00";
|
|
|
|
}
|
|
|
|
if (options.allowMissingPrefix && typeof (value) === "string" && value.substring(0, 2) !== "0x") {
|
|
|
|
value = "0x" + value;
|
|
|
|
}
|
|
|
|
if (isHexable(value)) {
|
|
|
|
return value.toHexString();
|
|
|
|
}
|
|
|
|
if (isHexString(value)) {
|
2019-09-28 02:36:19 -04:00
|
|
|
if (value.length % 2) {
|
|
|
|
if (options.hexPad === "left") {
|
|
|
|
value = "0x0" + value.substring(2);
|
|
|
|
}
|
|
|
|
else if (options.hexPad === "right") {
|
|
|
|
value += "0";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logger.throwArgumentError("hex data is odd-length", "value", value);
|
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
return value.toLowerCase();
|
|
|
|
}
|
|
|
|
if (isBytes(value)) {
|
2019-08-25 02:39:20 -04:00
|
|
|
let result = "0x";
|
|
|
|
for (let i = 0; i < value.length; i++) {
|
|
|
|
let v = value[i];
|
2019-05-14 18:48:48 -04:00
|
|
|
result += HexCharacters[(v & 0xf0) >> 4] + HexCharacters[v & 0x0f];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2019-08-02 02:10:58 -04:00
|
|
|
return logger.throwArgumentError("invalid hexlify value", "value", value);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
function unoddify(value: BytesLike | Hexable | number): BytesLike | Hexable | number {
|
|
|
|
if (typeof(value) === "string" && value.length % 2 && value.substring(0, 2) === "0x") {
|
|
|
|
return "0x0" + value.substring(2);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
*/
|
2019-08-25 02:39:20 -04:00
|
|
|
export function hexDataLength(data) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (typeof (data) !== "string") {
|
|
|
|
data = hexlify(data);
|
|
|
|
}
|
|
|
|
else if (!isHexString(data) || (data.length % 2)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (data.length - 2) / 2;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function hexDataSlice(data, offset, endOffset) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (typeof (data) !== "string") {
|
|
|
|
data = hexlify(data);
|
|
|
|
}
|
|
|
|
else if (!isHexString(data) || (data.length % 2)) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("invalid hexData", "value", data);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
offset = 2 + 2 * offset;
|
|
|
|
if (endOffset != null) {
|
|
|
|
return "0x" + data.substring(offset, 2 + 2 * endOffset);
|
|
|
|
}
|
|
|
|
return "0x" + data.substring(offset);
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function hexConcat(items) {
|
|
|
|
let result = "0x";
|
|
|
|
items.forEach((item) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
result += hexlify(item).substring(2);
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function hexValue(value) {
|
2019-11-20 18:57:38 +09:00
|
|
|
const trimmed = hexStripZeros(hexlify(value, { hexPad: "left" }));
|
2019-05-14 18:48:48 -04:00
|
|
|
if (trimmed === "0x") {
|
|
|
|
return "0x0";
|
|
|
|
}
|
|
|
|
return trimmed;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function hexStripZeros(value) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (typeof (value) !== "string") {
|
|
|
|
value = hexlify(value);
|
|
|
|
}
|
|
|
|
if (!isHexString(value)) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("invalid hex string", "value", value);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
value = value.substring(2);
|
2019-08-25 02:39:20 -04:00
|
|
|
let offset = 0;
|
2019-05-14 18:48:48 -04:00
|
|
|
while (offset < value.length && value[offset] === "0") {
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
return "0x" + value.substring(offset);
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function hexZeroPad(value, length) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (typeof (value) !== "string") {
|
|
|
|
value = hexlify(value);
|
|
|
|
}
|
|
|
|
else if (!isHexString(value)) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("invalid hex string", "value", value);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
if (value.length > 2 * length + 2) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("value out of range", "value", arguments[1]);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
while (value.length < 2 * length + 2) {
|
|
|
|
value = "0x0" + value.substring(2);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function splitSignature(signature) {
|
2019-11-20 18:57:38 +09:00
|
|
|
const result = {
|
2019-05-14 18:48:48 -04:00
|
|
|
r: "0x",
|
|
|
|
s: "0x",
|
|
|
|
_vs: "0x",
|
|
|
|
recoveryParam: 0,
|
|
|
|
v: 0
|
|
|
|
};
|
|
|
|
if (isBytesLike(signature)) {
|
2019-11-20 18:57:38 +09:00
|
|
|
const bytes = arrayify(signature);
|
2019-05-14 18:48:48 -04:00
|
|
|
if (bytes.length !== 65) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("invalid signature string; must be 65 bytes", "signature", signature);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-01-11 04:18:28 -05:00
|
|
|
// Get the r, s and v
|
2019-05-14 18:48:48 -04:00
|
|
|
result.r = hexlify(bytes.slice(0, 32));
|
|
|
|
result.s = hexlify(bytes.slice(32, 64));
|
|
|
|
result.v = bytes[64];
|
|
|
|
// Compute recoveryParam from v
|
2020-01-11 04:18:28 -05:00
|
|
|
result.recoveryParam = 1 - (result.v % 2);
|
|
|
|
// Allow a recid to be used as the v
|
|
|
|
if (result.v < 27) {
|
|
|
|
if (result.v === 0 || result.v === 1) {
|
|
|
|
result.v += 27;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logger.throwArgumentError("signature invalid v byte", "signature", signature);
|
|
|
|
}
|
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
// Compute _vs from recoveryParam and s
|
|
|
|
if (result.recoveryParam) {
|
|
|
|
bytes[32] |= 0x80;
|
|
|
|
}
|
|
|
|
result._vs = hexlify(bytes.slice(32, 64));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result.r = signature.r;
|
|
|
|
result.s = signature.s;
|
|
|
|
result.v = signature.v;
|
|
|
|
result.recoveryParam = signature.recoveryParam;
|
|
|
|
result._vs = signature._vs;
|
|
|
|
// If the _vs is available, use it to populate missing s, v and recoveryParam
|
|
|
|
// and verify non-missing s, v and recoveryParam
|
|
|
|
if (result._vs != null) {
|
2020-01-11 04:18:28 -05:00
|
|
|
const vs = zeroPad(arrayify(result._vs), 32);
|
|
|
|
result._vs = hexlify(vs);
|
|
|
|
// Set or check the recid
|
2019-11-20 18:57:38 +09:00
|
|
|
const recoveryParam = ((vs[0] >= 128) ? 1 : 0);
|
2020-01-11 04:18:28 -05:00
|
|
|
if (result.recoveryParam == null) {
|
|
|
|
result.recoveryParam = recoveryParam;
|
|
|
|
}
|
|
|
|
else if (result.recoveryParam !== recoveryParam) {
|
|
|
|
logger.throwArgumentError("signature recoveryParam mismatch _vs", "signature", signature);
|
|
|
|
}
|
|
|
|
// Set or check the s
|
2019-05-14 18:48:48 -04:00
|
|
|
vs[0] &= 0x7f;
|
2019-11-20 18:57:38 +09:00
|
|
|
const s = hexlify(vs);
|
2019-05-14 18:48:48 -04:00
|
|
|
if (result.s == null) {
|
|
|
|
result.s = s;
|
|
|
|
}
|
|
|
|
else if (result.s !== s) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("signature v mismatch _vs", "signature", signature);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-01-11 04:18:28 -05:00
|
|
|
}
|
|
|
|
// Use recid and v to populate each other
|
|
|
|
if (result.recoveryParam == null) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (result.v == null) {
|
2020-01-11 04:18:28 -05:00
|
|
|
logger.throwArgumentError("signature missing v and recoveryParam", "signature", signature);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-01-11 04:18:28 -05:00
|
|
|
else {
|
|
|
|
result.recoveryParam = 1 - (result.v % 2);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-01-11 04:18:28 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (result.v == null) {
|
|
|
|
result.v = 27 + result.recoveryParam;
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-01-11 04:18:28 -05:00
|
|
|
else if (result.recoveryParam !== (1 - (result.v % 2))) {
|
|
|
|
logger.throwArgumentError("signature recoveryParam mismatch v", "signature", signature);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
}
|
2020-01-11 04:18:28 -05:00
|
|
|
if (result.r == null || !isHexString(result.r)) {
|
|
|
|
logger.throwArgumentError("signature missing or invalid r", "signature", signature);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-01-11 04:18:28 -05:00
|
|
|
else {
|
|
|
|
result.r = hexZeroPad(result.r, 32);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-01-11 04:18:28 -05:00
|
|
|
if (result.s == null || !isHexString(result.s)) {
|
|
|
|
logger.throwArgumentError("signature missing or invalid s", "signature", signature);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-01-11 04:18:28 -05:00
|
|
|
else {
|
|
|
|
result.s = hexZeroPad(result.s, 32);
|
|
|
|
}
|
|
|
|
const vs = arrayify(result.s);
|
|
|
|
if (vs[0] >= 128) {
|
|
|
|
logger.throwArgumentError("signature s out of range", "signature", signature);
|
|
|
|
}
|
|
|
|
if (result.recoveryParam) {
|
|
|
|
vs[0] |= 0x80;
|
|
|
|
}
|
|
|
|
const _vs = hexlify(vs);
|
|
|
|
if (result._vs) {
|
|
|
|
if (!isHexString(result._vs)) {
|
|
|
|
logger.throwArgumentError("signature invalid _vs", "signature", signature);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-01-11 04:18:28 -05:00
|
|
|
result._vs = hexZeroPad(result._vs, 32);
|
|
|
|
}
|
|
|
|
// Set or check the _vs
|
|
|
|
if (result._vs == null) {
|
|
|
|
result._vs = _vs;
|
|
|
|
}
|
|
|
|
else if (result._vs !== _vs) {
|
|
|
|
logger.throwArgumentError("signature _vs mismatch v and s", "signature", signature);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function joinSignature(signature) {
|
2019-05-14 18:48:48 -04:00
|
|
|
signature = splitSignature(signature);
|
|
|
|
return hexlify(concat([
|
|
|
|
signature.r,
|
|
|
|
signature.s,
|
|
|
|
(signature.recoveryParam ? "0x1c" : "0x1b")
|
|
|
|
]));
|
|
|
|
}
|