UNPKG

@worldcoin/idkit-core

Version:

The identity SDK. Privacy-preserving identity and proof of personhood with World ID.

79 lines (77 loc) 2.78 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/lib/hashing.ts var hashing_exports = {}; __export(hashing_exports, { encodeAction: () => encodeAction, generateSignal: () => generateSignal, hashToField: () => hashToField, packAndEncode: () => packAndEncode, solidityEncode: () => solidityEncode }); module.exports = __toCommonJS(hashing_exports); var import_buffer = require("buffer/index.js"); var import_ox = require("ox"); function hashToField(input) { if (import_ox.Bytes.validate(input) || import_ox.Hex.validate(input)) return hashEncodedBytes(input); return hashString(input); } function packAndEncode(input) { const [types, values] = input.reduce( ([types2, values2], [type, value]) => { types2.push(type); values2.push(value); return [types2, values2]; }, [[], []] ); return hashEncodedBytes(import_ox.AbiParameters.encodePacked(types, values)); } function hashString(input) { const bytesInput = import_buffer.Buffer.from(input); return hashEncodedBytes(bytesInput); } function hashEncodedBytes(input) { const hash = BigInt(import_ox.Hash.keccak256(input, { as: "Hex" })) >> 8n; const rawDigest = hash.toString(16); return { hash, digest: `0x${rawDigest.padStart(64, "0")}` }; } var solidityEncode = (types, values) => { if (types.length !== values.length) { throw new Error("Types and values arrays must have the same length."); } return { types, values }; }; var generateSignal = (signal) => { if (!signal || typeof signal === "string") return hashToField(signal ?? ""); return packAndEncode(signal.types.map((type, index) => [type, signal.values[index]])); }; var encodeAction = (action) => { if (!action) return ""; if (typeof action === "string") return action; return action.types.map((type, index) => `${type}(${action.values[index]})`).join(","); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { encodeAction, generateSignal, hashToField, packAndEncode, solidityEncode });