@0xsequence/utils
Advanced tools
Comparing version 0.0.0-20240820173337 to 0.0.0-20240829183049
@@ -8,26 +8,23 @@ 'use strict'; | ||
const base64Encode = val => { | ||
return jsBase64.Base64.encode(val, true); | ||
}; | ||
const base64EncodeObject = obj => { | ||
return jsBase64.Base64.encode(JSON.stringify(obj), true); | ||
}; | ||
const base64Decode = encodedString => { | ||
if (encodedString === null || encodedString === undefined) { | ||
return undefined; | ||
const extractProjectIdFromAccessKey = accessKey => { | ||
// Convert URL-safe base64 string to standard base64 string | ||
const base64String = accessKey.replace(/-/g, '+').replace(/_/g, '/'); | ||
// Decode the base64 string to a binary string | ||
const binaryString = atob(base64String); | ||
// Convert the binary string to a byte array (Uint8Array) | ||
const byteArray = new Uint8Array(binaryString.length); | ||
for (let i = 0; i < binaryString.length; i++) { | ||
byteArray[i] = binaryString.charCodeAt(i); | ||
} | ||
return jsBase64.Base64.decode(encodedString); | ||
}; | ||
const base64DecodeObject = encodedObject => { | ||
if (encodedObject === null || encodedObject === undefined) { | ||
return undefined; | ||
if (byteArray[0] !== 1) { | ||
throw new Error('UnsupportedVersion'); | ||
} | ||
return JSON.parse(jsBase64.Base64.decode(encodedObject)); | ||
// Extract the project ID from bytes 2 to 9 (8 bytes) | ||
const projectIdBytes = byteArray.slice(1, 9); | ||
const projectId = projectIdBytes[7] | projectIdBytes[6] << 8 | projectIdBytes[5] << 16 | projectIdBytes[4] << 24 | projectIdBytes[3] << 32 | projectIdBytes[2] << 40 | projectIdBytes[1] << 48 | projectIdBytes[0] << 56; | ||
return projectId; | ||
}; | ||
// Monkey patch toJSON on BigInt to return a string | ||
BigInt.prototype.toJSON = function () { | ||
return this.toString(); | ||
}; | ||
const MAX_UINT_256 = BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'); | ||
@@ -92,2 +89,44 @@ | ||
// JSON.stringify doesn't handle BigInts, so we need to replace them with objects | ||
const bigintReplacer = (key, value) => { | ||
if (typeof value === 'bigint') { | ||
return { | ||
$bigint: value.toString() | ||
}; | ||
} | ||
return value; | ||
}; | ||
// JSON.parse will need to convert our serialized bigints back into BigInt | ||
const bigintReviver = (key, value) => { | ||
if (value !== null && typeof value === 'object' && '$bigint' in value && typeof value.$bigint === 'string') { | ||
return BigInt(value.$bigint); | ||
} | ||
// BigNumber compatibility with older versions of sequence.js with ethers v5 | ||
if (value !== null && typeof value === 'object' && value.type === 'BigNumber' && ethers.ethers.isHexString(value.hex)) { | ||
return BigInt(value.hex); | ||
} | ||
return value; | ||
}; | ||
const base64Encode = val => { | ||
return jsBase64.Base64.encode(val, true); | ||
}; | ||
const base64EncodeObject = obj => { | ||
return jsBase64.Base64.encode(JSON.stringify(obj, bigintReplacer), true); | ||
}; | ||
const base64Decode = encodedString => { | ||
if (encodedString === null || encodedString === undefined) { | ||
return undefined; | ||
} | ||
return jsBase64.Base64.decode(encodedString); | ||
}; | ||
const base64DecodeObject = encodedObject => { | ||
if (encodedObject === null || encodedObject === undefined) { | ||
return undefined; | ||
} | ||
return JSON.parse(jsBase64.Base64.decode(encodedObject), bigintReviver); | ||
}; | ||
const encodeMessageDigest = message => { | ||
@@ -662,2 +701,4 @@ if (typeof message === 'string') { | ||
exports.base64EncodeObject = base64EncodeObject; | ||
exports.bigintReplacer = bigintReplacer; | ||
exports.bigintReviver = bigintReviver; | ||
exports.configureLogger = configureLogger; | ||
@@ -668,2 +709,3 @@ exports.defineProperties = defineProperties; | ||
exports.encodeTypedDataHash = encodeTypedDataHash; | ||
exports.extractProjectIdFromAccessKey = extractProjectIdFromAccessKey; | ||
exports.formatEther = formatEther; | ||
@@ -670,0 +712,0 @@ exports.formatUnits = formatUnits; |
@@ -8,26 +8,23 @@ 'use strict'; | ||
const base64Encode = val => { | ||
return jsBase64.Base64.encode(val, true); | ||
}; | ||
const base64EncodeObject = obj => { | ||
return jsBase64.Base64.encode(JSON.stringify(obj), true); | ||
}; | ||
const base64Decode = encodedString => { | ||
if (encodedString === null || encodedString === undefined) { | ||
return undefined; | ||
const extractProjectIdFromAccessKey = accessKey => { | ||
// Convert URL-safe base64 string to standard base64 string | ||
const base64String = accessKey.replace(/-/g, '+').replace(/_/g, '/'); | ||
// Decode the base64 string to a binary string | ||
const binaryString = atob(base64String); | ||
// Convert the binary string to a byte array (Uint8Array) | ||
const byteArray = new Uint8Array(binaryString.length); | ||
for (let i = 0; i < binaryString.length; i++) { | ||
byteArray[i] = binaryString.charCodeAt(i); | ||
} | ||
return jsBase64.Base64.decode(encodedString); | ||
}; | ||
const base64DecodeObject = encodedObject => { | ||
if (encodedObject === null || encodedObject === undefined) { | ||
return undefined; | ||
if (byteArray[0] !== 1) { | ||
throw new Error('UnsupportedVersion'); | ||
} | ||
return JSON.parse(jsBase64.Base64.decode(encodedObject)); | ||
// Extract the project ID from bytes 2 to 9 (8 bytes) | ||
const projectIdBytes = byteArray.slice(1, 9); | ||
const projectId = projectIdBytes[7] | projectIdBytes[6] << 8 | projectIdBytes[5] << 16 | projectIdBytes[4] << 24 | projectIdBytes[3] << 32 | projectIdBytes[2] << 40 | projectIdBytes[1] << 48 | projectIdBytes[0] << 56; | ||
return projectId; | ||
}; | ||
// Monkey patch toJSON on BigInt to return a string | ||
BigInt.prototype.toJSON = function () { | ||
return this.toString(); | ||
}; | ||
const MAX_UINT_256 = BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'); | ||
@@ -92,2 +89,44 @@ | ||
// JSON.stringify doesn't handle BigInts, so we need to replace them with objects | ||
const bigintReplacer = (key, value) => { | ||
if (typeof value === 'bigint') { | ||
return { | ||
$bigint: value.toString() | ||
}; | ||
} | ||
return value; | ||
}; | ||
// JSON.parse will need to convert our serialized bigints back into BigInt | ||
const bigintReviver = (key, value) => { | ||
if (value !== null && typeof value === 'object' && '$bigint' in value && typeof value.$bigint === 'string') { | ||
return BigInt(value.$bigint); | ||
} | ||
// BigNumber compatibility with older versions of sequence.js with ethers v5 | ||
if (value !== null && typeof value === 'object' && value.type === 'BigNumber' && ethers.ethers.isHexString(value.hex)) { | ||
return BigInt(value.hex); | ||
} | ||
return value; | ||
}; | ||
const base64Encode = val => { | ||
return jsBase64.Base64.encode(val, true); | ||
}; | ||
const base64EncodeObject = obj => { | ||
return jsBase64.Base64.encode(JSON.stringify(obj, bigintReplacer), true); | ||
}; | ||
const base64Decode = encodedString => { | ||
if (encodedString === null || encodedString === undefined) { | ||
return undefined; | ||
} | ||
return jsBase64.Base64.decode(encodedString); | ||
}; | ||
const base64DecodeObject = encodedObject => { | ||
if (encodedObject === null || encodedObject === undefined) { | ||
return undefined; | ||
} | ||
return JSON.parse(jsBase64.Base64.decode(encodedObject), bigintReviver); | ||
}; | ||
const encodeMessageDigest = message => { | ||
@@ -662,2 +701,4 @@ if (typeof message === 'string') { | ||
exports.base64EncodeObject = base64EncodeObject; | ||
exports.bigintReplacer = bigintReplacer; | ||
exports.bigintReviver = bigintReviver; | ||
exports.configureLogger = configureLogger; | ||
@@ -668,2 +709,3 @@ exports.defineProperties = defineProperties; | ||
exports.encodeTypedDataHash = encodeTypedDataHash; | ||
exports.extractProjectIdFromAccessKey = extractProjectIdFromAccessKey; | ||
exports.formatEther = formatEther; | ||
@@ -670,0 +712,0 @@ exports.formatUnits = formatUnits; |
import { Base64 } from 'js-base64'; | ||
import { ethers } from 'ethers'; | ||
const base64Encode = val => { | ||
return Base64.encode(val, true); | ||
}; | ||
const base64EncodeObject = obj => { | ||
return Base64.encode(JSON.stringify(obj), true); | ||
}; | ||
const base64Decode = encodedString => { | ||
if (encodedString === null || encodedString === undefined) { | ||
return undefined; | ||
const extractProjectIdFromAccessKey = accessKey => { | ||
// Convert URL-safe base64 string to standard base64 string | ||
const base64String = accessKey.replace(/-/g, '+').replace(/_/g, '/'); | ||
// Decode the base64 string to a binary string | ||
const binaryString = atob(base64String); | ||
// Convert the binary string to a byte array (Uint8Array) | ||
const byteArray = new Uint8Array(binaryString.length); | ||
for (let i = 0; i < binaryString.length; i++) { | ||
byteArray[i] = binaryString.charCodeAt(i); | ||
} | ||
return Base64.decode(encodedString); | ||
}; | ||
const base64DecodeObject = encodedObject => { | ||
if (encodedObject === null || encodedObject === undefined) { | ||
return undefined; | ||
if (byteArray[0] !== 1) { | ||
throw new Error('UnsupportedVersion'); | ||
} | ||
return JSON.parse(Base64.decode(encodedObject)); | ||
// Extract the project ID from bytes 2 to 9 (8 bytes) | ||
const projectIdBytes = byteArray.slice(1, 9); | ||
const projectId = projectIdBytes[7] | projectIdBytes[6] << 8 | projectIdBytes[5] << 16 | projectIdBytes[4] << 24 | projectIdBytes[3] << 32 | projectIdBytes[2] << 40 | projectIdBytes[1] << 48 | projectIdBytes[0] << 56; | ||
return projectId; | ||
}; | ||
// Monkey patch toJSON on BigInt to return a string | ||
BigInt.prototype.toJSON = function () { | ||
return this.toString(); | ||
}; | ||
const MAX_UINT_256 = BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'); | ||
@@ -87,2 +84,44 @@ | ||
// JSON.stringify doesn't handle BigInts, so we need to replace them with objects | ||
const bigintReplacer = (key, value) => { | ||
if (typeof value === 'bigint') { | ||
return { | ||
$bigint: value.toString() | ||
}; | ||
} | ||
return value; | ||
}; | ||
// JSON.parse will need to convert our serialized bigints back into BigInt | ||
const bigintReviver = (key, value) => { | ||
if (value !== null && typeof value === 'object' && '$bigint' in value && typeof value.$bigint === 'string') { | ||
return BigInt(value.$bigint); | ||
} | ||
// BigNumber compatibility with older versions of sequence.js with ethers v5 | ||
if (value !== null && typeof value === 'object' && value.type === 'BigNumber' && ethers.isHexString(value.hex)) { | ||
return BigInt(value.hex); | ||
} | ||
return value; | ||
}; | ||
const base64Encode = val => { | ||
return Base64.encode(val, true); | ||
}; | ||
const base64EncodeObject = obj => { | ||
return Base64.encode(JSON.stringify(obj, bigintReplacer), true); | ||
}; | ||
const base64Decode = encodedString => { | ||
if (encodedString === null || encodedString === undefined) { | ||
return undefined; | ||
} | ||
return Base64.decode(encodedString); | ||
}; | ||
const base64DecodeObject = encodedObject => { | ||
if (encodedObject === null || encodedObject === undefined) { | ||
return undefined; | ||
} | ||
return JSON.parse(Base64.decode(encodedObject), bigintReviver); | ||
}; | ||
const encodeMessageDigest = message => { | ||
@@ -649,2 +688,2 @@ if (typeof message === 'string') { | ||
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, MAX_UINT_256, MerkleTreeGenerator, PromiseCache, base64Decode, base64DecodeObject, base64Encode, base64EncodeObject, bigintReplacer, bigintReviver, configureLogger, defineProperties, encodeMessageDigest, encodeTypedDataDigest, encodeTypedDataHash, extractProjectIdFromAccessKey, formatEther, formatUnits, getFetchRequest, getRandomInt, getSaleItemsLeaf, isBigNumberish, isBrowser, isNode, jwtDecodeClaims, logger, packMessageData, parseEther, parseUnits, promisify, queryStringFromObject, queryStringToObject, resolveProperties, sanitizeAlphanumeric, sanitizeHost, sanitizeNumberString, sleep, subDigestOf, toHexString, urlClean }; |
import { ethers } from 'ethers'; | ||
declare global { | ||
interface BigInt { | ||
toJSON(): string; | ||
} | ||
} | ||
export declare const MAX_UINT_256: bigint; | ||
@@ -14,1 +9,3 @@ export declare const isBigNumberish: (value: any) => value is ethers.BigNumberish; | ||
export declare const formatEther: (value: bigint) => string; | ||
export declare const bigintReplacer: (key: string, value: any) => any; | ||
export declare const bigintReviver: (key: string, value: any) => any; |
@@ -0,1 +1,2 @@ | ||
export * from "./access-key.js"; | ||
export * from "./base64.js"; | ||
@@ -2,0 +3,0 @@ export * from "./bigint.js"; |
{ | ||
"name": "@0xsequence/utils", | ||
"version": "0.0.0-20240820173337", | ||
"version": "0.0.0-20240829183049", | ||
"description": "utils sub-package for Sequence", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/0xsequence/sequence.js/tree/master/packages/utils", |
import { Base64 } from 'js-base64' | ||
import { bigintReplacer, bigintReviver } from './bigint' | ||
@@ -8,3 +9,3 @@ export const base64Encode = (val: string): string => { | ||
export const base64EncodeObject = (obj: any): string => { | ||
return Base64.encode(JSON.stringify(obj), true) | ||
return Base64.encode(JSON.stringify(obj, bigintReplacer), true) | ||
} | ||
@@ -23,3 +24,3 @@ | ||
} | ||
return JSON.parse(Base64.decode(encodedObject)) as T | ||
return JSON.parse(Base64.decode(encodedObject), bigintReviver) as T | ||
} |
import { ethers } from 'ethers' | ||
// Monkey patch toJSON on BigInt to return a string | ||
declare global { | ||
interface BigInt { | ||
toJSON(): string | ||
} | ||
} | ||
BigInt.prototype.toJSON = function () { | ||
return this.toString() | ||
} | ||
export const MAX_UINT_256 = BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff') | ||
@@ -97,1 +85,24 @@ | ||
export const formatEther = (value: bigint): string => formatUnits(value, 18) | ||
// JSON.stringify doesn't handle BigInts, so we need to replace them with objects | ||
export const bigintReplacer = (key: string, value: any): any => { | ||
if (typeof value === 'bigint') { | ||
return { $bigint: value.toString() } | ||
} | ||
return value | ||
} | ||
// JSON.parse will need to convert our serialized bigints back into BigInt | ||
export const bigintReviver = (key: string, value: any): any => { | ||
if (value !== null && typeof value === 'object' && '$bigint' in value && typeof value.$bigint === 'string') { | ||
return BigInt(value.$bigint) | ||
} | ||
// BigNumber compatibility with older versions of sequence.js with ethers v5 | ||
if (value !== null && typeof value === 'object' && value.type === 'BigNumber' && ethers.isHexString(value.hex)) { | ||
return BigInt(value.hex) | ||
} | ||
return value | ||
} |
@@ -0,1 +1,2 @@ | ||
export * from './access-key' | ||
export * from './base64' | ||
@@ -2,0 +3,0 @@ export * from './bigint' |
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
112638
53
2924