Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace buffer with uint8array #1403

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions packages/wallet-sdk/src/vendor-js/eth-eip712-util/abi.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function parseNumber (arg) {
}

// Encodes a single item (can be dynamic array)
// @returns: Buffer
// @returns: Uint8Array
function encodeSingle (type, arg) {
var size, num, ret, i

Expand All @@ -69,7 +69,7 @@ function encodeSingle (type, arg) {
} else if (type === 'bool') {
return encodeSingle('uint8', arg ? 1 : 0)
} else if (type === 'string') {
return encodeSingle('bytes', new Buffer(arg, 'utf8'))
return encodeSingle('bytes', new TextEncoder().encode(arg))
} else if (isArray(type)) {
// this part handles fixed-length ([2]) and variable length ([]) arrays
// NOTE: we catch here all calls to arrays, that simplifies the rest
Expand All @@ -92,14 +92,14 @@ function encodeSingle (type, arg) {
var length = encodeSingle('uint256', arg.length)
ret.unshift(length)
}
return Buffer.concat(ret)
return util.concatUint8Arrays(ret)
} else if (type === 'bytes') {
arg = new Buffer(arg)
arg = new Uint8Array(arg)

ret = Buffer.concat([ encodeSingle('uint256', arg.length), arg ])
ret = util.concatUint8Arrays([encodeSingle('uint256', arg.length), arg])

if ((arg.length % 32) !== 0) {
ret = Buffer.concat([ ret, util.zeros(32 - (arg.length % 32)) ])
ret = util.concatUint8Arrays([ret, util.zeros(32 - (arg.length % 32))])
}

return ret
Expand Down Expand Up @@ -196,7 +196,7 @@ function rawEncode (types, values) {
}
}

return Buffer.concat(output.concat(data))
return util.concatUint8Arrays(output.concat(data))
}

function solidityPack (types, values) {
Expand All @@ -214,9 +214,9 @@ function solidityPack (types, values) {
if (type === 'bytes') {
ret.push(value)
} else if (type === 'string') {
ret.push(new Buffer(value, 'utf8'))
ret.push(new TextEncoder().encode(value))
} else if (type === 'bool') {
ret.push(new Buffer(value ? '01' : '00', 'hex'))
ret.push(new Uint8Array([value ? 1 : 0]))
} else if (type === 'address') {
ret.push(util.setLength(value, 20))
} else if (type.startsWith('bytes')) {
Expand Down Expand Up @@ -259,7 +259,7 @@ function solidityPack (types, values) {
}
}

return Buffer.concat(ret)
return util.concatUint8Arrays(ret)
}

function soliditySHA3 (types, values) {
Expand Down
8 changes: 4 additions & 4 deletions packages/wallet-sdk/src/vendor-js/eth-eip712-util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const TypedDataUtils = {
if (type === 'string') {
// convert string to buffer - prevents ethUtil from interpreting strings like '0xabcd' as hex
if (typeof value === 'string') {
value = Buffer.from(value, 'utf8')
value = new TextEncoder().encode(value);
}
return ['bytes32', util.keccak(value)]
}
Expand Down Expand Up @@ -94,7 +94,7 @@ const TypedDataUtils = {
encodedTypes.push('bytes32')
// convert string to buffer - prevents ethUtil from interpreting strings like '0xabcd' as hex
if (typeof value === 'string') {
value = Buffer.from(value, 'utf8')
value = new TextEncoder().encode(value);
}
value = util.keccak(value)
encodedValues.push(value)
Expand Down Expand Up @@ -204,12 +204,12 @@ const TypedDataUtils = {
*/
hash (typedData, useV4 = true) {
const sanitizedData = this.sanitizeData(typedData)
const parts = [Buffer.from('1901', 'hex')]
const parts = [new Uint8Array([0x19, 0x01])]
parts.push(this.hashStruct('EIP712Domain', sanitizedData.domain, sanitizedData.types, useV4))
if (sanitizedData.primaryType !== 'EIP712Domain') {
parts.push(this.hashStruct(sanitizedData.primaryType, sanitizedData.message, sanitizedData.types, useV4))
}
return util.keccak(Buffer.concat(parts))
return util.keccak(util.concatUint8Arrays(parts));
},
}

Expand Down
68 changes: 39 additions & 29 deletions packages/wallet-sdk/src/vendor-js/eth-eip712-util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
const { keccak_256 } = require('@noble/hashes/sha3')

/**
* Returns a buffer filled with 0s
* Returns a Uint8Array filled with 0s
* @method zeros
* @param {Number} bytes the number of bytes the buffer should be
* @return {Buffer}
* @return {Uint8Array}
*/
function zeros (bytes) {
return Buffer.allocUnsafe(bytes).fill(0)
return new Uint8Array(bytes).fill(0)
}

function bitLengthFromBigInt (num) {
Expand All @@ -30,7 +30,7 @@ function bufferBEFromBigInt(num, length) {
byteArray.unshift(0); // Prepend with zeroes if shorter than required length
}

return Buffer.from(byteArray);
return new Uint8Array(byteArray);
}

function twosFromBigInt(value, width) {
Expand All @@ -51,67 +51,65 @@ function twosFromBigInt(value, width) {
}

/**
* Left Pads an `Array` or `Buffer` with leading zeros till it has `length` bytes.
* Left Pads an `Array` or `Uint8Array` with leading zeros till it has `length` bytes.
* Or it truncates the beginning if it exceeds.
* @method setLength
* @param {Buffer|Array} msg the value to pad
* @param {Uint8Array|Array} msg the value to pad
* @param {Number} length the number of bytes the output should be
* @param {Boolean} [right=false] whether to start padding form the left or right
* @return {Buffer|Array}
* @return {Uint8Array|Array}
*/
function setLength (msg, length, right) {
const buf = zeros(length)
msg = toBuffer(msg)
if (right) {
if (msg.length < length) {
msg.copy(buf)
buf.set(msg)
return buf
}
return msg.slice(0, length)
} else {
if (msg.length < length) {
msg.copy(buf, length - msg.length)
buf.set(msg, length - msg.length)
return buf
}
return msg.slice(-length)
}
}

/**
* Right Pads an `Array` or `Buffer` with leading zeros till it has `length` bytes.
* Right Pads an `Array` or `Uint8Array` with leading zeros till it has `length` bytes.
* Or it truncates the beginning if it exceeds.
* @param {Buffer|Array} msg the value to pad
* @param {Uint8Array|Array} msg the value to pad
* @param {Number} length the number of bytes the output should be
* @return {Buffer|Array}
* @return {Uint8Array|Array}
*/
function setLengthRight (msg, length) {
return setLength(msg, length, true)
}

/**
* Attempts to turn a value into a `Buffer`. As input it supports `Buffer`, `String`, `Number`, null/undefined, `BIgInt` and other objects with a `toArray()` method.
* Attempts to turn a value into a `Uint8Array`. As input it supports `Uint8Array`, `String`, `Number`, null/undefined, `BigInt` and other objects with a `toArray()` method.
* @param {*} v the value
*/
function toBuffer (v) {
if (!Buffer.isBuffer(v)) {
if (!(v instanceof Uint8Array)) {
if (Array.isArray(v)) {
v = Buffer.from(v)
v = new Uint8Array(v)
} else if (typeof v === 'string') {
if (isHexString(v)) {
v = Buffer.from(padToEven(stripHexPrefix(v)), 'hex')
v = new Uint8Array(padToEven(stripHexPrefix(v)).match(/.{1,2}/g).map(byte => parseInt(byte, 16)))
} else {
v = Buffer.from(v)
v = new TextEncoder().encode(v)
}
} else if (typeof v === 'number') {
v = intToBuffer(v)
v = new Uint8Array([v])
} else if (v === null || v === undefined) {
v = Buffer.allocUnsafe(0)
v = new Uint8Array(0)
} else if (typeof v === 'bigint') {
v = bufferBEFromBigInt(v)
} else if (v.toArray) {
// TODO: bigint should be handled above, may remove this duplicate
// converts a BigInt to a Buffer
v = Buffer.from(v.toArray())
v = new Uint8Array(v.toArray())
} else {
throw new Error('invalid type')
}
Expand All @@ -120,26 +118,26 @@ function toBuffer (v) {
}

/**
* Converts a `Buffer` into a hex `String`
* @param {Buffer} buf
* Converts a `Uint8Array` into a hex `String`
* @param {Uint8Array} buf
* @return {String}
*/
function bufferToHex (buf) {
buf = toBuffer(buf)
return '0x' + buf.toString('hex')
return '0x' + Array.from(buf).map(byte => byte.toString(16).padStart(2, '0')).join('')
}

/**
* Creates Keccak hash of the input
* @param {Buffer|Array|String|Number} a the input data
* @param {Uint8Array|Array|String|Number} a the input data
* @param {Number} [bits=256] the Keccak width
* @return {Buffer}
* @return {Uint8Array}
*/
function keccak (a, bits) {
a = toBuffer(a)
if (!bits) bits = 256

return Buffer.from(keccak_256('keccak' + bits))
return new Uint8Array(keccak_256(a))
}

function padToEven (str) {
Expand All @@ -157,6 +155,17 @@ function stripHexPrefix (str) {
return str
}

function concatUint8Arrays(arrays) {
let totalLength = arrays.reduce((acc, value) => acc + value.length, 0);
let result = new Uint8Array(totalLength);
let offset = 0;
for (let array of arrays) {
result.set(array, offset);
offset += array.length;
}
return result;
}

module.exports = {
zeros,
setLength,
Expand All @@ -168,5 +177,6 @@ module.exports = {
keccak,
bitLengthFromBigInt,
bufferBEFromBigInt,
twosFromBigInt
twosFromBigInt,
concatUint8Arrays
}
Loading