@mysten/zklogin
Advanced tools
Comparing version 0.0.0-experimental-20230907165401 to 0.0.0-experimental-20230907171842
@@ -22,3 +22,2 @@ "use strict"; | ||
computeZkAddress: () => import_address.computeZkAddress, | ||
convertBase: () => import_utils.convertBase, | ||
genAddressSeed: () => import_utils.genAddressSeed, | ||
@@ -28,3 +27,2 @@ generateNonce: () => import_nonce.generateNonce, | ||
hashASCIIStrToField: () => import_utils.hashASCIIStrToField, | ||
hashToField: () => import_utils.hashToField, | ||
jwtToAddress: () => import_address.jwtToAddress, | ||
@@ -31,0 +29,0 @@ poseidonHash: () => import_poseidon.poseidonHash |
# @mysten/zklogin | ||
## 0.0.0-experimental-20230907165401 | ||
## 0.0.0-experimental-20230907171842 | ||
### Patch Changes | ||
- 1786c68b5: Update hashASCIIStr logic and constants | ||
- 8384490bb: Remove BCS export and introduce new getZkSignature export. | ||
@@ -8,0 +9,0 @@ - 35bdbd00d: update bcs AddressParams struct |
@@ -6,2 +6,2 @@ export { computeZkAddress, jwtToAddress } from './address.js'; | ||
export { generateNonce } from './nonce.js'; | ||
export { convertBase, hashToField, hashASCIIStrToField, genAddressSeed } from './utils.js'; | ||
export { hashASCIIStrToField, genAddressSeed } from './utils.js'; |
@@ -22,3 +22,2 @@ "use strict"; | ||
computeZkAddress: () => import_address.computeZkAddress, | ||
convertBase: () => import_utils.convertBase, | ||
genAddressSeed: () => import_utils.genAddressSeed, | ||
@@ -28,3 +27,2 @@ generateNonce: () => import_nonce.generateNonce, | ||
hashASCIIStrToField: () => import_utils.hashASCIIStrToField, | ||
hashToField: () => import_utils.hashToField, | ||
jwtToAddress: () => import_address.jwtToAddress, | ||
@@ -31,0 +29,0 @@ poseidonHash: () => import_poseidon.poseidonHash |
/// <reference types="node" /> | ||
export declare function toBufferBE(num: bigint, width: number): Buffer; | ||
/** | ||
* ConvertBase | ||
* 1. Converts each input element into exactly inWidth bits | ||
* - Prefixing zeroes if needed | ||
* 2. Splits the resulting array into chunks of outWidth bits where | ||
* the last chunk's size is <= outWidth bits. | ||
* 3. Converts each chunk into a bigint | ||
* 4. Returns a vector of size Math.ceil((inArr.length * inWidth) / outWidth) | ||
* Splits an array into chunks of size chunk_size. If the array is not evenly | ||
* divisible by chunk_size, the first chunk will be smaller than chunk_size. | ||
* | ||
* E.g., arrayChunk([1, 2, 3, 4, 5], 2) => [[1], [2, 3], [4, 5]] | ||
* | ||
* Note: Can be made more efficient by avoiding the reverse() calls. | ||
*/ | ||
export declare function convertBase(inArr: bigint[], inWidth: number, outWidth: number): bigint[]; | ||
export declare function hashToField(input: bigint[], inWidth: number): bigint; | ||
export declare function chunkArray<T>(array: T[], chunk_size: number): T[][]; | ||
export declare function hashASCIIStrToField(str: string, maxSize: number): bigint; | ||
export declare function genAddressSeed(salt: bigint, name: string, value: string, aud: string, max_name_length?: number, max_value_length?: number, max_aud_length?: number): bigint; |
@@ -21,6 +21,5 @@ "use strict"; | ||
__export(utils_exports, { | ||
convertBase: () => convertBase, | ||
chunkArray: () => chunkArray, | ||
genAddressSeed: () => genAddressSeed, | ||
hashASCIIStrToField: () => hashASCIIStrToField, | ||
hashToField: () => hashToField, | ||
toBufferBE: () => toBufferBE | ||
@@ -30,5 +29,5 @@ }); | ||
var import_poseidon = require("./poseidon.js"); | ||
const MAX_KEY_CLAIM_NAME_LENGTH = 40; | ||
const MAX_KEY_CLAIM_VALUE_LENGTH = 100; | ||
const MAX_AUD_VALUE_LENGTH = 150; | ||
const MAX_KEY_CLAIM_NAME_LENGTH = 32; | ||
const MAX_KEY_CLAIM_VALUE_LENGTH = 115; | ||
const MAX_AUD_VALUE_LENGTH = 145; | ||
const PACK_WIDTH = 248; | ||
@@ -39,27 +38,14 @@ function toBufferBE(num, width) { | ||
} | ||
function bigintArrayToBitArray(arr, intSize) { | ||
return arr.reduce((bitArray, n) => { | ||
const binaryString = n.toString(2).padStart(intSize, "0"); | ||
const bitValues = binaryString.split("").map((bit) => bit === "1" ? 1 : 0); | ||
return [...bitArray, ...bitValues]; | ||
}, []); | ||
function chunkArray(array, chunk_size) { | ||
const chunks = Array(Math.ceil(array.length / chunk_size)); | ||
const revArray = array.reverse(); | ||
for (let i = 0; i < chunks.length; i++) { | ||
chunks[i] = revArray.slice(i * chunk_size, (i + 1) * chunk_size).reverse(); | ||
} | ||
return chunks.reverse(); | ||
} | ||
function chunkArray(arr, chunkSize) { | ||
return Array.from( | ||
{ length: Math.ceil(arr.length / chunkSize) }, | ||
(_, i) => arr.slice(i * chunkSize, (i + 1) * chunkSize) | ||
); | ||
function bytesBEToBigInt(bytes) { | ||
const hex = bytes.map((b) => b.toString(16).padStart(2, "0")).join(""); | ||
return BigInt("0x" + hex); | ||
} | ||
function convertBase(inArr, inWidth, outWidth) { | ||
const bits = bigintArrayToBitArray(inArr, inWidth); | ||
const packed = chunkArray(bits, outWidth).map((chunk) => BigInt("0b" + chunk.join(""))); | ||
return packed; | ||
} | ||
function hashToField(input, inWidth) { | ||
if (PACK_WIDTH % 8 !== 0) { | ||
throw new Error("PACK_WIDTH must be a multiple of 8"); | ||
} | ||
const packed = convertBase(input, inWidth, PACK_WIDTH); | ||
return (0, import_poseidon.poseidonHash)(packed); | ||
} | ||
function hashASCIIStrToField(str, maxSize) { | ||
@@ -69,4 +55,6 @@ if (str.length > maxSize) { | ||
} | ||
const strPadded = str.padEnd(maxSize, String.fromCharCode(0)).split("").map((c) => BigInt(c.charCodeAt(0))); | ||
return hashToField(strPadded, 8); | ||
const strPadded = str.padEnd(maxSize, String.fromCharCode(0)).split("").map((c) => c.charCodeAt(0)); | ||
const chunkSize = PACK_WIDTH / 8; | ||
const packed = chunkArray(strPadded, chunkSize).map((chunk) => bytesBEToBigInt(chunk)); | ||
return (0, import_poseidon.poseidonHash)(packed); | ||
} | ||
@@ -73,0 +61,0 @@ function genAddressSeed(salt, name, value, aud, max_name_length = MAX_KEY_CLAIM_NAME_LENGTH, max_value_length = MAX_KEY_CLAIM_VALUE_LENGTH, max_aud_length = MAX_AUD_VALUE_LENGTH) { |
@@ -6,2 +6,2 @@ export { computeZkAddress, jwtToAddress } from './address.js'; | ||
export { generateNonce } from './nonce.js'; | ||
export { convertBase, hashToField, hashASCIIStrToField, genAddressSeed } from './utils.js'; | ||
export { hashASCIIStrToField, genAddressSeed } from './utils.js'; |
@@ -5,6 +5,5 @@ import { computeZkAddress, jwtToAddress } from "./address.js"; | ||
import { generateNonce } from "./nonce.js"; | ||
import { convertBase, hashToField, hashASCIIStrToField, genAddressSeed } from "./utils.js"; | ||
import { hashASCIIStrToField, genAddressSeed } from "./utils.js"; | ||
export { | ||
computeZkAddress, | ||
convertBase, | ||
genAddressSeed, | ||
@@ -14,3 +13,2 @@ generateNonce, | ||
hashASCIIStrToField, | ||
hashToField, | ||
jwtToAddress, | ||
@@ -17,0 +15,0 @@ poseidonHash |
/// <reference types="node" /> | ||
export declare function toBufferBE(num: bigint, width: number): Buffer; | ||
/** | ||
* ConvertBase | ||
* 1. Converts each input element into exactly inWidth bits | ||
* - Prefixing zeroes if needed | ||
* 2. Splits the resulting array into chunks of outWidth bits where | ||
* the last chunk's size is <= outWidth bits. | ||
* 3. Converts each chunk into a bigint | ||
* 4. Returns a vector of size Math.ceil((inArr.length * inWidth) / outWidth) | ||
* Splits an array into chunks of size chunk_size. If the array is not evenly | ||
* divisible by chunk_size, the first chunk will be smaller than chunk_size. | ||
* | ||
* E.g., arrayChunk([1, 2, 3, 4, 5], 2) => [[1], [2, 3], [4, 5]] | ||
* | ||
* Note: Can be made more efficient by avoiding the reverse() calls. | ||
*/ | ||
export declare function convertBase(inArr: bigint[], inWidth: number, outWidth: number): bigint[]; | ||
export declare function hashToField(input: bigint[], inWidth: number): bigint; | ||
export declare function chunkArray<T>(array: T[], chunk_size: number): T[][]; | ||
export declare function hashASCIIStrToField(str: string, maxSize: number): bigint; | ||
export declare function genAddressSeed(salt: bigint, name: string, value: string, aud: string, max_name_length?: number, max_value_length?: number, max_aud_length?: number): bigint; |
import { poseidonHash } from "./poseidon.js"; | ||
const MAX_KEY_CLAIM_NAME_LENGTH = 40; | ||
const MAX_KEY_CLAIM_VALUE_LENGTH = 100; | ||
const MAX_AUD_VALUE_LENGTH = 150; | ||
const MAX_KEY_CLAIM_NAME_LENGTH = 32; | ||
const MAX_KEY_CLAIM_VALUE_LENGTH = 115; | ||
const MAX_AUD_VALUE_LENGTH = 145; | ||
const PACK_WIDTH = 248; | ||
@@ -10,27 +10,14 @@ function toBufferBE(num, width) { | ||
} | ||
function bigintArrayToBitArray(arr, intSize) { | ||
return arr.reduce((bitArray, n) => { | ||
const binaryString = n.toString(2).padStart(intSize, "0"); | ||
const bitValues = binaryString.split("").map((bit) => bit === "1" ? 1 : 0); | ||
return [...bitArray, ...bitValues]; | ||
}, []); | ||
function chunkArray(array, chunk_size) { | ||
const chunks = Array(Math.ceil(array.length / chunk_size)); | ||
const revArray = array.reverse(); | ||
for (let i = 0; i < chunks.length; i++) { | ||
chunks[i] = revArray.slice(i * chunk_size, (i + 1) * chunk_size).reverse(); | ||
} | ||
return chunks.reverse(); | ||
} | ||
function chunkArray(arr, chunkSize) { | ||
return Array.from( | ||
{ length: Math.ceil(arr.length / chunkSize) }, | ||
(_, i) => arr.slice(i * chunkSize, (i + 1) * chunkSize) | ||
); | ||
function bytesBEToBigInt(bytes) { | ||
const hex = bytes.map((b) => b.toString(16).padStart(2, "0")).join(""); | ||
return BigInt("0x" + hex); | ||
} | ||
function convertBase(inArr, inWidth, outWidth) { | ||
const bits = bigintArrayToBitArray(inArr, inWidth); | ||
const packed = chunkArray(bits, outWidth).map((chunk) => BigInt("0b" + chunk.join(""))); | ||
return packed; | ||
} | ||
function hashToField(input, inWidth) { | ||
if (PACK_WIDTH % 8 !== 0) { | ||
throw new Error("PACK_WIDTH must be a multiple of 8"); | ||
} | ||
const packed = convertBase(input, inWidth, PACK_WIDTH); | ||
return poseidonHash(packed); | ||
} | ||
function hashASCIIStrToField(str, maxSize) { | ||
@@ -40,4 +27,6 @@ if (str.length > maxSize) { | ||
} | ||
const strPadded = str.padEnd(maxSize, String.fromCharCode(0)).split("").map((c) => BigInt(c.charCodeAt(0))); | ||
return hashToField(strPadded, 8); | ||
const strPadded = str.padEnd(maxSize, String.fromCharCode(0)).split("").map((c) => c.charCodeAt(0)); | ||
const chunkSize = PACK_WIDTH / 8; | ||
const packed = chunkArray(strPadded, chunkSize).map((chunk) => bytesBEToBigInt(chunk)); | ||
return poseidonHash(packed); | ||
} | ||
@@ -53,8 +42,7 @@ function genAddressSeed(salt, name, value, aud, max_name_length = MAX_KEY_CLAIM_NAME_LENGTH, max_value_length = MAX_KEY_CLAIM_VALUE_LENGTH, max_aud_length = MAX_AUD_VALUE_LENGTH) { | ||
export { | ||
convertBase, | ||
chunkArray, | ||
genAddressSeed, | ||
hashASCIIStrToField, | ||
hashToField, | ||
toBufferBE | ||
}; | ||
//# sourceMappingURL=utils.js.map |
{ | ||
"name": "@mysten/zklogin", | ||
"version": "0.0.0-experimental-20230907165401", | ||
"version": "0.0.0-experimental-20230907171842", | ||
"description": "Utilities for interacting with zkLogin in Sui", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
@@ -13,2 +13,2 @@ // Copyright (c) Mysten Labs, Inc. | ||
export { convertBase, hashToField, hashASCIIStrToField, genAddressSeed } from './utils.js'; | ||
export { hashASCIIStrToField, genAddressSeed } from './utils.js'; |
@@ -6,7 +6,5 @@ // Copyright (c) Mysten Labs, Inc. | ||
type bit = 0 | 1; | ||
const MAX_KEY_CLAIM_NAME_LENGTH = 40; | ||
const MAX_KEY_CLAIM_VALUE_LENGTH = 100; | ||
const MAX_AUD_VALUE_LENGTH = 150; | ||
const MAX_KEY_CLAIM_NAME_LENGTH = 32; | ||
const MAX_KEY_CLAIM_VALUE_LENGTH = 115; | ||
const MAX_AUD_VALUE_LENGTH = 145; | ||
const PACK_WIDTH = 248; | ||
@@ -20,38 +18,22 @@ | ||
function bigintArrayToBitArray(arr: bigint[], intSize: number): bit[] { | ||
return arr.reduce((bitArray, n) => { | ||
const binaryString = n.toString(2).padStart(intSize, '0'); | ||
const bitValues = binaryString.split('').map((bit) => (bit === '1' ? 1 : 0)); | ||
return [...bitArray, ...bitValues]; | ||
}, [] as bit[]); | ||
} | ||
function chunkArray<T>(arr: T[], chunkSize: number) { | ||
return Array.from({ length: Math.ceil(arr.length / chunkSize) }, (_, i) => | ||
arr.slice(i * chunkSize, (i + 1) * chunkSize), | ||
); | ||
} | ||
/** | ||
* ConvertBase | ||
* 1. Converts each input element into exactly inWidth bits | ||
* - Prefixing zeroes if needed | ||
* 2. Splits the resulting array into chunks of outWidth bits where | ||
* the last chunk's size is <= outWidth bits. | ||
* 3. Converts each chunk into a bigint | ||
* 4. Returns a vector of size Math.ceil((inArr.length * inWidth) / outWidth) | ||
* Splits an array into chunks of size chunk_size. If the array is not evenly | ||
* divisible by chunk_size, the first chunk will be smaller than chunk_size. | ||
* | ||
* E.g., arrayChunk([1, 2, 3, 4, 5], 2) => [[1], [2, 3], [4, 5]] | ||
* | ||
* Note: Can be made more efficient by avoiding the reverse() calls. | ||
*/ | ||
export function convertBase(inArr: bigint[], inWidth: number, outWidth: number): bigint[] { | ||
const bits = bigintArrayToBitArray(inArr, inWidth); | ||
const packed = chunkArray(bits, outWidth).map((chunk) => BigInt('0b' + chunk.join(''))); | ||
return packed; | ||
export function chunkArray<T>(array: T[], chunk_size: number): T[][] { | ||
const chunks = Array(Math.ceil(array.length / chunk_size)); | ||
const revArray = array.reverse(); | ||
for (let i = 0; i < chunks.length; i++) { | ||
chunks[i] = revArray.slice(i * chunk_size, (i + 1) * chunk_size).reverse(); | ||
} | ||
return chunks.reverse(); | ||
} | ||
// hashes a stream of bigints to a field element | ||
export function hashToField(input: bigint[], inWidth: number) { | ||
if (PACK_WIDTH % 8 !== 0) { | ||
throw new Error('PACK_WIDTH must be a multiple of 8'); | ||
} | ||
const packed = convertBase(input, inWidth, PACK_WIDTH); | ||
return poseidonHash(packed); | ||
function bytesBEToBigInt(bytes: number[]): bigint { | ||
const hex = bytes.map((b) => b.toString(16).padStart(2, '0')).join(''); | ||
return BigInt('0x' + hex); | ||
} | ||
@@ -70,5 +52,7 @@ | ||
.split('') | ||
.map((c) => BigInt(c.charCodeAt(0))); | ||
.map((c) => c.charCodeAt(0)); | ||
return hashToField(strPadded, 8); | ||
const chunkSize = PACK_WIDTH / 8; | ||
const packed = chunkArray(strPadded, chunkSize).map((chunk) => bytesBEToBigInt(chunk)); | ||
return poseidonHash(packed); | ||
} | ||
@@ -75,0 +59,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
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
149581
972