@metamask/eth-sig-util
Advanced tools
Comparing version 5.0.3 to 5.1.0
@@ -1,2 +0,2 @@ | ||
export interface EthEncryptedData { | ||
export declare type EthEncryptedData = { | ||
version: string; | ||
@@ -6,3 +6,3 @@ nonce: string; | ||
ciphertext: string; | ||
} | ||
}; | ||
/** | ||
@@ -9,0 +9,0 @@ * Encrypt a message. |
@@ -107,3 +107,3 @@ "use strict"; | ||
const NACL_EXTRA_BYTES = 16; | ||
if (typeof data === 'object' && 'toJSON' in data) { | ||
if (typeof data === 'object' && data && 'toJSON' in data) { | ||
// remove toJSON attack vector | ||
@@ -149,4 +149,4 @@ // TODO, check all possible children | ||
// string to buffer to UInt8Array | ||
const recieverPrivateKeyUint8Array = nacl_decodeHex(privateKey); | ||
const recieverEncryptionPrivateKey = nacl.box.keyPair.fromSecretKey(recieverPrivateKeyUint8Array).secretKey; | ||
const receiverPrivateKeyUint8Array = naclDecodeHex(privateKey); | ||
const receiverEncryptionPrivateKey = nacl.box.keyPair.fromSecretKey(receiverPrivateKeyUint8Array).secretKey; | ||
// assemble decryption parameters | ||
@@ -157,15 +157,21 @@ const nonce = naclUtil.decodeBase64(encryptedData.nonce); | ||
// decrypt | ||
const decryptedMessage = nacl.box.open(ciphertext, nonce, ephemPublicKey, recieverEncryptionPrivateKey); | ||
const decryptedMessage = nacl.box.open(ciphertext, nonce, ephemPublicKey, receiverEncryptionPrivateKey); | ||
// return decrypted msg data | ||
let output; | ||
try { | ||
output = naclUtil.encodeUTF8(decryptedMessage); | ||
if (!decryptedMessage) { | ||
throw new Error(); | ||
} | ||
const output = naclUtil.encodeUTF8(decryptedMessage); | ||
// TODO: This is probably extraneous but was kept to minimize changes during refactor | ||
if (!output) { | ||
throw new Error(); | ||
} | ||
return output; | ||
} | ||
catch (err) { | ||
throw new Error('Decryption failed.'); | ||
if (err && typeof err.message === 'string' && err.message.length) { | ||
throw new Error(`Decryption failed: ${err.message}`); | ||
} | ||
throw new Error(`Decryption failed.`); | ||
} | ||
if (output) { | ||
return output; | ||
} | ||
throw new Error('Decryption failed.'); | ||
} | ||
@@ -203,3 +209,3 @@ default: | ||
function getEncryptionPublicKey(privateKey) { | ||
const privateKeyUint8Array = nacl_decodeHex(privateKey); | ||
const privateKeyUint8Array = naclDecodeHex(privateKey); | ||
const encryptionPublicKey = nacl.box.keyPair.fromSecretKey(privateKeyUint8Array).publicKey; | ||
@@ -215,3 +221,3 @@ return naclUtil.encodeBase64(encryptionPublicKey); | ||
*/ | ||
function nacl_decodeHex(msgHex) { | ||
function naclDecodeHex(msgHex) { | ||
const msgBase64 = Buffer.from(msgHex, 'hex').toString('base64'); | ||
@@ -218,0 +224,0 @@ return naclUtil.decodeBase64(msgBase64); |
/// <reference types="node" /> | ||
import BN from 'bn.js'; | ||
/** | ||
@@ -16,3 +17,3 @@ * Packs non-standard encoded values packed according to their respective type in types in a buffer. | ||
*/ | ||
export declare function parseNumber(arg: any): any; | ||
export declare function parseNumber(arg: string | number | BN): BN; | ||
/** | ||
@@ -22,2 +23,2 @@ * @param types | ||
*/ | ||
export declare function rawEncode(types: any, values: any): Buffer; | ||
export declare function rawEncode(types: string[], values: (BN | Buffer | string | number | string[] | number[])[]): Buffer; |
@@ -12,4 +12,4 @@ "use strict"; | ||
const util_1 = require("@ethereumjs/util"); | ||
const bn_js_1 = __importDefault(require("bn.js")); | ||
const ethjs_util_1 = require("ethjs-util"); | ||
const bn_js_1 = __importDefault(require("bn.js")); | ||
const utils_1 = require("./utils"); | ||
@@ -47,3 +47,3 @@ // | ||
function isArray(type) { | ||
return type.lastIndexOf(']') === type.length - 1; | ||
return type.endsWith(']'); | ||
} | ||
@@ -70,3 +70,7 @@ /** | ||
function parseTypeN(type) { | ||
return parseInt(/^\D+(\d+)$/u.exec(type)[1], 10); | ||
const match = /^\D+(\d+)$/u.exec(type); | ||
if (match === null) { | ||
throw new Error(`Invalid parseTypeN input "${type}".`); | ||
} | ||
return parseInt(match[1], 10); | ||
} | ||
@@ -90,4 +94,4 @@ /** | ||
} | ||
else if (arg.toArray) { | ||
// assume this is a BN for the moment, replace with BN.isBN soon | ||
else if ((arg && Object.prototype.hasOwnProperty.call(arg, 'toArray')) || | ||
bn_js_1.default.isBN(arg)) { | ||
return arg; | ||
@@ -108,3 +112,2 @@ } | ||
// pass in bitsize = null if use default bitsize | ||
let size, num; | ||
if (isArray(type)) { | ||
@@ -116,2 +119,3 @@ const subType = type.replace(/\[.*?\]/u, ''); | ||
arraySize !== 0 && | ||
arraySize !== null && | ||
value.length > arraySize) { | ||
@@ -121,5 +125,3 @@ throw new Error(`Elements exceed array size: ${arraySize}`); | ||
} | ||
const arrayValues = value.map(function (v) { | ||
return solidityHexValue(subType, v, 256); | ||
}); | ||
const arrayValues = value.map((v) => solidityHexValue(subType, v, 256)); | ||
return Buffer.concat(arrayValues); | ||
@@ -134,2 +136,3 @@ } | ||
else if (type === 'bool') { | ||
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing | ||
bitsize = bitsize || 8; | ||
@@ -147,3 +150,3 @@ const padding = Array(bitsize / 4).join('0'); | ||
else if (type.startsWith('bytes')) { | ||
size = parseTypeN(type); | ||
const size = parseTypeN(type); | ||
if (size < 1 || size > 32) { | ||
@@ -158,10 +161,11 @@ throw new Error(`Invalid bytes<N> width: ${size}`); | ||
else if (type.startsWith('uint')) { | ||
size = parseTypeN(type); | ||
const size = parseTypeN(type); | ||
if (size % 8 || size < 8 || size > 256) { | ||
throw new Error(`Invalid uint<N> width: ${size}`); | ||
} | ||
num = parseNumber(value); | ||
const num = parseNumber(value); | ||
if (num.bitLength() > size) { | ||
throw new Error(`Supplied uint exceeds width: ${size} vs ${num.bitLength()}`); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing | ||
bitsize = bitsize || size; | ||
@@ -171,10 +175,11 @@ return num.toArrayLike(Buffer, 'be', bitsize / 8); | ||
else if (type.startsWith('int')) { | ||
size = parseTypeN(type); | ||
const size = parseTypeN(type); | ||
if (size % 8 || size < 8 || size > 256) { | ||
throw new Error(`Invalid int<N> width: ${size}`); | ||
} | ||
num = parseNumber(value); | ||
const num = parseNumber(value); | ||
if (num.bitLength() > size) { | ||
throw new Error(`Supplied int exceeds width: ${size} vs ${num.bitLength()}`); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing | ||
bitsize = bitsize || size; | ||
@@ -184,3 +189,3 @@ return num.toTwos(size).toArrayLike(Buffer, 'be', bitsize / 8); | ||
// FIXME: support all other types | ||
throw new Error(`Unsupported or invalid type: ${type}`); | ||
throw new Error(`Unsupported or invalid type: ${JSON.stringify(type)}`); | ||
} | ||
@@ -228,7 +233,7 @@ /** | ||
let headLength = 0; | ||
types.forEach(function (type) { | ||
types.forEach((type) => { | ||
if (isArray(type)) { | ||
const size = parseTypeArray(type); | ||
// eslint-disable-next-line no-negated-condition | ||
if (size !== 'dynamic') { | ||
if (size !== 'dynamic' && size !== null) { | ||
headLength += 32 * size; | ||
@@ -268,3 +273,2 @@ } | ||
function encodeSingle(type, arg) { | ||
let size, num, ret, i; | ||
if (type === 'address') { | ||
@@ -285,7 +289,10 @@ return encodeSingle('uint160', parseNumber(arg)); | ||
} | ||
size = parseTypeArray(type); | ||
if (size !== 'dynamic' && size !== 0 && arg.length > size) { | ||
const size = parseTypeArray(type); | ||
if (size !== 'dynamic' && | ||
size !== 0 && | ||
size !== null && | ||
arg.length > size) { | ||
throw new Error(`Elements exceed array size: ${size}`); | ||
} | ||
ret = []; | ||
const ret = []; | ||
type = type.slice(0, type.lastIndexOf('[')); | ||
@@ -295,3 +302,4 @@ if (typeof arg === 'string') { | ||
} | ||
for (i in arg) { | ||
// TODO: if this is array, should do for-of | ||
for (const i in arg) { | ||
if (Object.prototype.hasOwnProperty.call(arg, i)) { | ||
@@ -309,3 +317,3 @@ ret.push(encodeSingle(type, arg[i])); | ||
arg = Buffer.from(arg); | ||
ret = Buffer.concat([encodeSingle('uint256', arg.length), arg]); | ||
let ret = Buffer.concat([encodeSingle('uint256', arg.length), arg]); | ||
if (arg.length % 32 !== 0) { | ||
@@ -317,21 +325,20 @@ ret = Buffer.concat([ret, (0, util_1.zeros)(32 - (arg.length % 32))]); | ||
else if (type.startsWith('bytes')) { | ||
size = parseTypeN(type); | ||
const size = parseTypeN(type); | ||
if (size < 1 || size > 32) { | ||
throw new Error(`Invalid bytes<N> width: ${size}`); | ||
} | ||
if (typeof arg === 'number') { | ||
arg = (0, utils_1.normalize)(arg); | ||
} | ||
return (0, util_1.setLengthRight)((0, util_1.toBuffer)(arg), 32); | ||
// TODO: fix types here | ||
const nArg = typeof arg === 'number' ? (0, utils_1.normalize)(arg) : arg; | ||
return (0, util_1.setLengthRight)((0, util_1.toBuffer)(nArg), 32); | ||
} | ||
else if (type.startsWith('uint')) { | ||
size = parseTypeN(type); | ||
const size = parseTypeN(type); | ||
if (size % 8 || size < 8 || size > 256) { | ||
throw new Error(`Invalid uint<N> width: ${size}`); | ||
} | ||
num = parseNumber(arg); | ||
const num = parseNumber(arg); | ||
if (num.bitLength() > size) { | ||
throw new Error(`Supplied uint exceeds width: ${size} vs ${num.bitLength()}`); | ||
} | ||
if (num < 0) { | ||
if (num.isNeg()) { | ||
throw new Error('Supplied uint is negative'); | ||
@@ -342,7 +349,7 @@ } | ||
else if (type.startsWith('int')) { | ||
size = parseTypeN(type); | ||
const size = parseTypeN(type); | ||
if (size % 8 || size < 8 || size > 256) { | ||
throw new Error(`Invalid int<N> width: ${size}`); | ||
} | ||
num = parseNumber(arg); | ||
const num = parseNumber(arg); | ||
if (num.bitLength() > size) { | ||
@@ -354,5 +361,5 @@ throw new Error(`Supplied int exceeds width: ${size} vs ${num.bitLength()}`); | ||
else if (type.startsWith('ufixed')) { | ||
size = parseTypeNxM(type); | ||
num = parseNumber(arg); | ||
if (num < 0) { | ||
const size = parseTypeNxM(type); | ||
const num = parseNumber(arg); | ||
if (num.isNeg()) { | ||
throw new Error('Supplied ufixed is negative'); | ||
@@ -363,6 +370,6 @@ } | ||
else if (type.startsWith('fixed')) { | ||
size = parseTypeNxM(type); | ||
const size = parseTypeNxM(type); | ||
return encodeSingle('int256', parseNumber(arg).mul(new bn_js_1.default(2).pow(new bn_js_1.default(size[1])))); | ||
} | ||
throw new Error(`Unsupported or invalid type: ${type}`); | ||
throw new Error(`Unsupported or invalid type: ${JSON.stringify(type)}`); | ||
} | ||
@@ -382,5 +389,8 @@ // Is a type dynamic? | ||
function parseTypeNxM(type) { | ||
const tmp = /^\D+(\d+)x(\d+)$/u.exec(type); | ||
return [parseInt(tmp[1], 10), parseInt(tmp[2], 10)]; | ||
const match = /^\D+(\d+)x(\d+)$/u.exec(type); | ||
if (match === null || match.length < 1) { | ||
throw new Error(`Invalid parseTypeNxM input "${type}".`); | ||
} | ||
return [parseInt(match[1], 10), parseInt(match[2], 10)]; | ||
} | ||
//# sourceMappingURL=ethereumjs-abi-utils.js.map |
@@ -13,7 +13,7 @@ /// <reference types="node" /> | ||
*/ | ||
export interface TypedDataV1Field { | ||
export declare type TypedDataV1Field = { | ||
name: string; | ||
type: string; | ||
value: any; | ||
} | ||
}; | ||
/** | ||
@@ -35,10 +35,10 @@ * Represents the version of `signTypedData` being used. | ||
} | ||
export interface MessageTypeProperty { | ||
export declare type MessageTypeProperty = { | ||
name: string; | ||
type: string; | ||
} | ||
export interface MessageTypes { | ||
}; | ||
export declare type MessageTypes = { | ||
EIP712Domain: MessageTypeProperty[]; | ||
[additionalProperties: string]: MessageTypeProperty[]; | ||
} | ||
}; | ||
/** | ||
@@ -61,3 +61,3 @@ * This is the message format used for `signTypeData`, for all versions | ||
*/ | ||
export interface TypedMessage<T extends MessageTypes> { | ||
export declare type TypedMessage<T extends MessageTypes> = { | ||
types: T; | ||
@@ -73,3 +73,3 @@ primaryType: keyof T; | ||
message: Record<string, unknown>; | ||
} | ||
}; | ||
export declare const TYPED_MESSAGE_SCHEMA: { | ||
@@ -76,0 +76,0 @@ type: string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.recoverTypedSignature = exports.signTypedData = exports.typedSignatureHash = exports.TypedDataUtils = exports.TYPED_MESSAGE_SCHEMA = exports.SignTypedDataVersion = void 0; | ||
const ethjs_util_1 = require("ethjs-util"); | ||
const util_1 = require("@ethereumjs/util"); | ||
const keccak_1 = require("ethereum-cryptography/keccak"); | ||
const ethjs_util_1 = require("ethjs-util"); | ||
const ethereumjs_abi_utils_1 = require("./ethereumjs-abi-utils"); | ||
@@ -74,3 +74,5 @@ const utils_1 = require("./utils"); | ||
*/ | ||
function encodeField(types, name, type, value, version) { | ||
function encodeField(types, name, type, | ||
// TODO: constrain type on `value` | ||
value, version) { | ||
validateVersion(version, [SignTypedDataVersion.V3, SignTypedDataVersion.V4]); | ||
@@ -80,2 +82,3 @@ if (types[type] !== undefined) { | ||
'bytes32', | ||
// TODO: return Buffer, remove string from return type | ||
version === SignTypedDataVersion.V4 && value == null // eslint-disable-line no-eq-null | ||
@@ -111,3 +114,3 @@ ? '0x0000000000000000000000000000000000000000000000000000000000000000' | ||
} | ||
if (type.lastIndexOf(']') === type.length - 1) { | ||
if (type.endsWith(']')) { | ||
if (version === SignTypedDataVersion.V3) { | ||
@@ -180,3 +183,7 @@ throw new Error('Arrays are unimplemented in encodeData; use V4 extension'); | ||
function findTypeDependencies(primaryType, types, results = new Set()) { | ||
[primaryType] = primaryType.match(/^\w*/u); | ||
if (typeof primaryType !== 'string') { | ||
throw new Error(`Invalid findTypeDependencies input ${JSON.stringify(primaryType)}`); | ||
} | ||
const match = primaryType.match(/^\w*/u); | ||
[primaryType] = match; | ||
if (results.has(primaryType) || types[primaryType] === undefined) { | ||
@@ -232,2 +239,3 @@ return results; | ||
if ('types' in sanitizedData) { | ||
// TODO: Fix types | ||
sanitizedData.types = Object.assign({ EIP712Domain: [] }, sanitizedData.types); | ||
@@ -234,0 +242,0 @@ } |
@@ -55,3 +55,3 @@ /// <reference types="node" /> | ||
*/ | ||
export declare function normalize(input: number | string): string; | ||
export declare function normalize(input: number | string): string | undefined; | ||
/** | ||
@@ -58,0 +58,0 @@ * Node's Buffer.from() method does not seem to buffer numbers correctly out of the box. |
{ | ||
"name": "@metamask/eth-sig-util", | ||
"version": "5.0.3", | ||
"version": "5.1.0", | ||
"description": "A few useful functions for signing ethereum data", | ||
@@ -27,3 +27,8 @@ "keywords": [ | ||
"files": [ | ||
"dist" | ||
"dist", | ||
"!__snapshots__", | ||
"!*.test.js", | ||
"!*.test.js.map", | ||
"!*.test.ts", | ||
"!*.test.d.ts" | ||
], | ||
@@ -44,3 +49,3 @@ "scripts": { | ||
"@ethereumjs/util": "^8.0.6", | ||
"bn.js": "^4.11.8", | ||
"bn.js": "^4.12.0", | ||
"ethereum-cryptography": "^2.0.0", | ||
@@ -52,20 +57,21 @@ "ethjs-util": "^0.1.6", | ||
"devDependencies": { | ||
"@lavamoat/allow-scripts": "^2.0.3", | ||
"@lavamoat/allow-scripts": "^2.3.1", | ||
"@metamask/auto-changelog": "^3.1.0", | ||
"@metamask/eslint-config": "^9.0.0", | ||
"@metamask/eslint-config-jest": "^9.0.0", | ||
"@metamask/eslint-config-nodejs": "^9.0.0", | ||
"@metamask/eslint-config-typescript": "^9.0.1", | ||
"@types/jest": "^26.0.24", | ||
"@metamask/eslint-config": "^11.1.0", | ||
"@metamask/eslint-config-jest": "^11.1.0", | ||
"@metamask/eslint-config-nodejs": "^11.1.0", | ||
"@metamask/eslint-config-typescript": "^11.1.0", | ||
"@types/bn.js": "^4.11.6", | ||
"@types/jest": "^27.0.6", | ||
"@types/node": "^14.14.25", | ||
"@typescript-eslint/eslint-plugin": "^4.28.2", | ||
"@typescript-eslint/parser": "^4.28.2", | ||
"@typescript-eslint/eslint-plugin": "^5.59.1", | ||
"@typescript-eslint/parser": "^5.59.1", | ||
"ajv": "^8.11.0", | ||
"eslint": "^7.30.0", | ||
"eslint": "^8.27.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-import": "^2.23.4", | ||
"eslint-plugin-jest": "^24.3.6", | ||
"eslint-plugin-jsdoc": "^39.2.0", | ||
"eslint-plugin-jest": "^27.1.5", | ||
"eslint-plugin-jsdoc": "^39.6.2", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-prettier": "^3.4.0", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"jest": "^27.0.6", | ||
@@ -76,4 +82,4 @@ "prettier": "^2.3.2", | ||
"ts-jest": "^27.0.3", | ||
"typedoc": "^0.22.15", | ||
"typescript": "^4.1.3" | ||
"typedoc": "^0.24.6", | ||
"typescript": "~4.8.4" | ||
}, | ||
@@ -80,0 +86,0 @@ "packageManager": "yarn@3.2.2", |
@@ -5,4 +5,2 @@ # `@metamask/eth-sig-util` | ||
You can find usage examples [here](https://github.com/metamask/test-dapp) | ||
[Available on NPM](https://www.npmjs.com/package/@metamask/eth-sig-util) | ||
@@ -9,0 +7,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
150049
1661
26
75
Updatedbn.js@^4.12.0