@0xsequence/utils
Advanced tools
Comparing version 0.0.0-20240812142652 to 0.0.0-20240814043648
@@ -27,71 +27,12 @@ 'use strict'; | ||
// Monkey patch toJSON on BigInt to return a string | ||
BigInt.prototype.toJSON = function () { | ||
return this.toString(); | ||
}; | ||
const MAX_UINT_256 = BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'); | ||
// ethers implement this method but doesn't exports it | ||
const isBigNumberish = value => { | ||
return value != null && (typeof value === 'number' && value % 1 === 0 || typeof value === 'string' && !!value.match(/^-?[0-9]+$/) || ethers.ethers.isHexString(value) || typeof value === 'bigint'); | ||
}; | ||
function isBigNumberish(value) { | ||
return value != null && (ethers.BigNumber.isBigNumber(value) || typeof value === 'number' && value % 1 === 0 || typeof value === 'string' && !!value.match(/^-?[0-9]+$/) || ethers.utils.isHexString(value) || typeof value === 'bigint' || ethers.utils.isBytes(value)); | ||
} | ||
// Even length zero-padded hex string with 0x prefix | ||
const toHexString = value => { | ||
const result = value.toString(16); | ||
return `${result.length % 2 === 0 ? '0x' : '0x0'}${result}`; | ||
}; | ||
const parseUnits = (value, decimals = 18) => { | ||
let [integer, fraction = '0'] = value.split('.'); | ||
const negative = integer.startsWith('-'); | ||
if (negative) { | ||
integer = integer.slice(1); | ||
} | ||
// trim trailing zeros. | ||
fraction = fraction.replace(/(0+)$/, ''); | ||
// round off if the fraction is larger than the number of decimals. | ||
if (decimals === 0) { | ||
integer = `${Math.round(Number(`${integer}.${fraction}`))}`; | ||
fraction = ''; | ||
} else if (fraction.length > decimals) { | ||
const [left, unit, right] = [fraction.slice(0, decimals - 1), fraction.slice(decimals - 1, decimals), fraction.slice(decimals)]; | ||
const rounded = Math.round(Number(`${unit}.${right}`)); | ||
if (rounded > 9) { | ||
fraction = `${BigInt(left) + 1n}0`.padStart(left.length + 1, '0'); | ||
} else { | ||
fraction = `${left}${rounded}`; | ||
} | ||
if (fraction.length > decimals) { | ||
fraction = fraction.slice(1); | ||
integer = `${BigInt(integer) + 1n}`; | ||
} | ||
fraction = fraction.slice(0, decimals); | ||
} else { | ||
fraction = fraction.padEnd(decimals, '0'); | ||
} | ||
return BigInt(`${negative ? '-' : ''}${integer}${fraction}`); | ||
}; | ||
const parseEther = value => parseUnits(value, 18); | ||
const formatUnits = (value, decimals = 18) => { | ||
let display = value.toString(); | ||
const negative = display.startsWith('-'); | ||
if (negative) { | ||
display = display.slice(1); | ||
} | ||
display = display.padStart(decimals, '0'); | ||
const integer = display.slice(0, display.length - decimals); | ||
let fraction = display.slice(display.length - decimals); | ||
fraction = fraction.replace(/(0+)$/, ''); | ||
return `${negative ? '-' : ''}${integer || '0'}${fraction ? `.${fraction}` : ''}`; | ||
}; | ||
const formatEther = value => formatUnits(value, 18); | ||
const encodeMessageDigest = message => { | ||
if (typeof message === 'string') { | ||
return ethers.ethers.getBytes(ethers.ethers.id(message)); | ||
return ethers.ethers.utils.arrayify(ethers.ethers.utils.keccak256(ethers.ethers.utils.toUtf8Bytes(message))); | ||
} else { | ||
return ethers.ethers.getBytes(ethers.ethers.keccak256(message)); | ||
return ethers.ethers.utils.arrayify(ethers.ethers.utils.keccak256(message)); | ||
} | ||
@@ -102,6 +43,6 @@ }; | ||
const packMessageData = (walletAddress, chainId, digest) => { | ||
return ethers.ethers.solidityPacked(['string', 'uint256', 'address', 'bytes32'], ['\x19\x01', chainId, walletAddress, digest]); | ||
return ethers.ethers.utils.solidityPack(['string', 'uint256', 'address', 'bytes32'], ['\x19\x01', chainId, walletAddress, digest]); | ||
}; | ||
const subDigestOf = (address, chainId, digest) => { | ||
return ethers.ethers.keccak256(packMessageData(address, chainId, digest)); | ||
return ethers.ethers.utils.keccak256(packMessageData(address, chainId, digest)); | ||
}; | ||
@@ -296,3 +237,3 @@ | ||
if (typeof value === 'string') { | ||
return ethers.ethers.getBytes(value); | ||
return ethers.ethers.utils.arrayify(value); | ||
} | ||
@@ -305,3 +246,5 @@ return value; | ||
static bufferToHex(value, withPrefix = true) { | ||
const prefixed = ethers.ethers.hexlify(value); | ||
const prefixed = ethers.ethers.utils.hexlify(value, { | ||
allowMissingPrefix: true | ||
}); | ||
return withPrefix ? prefixed : prefixed.substring(2); | ||
@@ -326,3 +269,3 @@ } | ||
this.sortPairs = !!options.sortPairs; | ||
this.hashFn = Base.bufferifyFn(ethers.ethers.keccak256); | ||
this.hashFn = Base.bufferifyFn(ethers.ethers.utils.keccak256); | ||
this.processLeaves(leaves); | ||
@@ -366,3 +309,3 @@ } | ||
} | ||
const hash = this.hashFn(ethers.ethers.concat(combined)); | ||
const hash = this.hashFn(ethers.ethers.utils.concat(combined)); | ||
this.layers[layerIndex].push(hash); | ||
@@ -447,7 +390,7 @@ } | ||
} | ||
hash = this.hashFn(ethers.ethers.concat(buffers)); | ||
hash = this.hashFn(ethers.ethers.utils.concat(buffers)); | ||
} else { | ||
buffers.push(hash); | ||
buffers[isLeftNode ? 'unshift' : 'push'](data); | ||
hash = this.hashFn(ethers.ethers.concat(buffers)); | ||
hash = this.hashFn(ethers.ethers.utils.concat(buffers)); | ||
} | ||
@@ -489,39 +432,24 @@ } | ||
} | ||
const getSaleItemsLeaf = element => ethers.ethers.solidityPackedKeccak256(['address', 'uint256'], [element.address.toLowerCase(), element.tokenId]); | ||
const getSaleItemsLeaf = element => ethers.utils.solidityKeccak256(['address', 'uint256'], [element.address.toLowerCase(), element.tokenId]); | ||
// export const getEthersConnectionInfo = (url: string, projectAccessKey?: string, jwt?: string): ConnectionInfo => { | ||
// const headers: { | ||
// [key: string]: string | number | ||
// } = {} | ||
// if (jwt && jwt.length > 0) { | ||
// headers['Authorization'] = `BEARER ${jwt}` | ||
// } | ||
// if (projectAccessKey && projectAccessKey.length > 0) { | ||
// headers['X-Access-Key'] = projectAccessKey | ||
// } | ||
// return { | ||
// url, | ||
// headers, | ||
// skipFetchSetup: true, | ||
// fetchOptions: { | ||
// mode: 'cors', | ||
// cache: 'force-cache', | ||
// credentials: 'same-origin', | ||
// redirect: 'follow', | ||
// referrer: 'client' | ||
// } | ||
// } | ||
// } | ||
const getFetchRequest = (url, projectAccessKey, jwt) => { | ||
const req = new ethers.ethers.FetchRequest(url); | ||
if (jwt) { | ||
req.setHeader('Authorization', `BEARER ${jwt}`); | ||
const getEthersConnectionInfo = (url, projectAccessKey, jwt) => { | ||
const headers = {}; | ||
if (jwt && jwt.length > 0) { | ||
headers['Authorization'] = `BEARER ${jwt}`; | ||
} | ||
if (projectAccessKey) { | ||
req.setHeader('X-Access-Key', projectAccessKey); | ||
if (projectAccessKey && projectAccessKey.length > 0) { | ||
headers['X-Access-Key'] = projectAccessKey; | ||
} | ||
return req; | ||
return { | ||
url, | ||
headers, | ||
skipFetchSetup: true, | ||
fetchOptions: { | ||
mode: 'cors', | ||
cache: 'force-cache', | ||
credentials: 'same-origin', | ||
redirect: 'follow', | ||
referrer: 'client' | ||
} | ||
}; | ||
}; | ||
@@ -535,3 +463,3 @@ | ||
do(key, validMilliseconds, task, ...args) { | ||
key = `${key}:${ethers.ethers.id(JSON.stringify(args, deterministically))}`; | ||
key = `${key}:${ethers.ethers.utils.keccak256(ethers.ethers.utils.toUtf8Bytes(JSON.stringify(args, deterministically)))}`; | ||
let entry = this.cache.get(key); | ||
@@ -647,12 +575,16 @@ if (entry) { | ||
delete types['EIP712Domain']; | ||
return ethers.ethers.TypedDataEncoder.hash(typedData.domain, types, typedData.message); | ||
return ethers.ethers.utils._TypedDataEncoder.hash(typedData.domain, types, typedData.message); | ||
}; | ||
const encodeTypedDataDigest = typedData => { | ||
return ethers.ethers.getBytes(encodeTypedDataHash(typedData)); | ||
return ethers.ethers.utils.arrayify(encodeTypedDataHash(typedData)); | ||
}; | ||
const { | ||
defineProperties, | ||
resolveProperties | ||
} = ethers.ethers; | ||
defineReadOnly, | ||
getStatic, | ||
resolveProperties, | ||
checkProperties, | ||
shallowCopy, | ||
deepCopy | ||
} = ethers.utils; | ||
@@ -663,3 +595,2 @@ // urlClean removes double slashes from url path | ||
exports.Logger = Logger; | ||
exports.MAX_UINT_256 = MAX_UINT_256; | ||
exports.MerkleTreeGenerator = MerkleTreeGenerator; | ||
@@ -671,12 +602,13 @@ exports.PromiseCache = PromiseCache; | ||
exports.base64EncodeObject = base64EncodeObject; | ||
exports.checkProperties = checkProperties; | ||
exports.configureLogger = configureLogger; | ||
exports.defineProperties = defineProperties; | ||
exports.deepCopy = deepCopy; | ||
exports.defineReadOnly = defineReadOnly; | ||
exports.encodeMessageDigest = encodeMessageDigest; | ||
exports.encodeTypedDataDigest = encodeTypedDataDigest; | ||
exports.encodeTypedDataHash = encodeTypedDataHash; | ||
exports.formatEther = formatEther; | ||
exports.formatUnits = formatUnits; | ||
exports.getFetchRequest = getFetchRequest; | ||
exports.getEthersConnectionInfo = getEthersConnectionInfo; | ||
exports.getRandomInt = getRandomInt; | ||
exports.getSaleItemsLeaf = getSaleItemsLeaf; | ||
exports.getStatic = getStatic; | ||
exports.isBigNumberish = isBigNumberish; | ||
@@ -688,4 +620,2 @@ exports.isBrowser = isBrowser; | ||
exports.packMessageData = packMessageData; | ||
exports.parseEther = parseEther; | ||
exports.parseUnits = parseUnits; | ||
exports.promisify = promisify; | ||
@@ -698,5 +628,5 @@ exports.queryStringFromObject = queryStringFromObject; | ||
exports.sanitizeNumberString = sanitizeNumberString; | ||
exports.shallowCopy = shallowCopy; | ||
exports.sleep = sleep; | ||
exports.subDigestOf = subDigestOf; | ||
exports.toHexString = toHexString; | ||
exports.urlClean = urlClean; |
@@ -27,71 +27,12 @@ 'use strict'; | ||
// Monkey patch toJSON on BigInt to return a string | ||
BigInt.prototype.toJSON = function () { | ||
return this.toString(); | ||
}; | ||
const MAX_UINT_256 = BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'); | ||
// ethers implement this method but doesn't exports it | ||
const isBigNumberish = value => { | ||
return value != null && (typeof value === 'number' && value % 1 === 0 || typeof value === 'string' && !!value.match(/^-?[0-9]+$/) || ethers.ethers.isHexString(value) || typeof value === 'bigint'); | ||
}; | ||
function isBigNumberish(value) { | ||
return value != null && (ethers.BigNumber.isBigNumber(value) || typeof value === 'number' && value % 1 === 0 || typeof value === 'string' && !!value.match(/^-?[0-9]+$/) || ethers.utils.isHexString(value) || typeof value === 'bigint' || ethers.utils.isBytes(value)); | ||
} | ||
// Even length zero-padded hex string with 0x prefix | ||
const toHexString = value => { | ||
const result = value.toString(16); | ||
return `${result.length % 2 === 0 ? '0x' : '0x0'}${result}`; | ||
}; | ||
const parseUnits = (value, decimals = 18) => { | ||
let [integer, fraction = '0'] = value.split('.'); | ||
const negative = integer.startsWith('-'); | ||
if (negative) { | ||
integer = integer.slice(1); | ||
} | ||
// trim trailing zeros. | ||
fraction = fraction.replace(/(0+)$/, ''); | ||
// round off if the fraction is larger than the number of decimals. | ||
if (decimals === 0) { | ||
integer = `${Math.round(Number(`${integer}.${fraction}`))}`; | ||
fraction = ''; | ||
} else if (fraction.length > decimals) { | ||
const [left, unit, right] = [fraction.slice(0, decimals - 1), fraction.slice(decimals - 1, decimals), fraction.slice(decimals)]; | ||
const rounded = Math.round(Number(`${unit}.${right}`)); | ||
if (rounded > 9) { | ||
fraction = `${BigInt(left) + 1n}0`.padStart(left.length + 1, '0'); | ||
} else { | ||
fraction = `${left}${rounded}`; | ||
} | ||
if (fraction.length > decimals) { | ||
fraction = fraction.slice(1); | ||
integer = `${BigInt(integer) + 1n}`; | ||
} | ||
fraction = fraction.slice(0, decimals); | ||
} else { | ||
fraction = fraction.padEnd(decimals, '0'); | ||
} | ||
return BigInt(`${negative ? '-' : ''}${integer}${fraction}`); | ||
}; | ||
const parseEther = value => parseUnits(value, 18); | ||
const formatUnits = (value, decimals = 18) => { | ||
let display = value.toString(); | ||
const negative = display.startsWith('-'); | ||
if (negative) { | ||
display = display.slice(1); | ||
} | ||
display = display.padStart(decimals, '0'); | ||
const integer = display.slice(0, display.length - decimals); | ||
let fraction = display.slice(display.length - decimals); | ||
fraction = fraction.replace(/(0+)$/, ''); | ||
return `${negative ? '-' : ''}${integer || '0'}${fraction ? `.${fraction}` : ''}`; | ||
}; | ||
const formatEther = value => formatUnits(value, 18); | ||
const encodeMessageDigest = message => { | ||
if (typeof message === 'string') { | ||
return ethers.ethers.getBytes(ethers.ethers.id(message)); | ||
return ethers.ethers.utils.arrayify(ethers.ethers.utils.keccak256(ethers.ethers.utils.toUtf8Bytes(message))); | ||
} else { | ||
return ethers.ethers.getBytes(ethers.ethers.keccak256(message)); | ||
return ethers.ethers.utils.arrayify(ethers.ethers.utils.keccak256(message)); | ||
} | ||
@@ -102,6 +43,6 @@ }; | ||
const packMessageData = (walletAddress, chainId, digest) => { | ||
return ethers.ethers.solidityPacked(['string', 'uint256', 'address', 'bytes32'], ['\x19\x01', chainId, walletAddress, digest]); | ||
return ethers.ethers.utils.solidityPack(['string', 'uint256', 'address', 'bytes32'], ['\x19\x01', chainId, walletAddress, digest]); | ||
}; | ||
const subDigestOf = (address, chainId, digest) => { | ||
return ethers.ethers.keccak256(packMessageData(address, chainId, digest)); | ||
return ethers.ethers.utils.keccak256(packMessageData(address, chainId, digest)); | ||
}; | ||
@@ -296,3 +237,3 @@ | ||
if (typeof value === 'string') { | ||
return ethers.ethers.getBytes(value); | ||
return ethers.ethers.utils.arrayify(value); | ||
} | ||
@@ -305,3 +246,5 @@ return value; | ||
static bufferToHex(value, withPrefix = true) { | ||
const prefixed = ethers.ethers.hexlify(value); | ||
const prefixed = ethers.ethers.utils.hexlify(value, { | ||
allowMissingPrefix: true | ||
}); | ||
return withPrefix ? prefixed : prefixed.substring(2); | ||
@@ -326,3 +269,3 @@ } | ||
this.sortPairs = !!options.sortPairs; | ||
this.hashFn = Base.bufferifyFn(ethers.ethers.keccak256); | ||
this.hashFn = Base.bufferifyFn(ethers.ethers.utils.keccak256); | ||
this.processLeaves(leaves); | ||
@@ -366,3 +309,3 @@ } | ||
} | ||
const hash = this.hashFn(ethers.ethers.concat(combined)); | ||
const hash = this.hashFn(ethers.ethers.utils.concat(combined)); | ||
this.layers[layerIndex].push(hash); | ||
@@ -447,7 +390,7 @@ } | ||
} | ||
hash = this.hashFn(ethers.ethers.concat(buffers)); | ||
hash = this.hashFn(ethers.ethers.utils.concat(buffers)); | ||
} else { | ||
buffers.push(hash); | ||
buffers[isLeftNode ? 'unshift' : 'push'](data); | ||
hash = this.hashFn(ethers.ethers.concat(buffers)); | ||
hash = this.hashFn(ethers.ethers.utils.concat(buffers)); | ||
} | ||
@@ -489,39 +432,24 @@ } | ||
} | ||
const getSaleItemsLeaf = element => ethers.ethers.solidityPackedKeccak256(['address', 'uint256'], [element.address.toLowerCase(), element.tokenId]); | ||
const getSaleItemsLeaf = element => ethers.utils.solidityKeccak256(['address', 'uint256'], [element.address.toLowerCase(), element.tokenId]); | ||
// export const getEthersConnectionInfo = (url: string, projectAccessKey?: string, jwt?: string): ConnectionInfo => { | ||
// const headers: { | ||
// [key: string]: string | number | ||
// } = {} | ||
// if (jwt && jwt.length > 0) { | ||
// headers['Authorization'] = `BEARER ${jwt}` | ||
// } | ||
// if (projectAccessKey && projectAccessKey.length > 0) { | ||
// headers['X-Access-Key'] = projectAccessKey | ||
// } | ||
// return { | ||
// url, | ||
// headers, | ||
// skipFetchSetup: true, | ||
// fetchOptions: { | ||
// mode: 'cors', | ||
// cache: 'force-cache', | ||
// credentials: 'same-origin', | ||
// redirect: 'follow', | ||
// referrer: 'client' | ||
// } | ||
// } | ||
// } | ||
const getFetchRequest = (url, projectAccessKey, jwt) => { | ||
const req = new ethers.ethers.FetchRequest(url); | ||
if (jwt) { | ||
req.setHeader('Authorization', `BEARER ${jwt}`); | ||
const getEthersConnectionInfo = (url, projectAccessKey, jwt) => { | ||
const headers = {}; | ||
if (jwt && jwt.length > 0) { | ||
headers['Authorization'] = `BEARER ${jwt}`; | ||
} | ||
if (projectAccessKey) { | ||
req.setHeader('X-Access-Key', projectAccessKey); | ||
if (projectAccessKey && projectAccessKey.length > 0) { | ||
headers['X-Access-Key'] = projectAccessKey; | ||
} | ||
return req; | ||
return { | ||
url, | ||
headers, | ||
skipFetchSetup: true, | ||
fetchOptions: { | ||
mode: 'cors', | ||
cache: 'force-cache', | ||
credentials: 'same-origin', | ||
redirect: 'follow', | ||
referrer: 'client' | ||
} | ||
}; | ||
}; | ||
@@ -535,3 +463,3 @@ | ||
do(key, validMilliseconds, task, ...args) { | ||
key = `${key}:${ethers.ethers.id(JSON.stringify(args, deterministically))}`; | ||
key = `${key}:${ethers.ethers.utils.keccak256(ethers.ethers.utils.toUtf8Bytes(JSON.stringify(args, deterministically)))}`; | ||
let entry = this.cache.get(key); | ||
@@ -647,12 +575,16 @@ if (entry) { | ||
delete types['EIP712Domain']; | ||
return ethers.ethers.TypedDataEncoder.hash(typedData.domain, types, typedData.message); | ||
return ethers.ethers.utils._TypedDataEncoder.hash(typedData.domain, types, typedData.message); | ||
}; | ||
const encodeTypedDataDigest = typedData => { | ||
return ethers.ethers.getBytes(encodeTypedDataHash(typedData)); | ||
return ethers.ethers.utils.arrayify(encodeTypedDataHash(typedData)); | ||
}; | ||
const { | ||
defineProperties, | ||
resolveProperties | ||
} = ethers.ethers; | ||
defineReadOnly, | ||
getStatic, | ||
resolveProperties, | ||
checkProperties, | ||
shallowCopy, | ||
deepCopy | ||
} = ethers.utils; | ||
@@ -663,3 +595,2 @@ // urlClean removes double slashes from url path | ||
exports.Logger = Logger; | ||
exports.MAX_UINT_256 = MAX_UINT_256; | ||
exports.MerkleTreeGenerator = MerkleTreeGenerator; | ||
@@ -671,12 +602,13 @@ exports.PromiseCache = PromiseCache; | ||
exports.base64EncodeObject = base64EncodeObject; | ||
exports.checkProperties = checkProperties; | ||
exports.configureLogger = configureLogger; | ||
exports.defineProperties = defineProperties; | ||
exports.deepCopy = deepCopy; | ||
exports.defineReadOnly = defineReadOnly; | ||
exports.encodeMessageDigest = encodeMessageDigest; | ||
exports.encodeTypedDataDigest = encodeTypedDataDigest; | ||
exports.encodeTypedDataHash = encodeTypedDataHash; | ||
exports.formatEther = formatEther; | ||
exports.formatUnits = formatUnits; | ||
exports.getFetchRequest = getFetchRequest; | ||
exports.getEthersConnectionInfo = getEthersConnectionInfo; | ||
exports.getRandomInt = getRandomInt; | ||
exports.getSaleItemsLeaf = getSaleItemsLeaf; | ||
exports.getStatic = getStatic; | ||
exports.isBigNumberish = isBigNumberish; | ||
@@ -688,4 +620,2 @@ exports.isBrowser = isBrowser; | ||
exports.packMessageData = packMessageData; | ||
exports.parseEther = parseEther; | ||
exports.parseUnits = parseUnits; | ||
exports.promisify = promisify; | ||
@@ -698,5 +628,5 @@ exports.queryStringFromObject = queryStringFromObject; | ||
exports.sanitizeNumberString = sanitizeNumberString; | ||
exports.shallowCopy = shallowCopy; | ||
exports.sleep = sleep; | ||
exports.subDigestOf = subDigestOf; | ||
exports.toHexString = toHexString; | ||
exports.urlClean = urlClean; |
import { Base64 } from 'js-base64'; | ||
import { ethers } from 'ethers'; | ||
import { BigNumber, utils, ethers } from 'ethers'; | ||
@@ -23,71 +23,12 @@ const base64Encode = val => { | ||
// Monkey patch toJSON on BigInt to return a string | ||
BigInt.prototype.toJSON = function () { | ||
return this.toString(); | ||
}; | ||
const MAX_UINT_256 = BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'); | ||
// ethers implement this method but doesn't exports it | ||
const isBigNumberish = value => { | ||
return value != null && (typeof value === 'number' && value % 1 === 0 || typeof value === 'string' && !!value.match(/^-?[0-9]+$/) || ethers.isHexString(value) || typeof value === 'bigint'); | ||
}; | ||
function isBigNumberish(value) { | ||
return value != null && (BigNumber.isBigNumber(value) || typeof value === 'number' && value % 1 === 0 || typeof value === 'string' && !!value.match(/^-?[0-9]+$/) || utils.isHexString(value) || typeof value === 'bigint' || utils.isBytes(value)); | ||
} | ||
// Even length zero-padded hex string with 0x prefix | ||
const toHexString = value => { | ||
const result = value.toString(16); | ||
return `${result.length % 2 === 0 ? '0x' : '0x0'}${result}`; | ||
}; | ||
const parseUnits = (value, decimals = 18) => { | ||
let [integer, fraction = '0'] = value.split('.'); | ||
const negative = integer.startsWith('-'); | ||
if (negative) { | ||
integer = integer.slice(1); | ||
} | ||
// trim trailing zeros. | ||
fraction = fraction.replace(/(0+)$/, ''); | ||
// round off if the fraction is larger than the number of decimals. | ||
if (decimals === 0) { | ||
integer = `${Math.round(Number(`${integer}.${fraction}`))}`; | ||
fraction = ''; | ||
} else if (fraction.length > decimals) { | ||
const [left, unit, right] = [fraction.slice(0, decimals - 1), fraction.slice(decimals - 1, decimals), fraction.slice(decimals)]; | ||
const rounded = Math.round(Number(`${unit}.${right}`)); | ||
if (rounded > 9) { | ||
fraction = `${BigInt(left) + 1n}0`.padStart(left.length + 1, '0'); | ||
} else { | ||
fraction = `${left}${rounded}`; | ||
} | ||
if (fraction.length > decimals) { | ||
fraction = fraction.slice(1); | ||
integer = `${BigInt(integer) + 1n}`; | ||
} | ||
fraction = fraction.slice(0, decimals); | ||
} else { | ||
fraction = fraction.padEnd(decimals, '0'); | ||
} | ||
return BigInt(`${negative ? '-' : ''}${integer}${fraction}`); | ||
}; | ||
const parseEther = value => parseUnits(value, 18); | ||
const formatUnits = (value, decimals = 18) => { | ||
let display = value.toString(); | ||
const negative = display.startsWith('-'); | ||
if (negative) { | ||
display = display.slice(1); | ||
} | ||
display = display.padStart(decimals, '0'); | ||
const integer = display.slice(0, display.length - decimals); | ||
let fraction = display.slice(display.length - decimals); | ||
fraction = fraction.replace(/(0+)$/, ''); | ||
return `${negative ? '-' : ''}${integer || '0'}${fraction ? `.${fraction}` : ''}`; | ||
}; | ||
const formatEther = value => formatUnits(value, 18); | ||
const encodeMessageDigest = message => { | ||
if (typeof message === 'string') { | ||
return ethers.getBytes(ethers.id(message)); | ||
return ethers.utils.arrayify(ethers.utils.keccak256(ethers.utils.toUtf8Bytes(message))); | ||
} else { | ||
return ethers.getBytes(ethers.keccak256(message)); | ||
return ethers.utils.arrayify(ethers.utils.keccak256(message)); | ||
} | ||
@@ -98,6 +39,6 @@ }; | ||
const packMessageData = (walletAddress, chainId, digest) => { | ||
return ethers.solidityPacked(['string', 'uint256', 'address', 'bytes32'], ['\x19\x01', chainId, walletAddress, digest]); | ||
return ethers.utils.solidityPack(['string', 'uint256', 'address', 'bytes32'], ['\x19\x01', chainId, walletAddress, digest]); | ||
}; | ||
const subDigestOf = (address, chainId, digest) => { | ||
return ethers.keccak256(packMessageData(address, chainId, digest)); | ||
return ethers.utils.keccak256(packMessageData(address, chainId, digest)); | ||
}; | ||
@@ -292,3 +233,3 @@ | ||
if (typeof value === 'string') { | ||
return ethers.getBytes(value); | ||
return ethers.utils.arrayify(value); | ||
} | ||
@@ -301,3 +242,5 @@ return value; | ||
static bufferToHex(value, withPrefix = true) { | ||
const prefixed = ethers.hexlify(value); | ||
const prefixed = ethers.utils.hexlify(value, { | ||
allowMissingPrefix: true | ||
}); | ||
return withPrefix ? prefixed : prefixed.substring(2); | ||
@@ -322,3 +265,3 @@ } | ||
this.sortPairs = !!options.sortPairs; | ||
this.hashFn = Base.bufferifyFn(ethers.keccak256); | ||
this.hashFn = Base.bufferifyFn(ethers.utils.keccak256); | ||
this.processLeaves(leaves); | ||
@@ -362,3 +305,3 @@ } | ||
} | ||
const hash = this.hashFn(ethers.concat(combined)); | ||
const hash = this.hashFn(ethers.utils.concat(combined)); | ||
this.layers[layerIndex].push(hash); | ||
@@ -443,7 +386,7 @@ } | ||
} | ||
hash = this.hashFn(ethers.concat(buffers)); | ||
hash = this.hashFn(ethers.utils.concat(buffers)); | ||
} else { | ||
buffers.push(hash); | ||
buffers[isLeftNode ? 'unshift' : 'push'](data); | ||
hash = this.hashFn(ethers.concat(buffers)); | ||
hash = this.hashFn(ethers.utils.concat(buffers)); | ||
} | ||
@@ -485,39 +428,24 @@ } | ||
} | ||
const getSaleItemsLeaf = element => ethers.solidityPackedKeccak256(['address', 'uint256'], [element.address.toLowerCase(), element.tokenId]); | ||
const getSaleItemsLeaf = element => utils.solidityKeccak256(['address', 'uint256'], [element.address.toLowerCase(), element.tokenId]); | ||
// export const getEthersConnectionInfo = (url: string, projectAccessKey?: string, jwt?: string): ConnectionInfo => { | ||
// const headers: { | ||
// [key: string]: string | number | ||
// } = {} | ||
// if (jwt && jwt.length > 0) { | ||
// headers['Authorization'] = `BEARER ${jwt}` | ||
// } | ||
// if (projectAccessKey && projectAccessKey.length > 0) { | ||
// headers['X-Access-Key'] = projectAccessKey | ||
// } | ||
// return { | ||
// url, | ||
// headers, | ||
// skipFetchSetup: true, | ||
// fetchOptions: { | ||
// mode: 'cors', | ||
// cache: 'force-cache', | ||
// credentials: 'same-origin', | ||
// redirect: 'follow', | ||
// referrer: 'client' | ||
// } | ||
// } | ||
// } | ||
const getFetchRequest = (url, projectAccessKey, jwt) => { | ||
const req = new ethers.FetchRequest(url); | ||
if (jwt) { | ||
req.setHeader('Authorization', `BEARER ${jwt}`); | ||
const getEthersConnectionInfo = (url, projectAccessKey, jwt) => { | ||
const headers = {}; | ||
if (jwt && jwt.length > 0) { | ||
headers['Authorization'] = `BEARER ${jwt}`; | ||
} | ||
if (projectAccessKey) { | ||
req.setHeader('X-Access-Key', projectAccessKey); | ||
if (projectAccessKey && projectAccessKey.length > 0) { | ||
headers['X-Access-Key'] = projectAccessKey; | ||
} | ||
return req; | ||
return { | ||
url, | ||
headers, | ||
skipFetchSetup: true, | ||
fetchOptions: { | ||
mode: 'cors', | ||
cache: 'force-cache', | ||
credentials: 'same-origin', | ||
redirect: 'follow', | ||
referrer: 'client' | ||
} | ||
}; | ||
}; | ||
@@ -531,3 +459,3 @@ | ||
do(key, validMilliseconds, task, ...args) { | ||
key = `${key}:${ethers.id(JSON.stringify(args, deterministically))}`; | ||
key = `${key}:${ethers.utils.keccak256(ethers.utils.toUtf8Bytes(JSON.stringify(args, deterministically)))}`; | ||
let entry = this.cache.get(key); | ||
@@ -643,12 +571,16 @@ if (entry) { | ||
delete types['EIP712Domain']; | ||
return ethers.TypedDataEncoder.hash(typedData.domain, types, typedData.message); | ||
return ethers.utils._TypedDataEncoder.hash(typedData.domain, types, typedData.message); | ||
}; | ||
const encodeTypedDataDigest = typedData => { | ||
return ethers.getBytes(encodeTypedDataHash(typedData)); | ||
return ethers.utils.arrayify(encodeTypedDataHash(typedData)); | ||
}; | ||
const { | ||
defineProperties, | ||
resolveProperties | ||
} = ethers; | ||
defineReadOnly, | ||
getStatic, | ||
resolveProperties, | ||
checkProperties, | ||
shallowCopy, | ||
deepCopy | ||
} = utils; | ||
@@ -658,2 +590,2 @@ // urlClean removes double slashes from url path | ||
export { Logger, MAX_UINT_256, MerkleTreeGenerator, PromiseCache, base64Decode, base64DecodeObject, base64Encode, base64EncodeObject, configureLogger, defineProperties, encodeMessageDigest, encodeTypedDataDigest, encodeTypedDataHash, formatEther, formatUnits, getFetchRequest, getRandomInt, getSaleItemsLeaf, isBigNumberish, isBrowser, isNode, jwtDecodeClaims, logger, packMessageData, parseEther, parseUnits, promisify, queryStringFromObject, queryStringToObject, resolveProperties, sanitizeAlphanumeric, sanitizeHost, sanitizeNumberString, sleep, subDigestOf, toHexString, urlClean }; | ||
export { Logger, MerkleTreeGenerator, PromiseCache, base64Decode, base64DecodeObject, base64Encode, base64EncodeObject, checkProperties, configureLogger, deepCopy, defineReadOnly, encodeMessageDigest, encodeTypedDataDigest, encodeTypedDataHash, getEthersConnectionInfo, getRandomInt, getSaleItemsLeaf, getStatic, isBigNumberish, isBrowser, isNode, jwtDecodeClaims, logger, packMessageData, promisify, queryStringFromObject, queryStringToObject, resolveProperties, sanitizeAlphanumeric, sanitizeHost, sanitizeNumberString, shallowCopy, sleep, subDigestOf, urlClean }; |
import { ethers } from 'ethers'; | ||
export declare const encodeMessageDigest: (message: string | ethers.BytesLike) => Uint8Array; | ||
export declare const encodeMessageDigest: (message: string | Uint8Array) => Uint8Array; | ||
export declare const packMessageData: (walletAddress: string, chainId: ethers.BigNumberish, digest: ethers.BytesLike) => string; | ||
export declare const subDigestOf: (address: string, chainId: ethers.BigNumberish, digest: ethers.BytesLike) => string; |
export * from "./base64.js"; | ||
export * from "./bigint.js"; | ||
export * from "./big-number.js"; | ||
export * from "./digest.js"; | ||
@@ -4,0 +4,0 @@ export * from "./is-node-or-browser.js"; |
@@ -1,2 +0,2 @@ | ||
import { ethers } from 'ethers'; | ||
import { BigNumberish } from 'ethers'; | ||
import { MerkleTree } from "./merkletree/index.js"; | ||
@@ -17,4 +17,4 @@ export type ToLeaf<T> = (element: T) => string; | ||
address: string; | ||
tokenId: ethers.BigNumberish; | ||
tokenId: BigNumberish; | ||
}; | ||
export declare const getSaleItemsLeaf: ToLeaf<SaleItemsElement>; |
import { ethers } from 'ethers'; | ||
export declare const getFetchRequest: (url: string, projectAccessKey?: string, jwt?: string) => ethers.FetchRequest; | ||
export declare const getEthersConnectionInfo: (url: string, projectAccessKey?: string, jwt?: string) => ethers.utils.ConnectionInfo; |
@@ -1,4 +0,6 @@ | ||
import { ethers } from 'ethers'; | ||
declare const defineProperties: typeof ethers.defineProperties, resolveProperties: typeof ethers.resolveProperties; | ||
export { defineProperties, resolveProperties }; | ||
import { utils } from 'ethers'; | ||
type Deferrable<T> = utils.Deferrable<T>; | ||
declare const defineReadOnly: typeof utils.defineReadOnly, getStatic: typeof utils.getStatic, resolveProperties: typeof utils.resolveProperties, checkProperties: typeof utils.checkProperties, shallowCopy: typeof utils.shallowCopy, deepCopy: typeof utils.deepCopy; | ||
export type { Deferrable }; | ||
export { defineReadOnly, getStatic, resolveProperties, checkProperties, shallowCopy, deepCopy }; | ||
export type Optionals<T extends object> = Omit<T, Exclude<{ | ||
@@ -5,0 +7,0 @@ [K in keyof T]: T extends Record<K, T[K]> ? K : never; |
{ | ||
"name": "@0xsequence/utils", | ||
"version": "0.0.0-20240812142652", | ||
"version": "0.0.0-20240814043648", | ||
"description": "utils sub-package for Sequence", | ||
@@ -15,6 +15,6 @@ "repository": "https://github.com/0xsequence/sequence.js/tree/master/packages/utils", | ||
"peerDependencies": { | ||
"ethers": ">=6" | ||
"ethers": ">=5.5 < 6" | ||
}, | ||
"devDependencies": { | ||
"ethers": "^6.13.0" | ||
"ethers": "^5.7.2" | ||
}, | ||
@@ -21,0 +21,0 @@ "files": [ |
import { ethers } from 'ethers' | ||
export const encodeMessageDigest = (message: string | ethers.BytesLike) => { | ||
export const encodeMessageDigest = (message: string | Uint8Array) => { | ||
if (typeof message === 'string') { | ||
return ethers.getBytes(ethers.id(message)) | ||
return ethers.utils.arrayify(ethers.utils.keccak256(ethers.utils.toUtf8Bytes(message))) | ||
} else { | ||
return ethers.getBytes(ethers.keccak256(message)) | ||
return ethers.utils.arrayify(ethers.utils.keccak256(message)) | ||
} | ||
@@ -13,7 +13,7 @@ } | ||
export const packMessageData = (walletAddress: string, chainId: ethers.BigNumberish, digest: ethers.BytesLike): string => { | ||
return ethers.solidityPacked(['string', 'uint256', 'address', 'bytes32'], ['\x19\x01', chainId, walletAddress, digest]) | ||
return ethers.utils.solidityPack(['string', 'uint256', 'address', 'bytes32'], ['\x19\x01', chainId, walletAddress, digest]) | ||
} | ||
export const subDigestOf = (address: string, chainId: ethers.BigNumberish, digest: ethers.BytesLike): string => { | ||
return ethers.keccak256(packMessageData(address, chainId, digest)) | ||
return ethers.utils.keccak256(packMessageData(address, chainId, digest)) | ||
} |
export * from './base64' | ||
export * from './bigint' | ||
export * from './big-number' | ||
export * from './digest' | ||
@@ -4,0 +4,0 @@ export * from './is-node-or-browser' |
@@ -1,2 +0,2 @@ | ||
import { ethers } from 'ethers' | ||
import { BigNumberish, utils } from 'ethers' | ||
import { MerkleTree } from './merkletree' | ||
@@ -45,6 +45,6 @@ | ||
address: string | ||
tokenId: ethers.BigNumberish | ||
tokenId: BigNumberish | ||
} | ||
export const getSaleItemsLeaf: ToLeaf<SaleItemsElement> = element => | ||
ethers.solidityPackedKeccak256(['address', 'uint256'], [element.address.toLowerCase(), element.tokenId]) | ||
utils.solidityKeccak256(['address', 'uint256'], [element.address.toLowerCase(), element.tokenId]) |
@@ -88,3 +88,3 @@ import { ethers } from 'ethers' | ||
if (typeof value === 'string') { | ||
return ethers.getBytes(value) | ||
return ethers.utils.arrayify(value) | ||
} | ||
@@ -99,3 +99,5 @@ return value | ||
static bufferToHex(value: Uint8Array, withPrefix: boolean = true): string { | ||
const prefixed = ethers.hexlify(value) | ||
const prefixed = ethers.utils.hexlify(value, { | ||
allowMissingPrefix: true | ||
}) | ||
return withPrefix ? prefixed : prefixed.substring(2) | ||
@@ -102,0 +104,0 @@ } |
@@ -29,3 +29,3 @@ import { ethers } from 'ethers' | ||
this.hashFn = Base.bufferifyFn(ethers.keccak256) | ||
this.hashFn = Base.bufferifyFn(ethers.utils.keccak256) | ||
this.processLeaves(leaves) | ||
@@ -79,3 +79,3 @@ } | ||
const hash = this.hashFn(ethers.concat(combined)) | ||
const hash = this.hashFn(ethers.utils.concat(combined)) | ||
this.layers[layerIndex].push(hash) | ||
@@ -178,7 +178,7 @@ } | ||
} | ||
hash = this.hashFn(ethers.concat(buffers)) | ||
hash = this.hashFn(ethers.utils.concat(buffers)) | ||
} else { | ||
buffers.push(hash) | ||
buffers[isLeftNode ? 'unshift' : 'push'](data) | ||
hash = this.hashFn(ethers.concat(buffers)) | ||
hash = this.hashFn(ethers.utils.concat(buffers)) | ||
} | ||
@@ -185,0 +185,0 @@ } |
import { ethers } from 'ethers' | ||
// export const getEthersConnectionInfo = (url: string, projectAccessKey?: string, jwt?: string): ConnectionInfo => { | ||
// const headers: { | ||
// [key: string]: string | number | ||
// } = {} | ||
export const getEthersConnectionInfo = (url: string, projectAccessKey?: string, jwt?: string): ethers.utils.ConnectionInfo => { | ||
const headers: { | ||
[key: string]: string | number | ||
} = {} | ||
// if (jwt && jwt.length > 0) { | ||
// headers['Authorization'] = `BEARER ${jwt}` | ||
// } | ||
// if (projectAccessKey && projectAccessKey.length > 0) { | ||
// headers['X-Access-Key'] = projectAccessKey | ||
// } | ||
// return { | ||
// url, | ||
// headers, | ||
// skipFetchSetup: true, | ||
// fetchOptions: { | ||
// mode: 'cors', | ||
// cache: 'force-cache', | ||
// credentials: 'same-origin', | ||
// redirect: 'follow', | ||
// referrer: 'client' | ||
// } | ||
// } | ||
// } | ||
export const getFetchRequest = (url: string, projectAccessKey?: string, jwt?: string): ethers.FetchRequest => { | ||
const req = new ethers.FetchRequest(url) | ||
if (jwt) { | ||
req.setHeader('Authorization', `BEARER ${jwt}`) | ||
if (jwt && jwt.length > 0) { | ||
headers['Authorization'] = `BEARER ${jwt}` | ||
} | ||
if (projectAccessKey && projectAccessKey.length > 0) { | ||
headers['X-Access-Key'] = projectAccessKey | ||
} | ||
if (projectAccessKey) { | ||
req.setHeader('X-Access-Key', projectAccessKey) | ||
return { | ||
url, | ||
headers, | ||
skipFetchSetup: true, | ||
fetchOptions: { | ||
mode: 'cors', | ||
cache: 'force-cache', | ||
credentials: 'same-origin', | ||
redirect: 'follow', | ||
referrer: 'client' | ||
} | ||
} | ||
return req | ||
} |
@@ -16,3 +16,3 @@ import { ethers } from 'ethers' | ||
): Promise<T> { | ||
key = `${key}:${ethers.id(JSON.stringify(args, deterministically))}` | ||
key = `${key}:${ethers.utils.keccak256(ethers.utils.toUtf8Bytes(JSON.stringify(args, deterministically)))}` | ||
@@ -19,0 +19,0 @@ let entry = this.cache.get(key) |
@@ -19,7 +19,7 @@ import { ethers, TypedDataDomain, TypedDataField } from 'ethers' | ||
return ethers.TypedDataEncoder.hash(typedData.domain, types, typedData.message) | ||
return ethers.utils._TypedDataEncoder.hash(typedData.domain, types, typedData.message) | ||
} | ||
export const encodeTypedDataDigest = (typedData: TypedData): Uint8Array => { | ||
return ethers.getBytes(encodeTypedDataHash(typedData)) | ||
return ethers.utils.arrayify(encodeTypedDataHash(typedData)) | ||
} |
@@ -1,7 +0,11 @@ | ||
import { ethers } from 'ethers' | ||
import { utils } from 'ethers' | ||
const { defineProperties, resolveProperties } = ethers | ||
type Deferrable<T> = utils.Deferrable<T> | ||
export { defineProperties, resolveProperties } | ||
const { defineReadOnly, getStatic, resolveProperties, checkProperties, shallowCopy, deepCopy } = utils | ||
export type { Deferrable } | ||
export { defineReadOnly, getStatic, resolveProperties, checkProperties, shallowCopy, deepCopy } | ||
export type Optionals<T extends object> = Omit< | ||
@@ -8,0 +12,0 @@ T, |
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
96329
2520