0f87c61bfa26b8e629b4590fbae43ddd02b610edff3f3de14919814db66564d0.json raw

   1  {"ast":null,"code":"\"use strict\";\n\nObject.defineProperty(exports, \"__esModule\", {\n  value: true\n});\nexports.deriveKeysetId = exports.deserializeMintKeys = exports.serializeMintKeys = exports.createRandomPrivateKey = exports.getKeysetIdInt = exports.pointFromHex = exports.pointFromBytes = exports.hash_e = exports.hashToCurve = void 0;\nconst secp256k1_1 = require(\"@noble/curves/secp256k1\");\nconst sha256_1 = require(\"@noble/hashes/sha256\");\nconst utils_1 = require(\"@noble/curves/abstract/utils\");\nconst utils_js_1 = require(\"../util/utils.js\");\nconst buffer_1 = require(\"buffer/\");\nconst DOMAIN_SEPARATOR = (0, utils_1.hexToBytes)('536563703235366b315f48617368546f43757276655f43617368755f');\nfunction hashToCurve(secret) {\n  const msgToHash = (0, sha256_1.sha256)(buffer_1.Buffer.concat([DOMAIN_SEPARATOR, secret]));\n  const counter = new Uint32Array(1);\n  const maxIterations = 2 ** 16;\n  for (let i = 0; i < maxIterations; i++) {\n    const counterBytes = new Uint8Array(counter.buffer);\n    const hash = (0, sha256_1.sha256)(buffer_1.Buffer.concat([msgToHash, counterBytes]));\n    try {\n      return pointFromHex((0, utils_1.bytesToHex)(buffer_1.Buffer.concat([new Uint8Array([0x02]), hash])));\n    } catch (error) {\n      counter[0]++;\n    }\n  }\n  throw new Error('No valid point found');\n}\nexports.hashToCurve = hashToCurve;\nfunction hash_e(pubkeys) {\n  const hexStrings = pubkeys.map(p => p.toHex(false));\n  const e_ = hexStrings.join('');\n  const e = (0, sha256_1.sha256)(new TextEncoder().encode(e_));\n  return e;\n}\nexports.hash_e = hash_e;\nfunction pointFromBytes(bytes) {\n  return secp256k1_1.secp256k1.ProjectivePoint.fromHex((0, utils_1.bytesToHex)(bytes));\n}\nexports.pointFromBytes = pointFromBytes;\nfunction pointFromHex(hex) {\n  return secp256k1_1.secp256k1.ProjectivePoint.fromHex(hex);\n}\nexports.pointFromHex = pointFromHex;\nconst getKeysetIdInt = keysetId => {\n  let keysetIdInt;\n  if (/^[a-fA-F0-9]+$/.test(keysetId)) {\n    keysetIdInt = (0, utils_js_1.hexToNumber)(keysetId) % BigInt(2 ** 31 - 1);\n  } else {\n    //legacy keyset compatibility\n    keysetIdInt = (0, utils_js_1.bytesToNumber)((0, utils_js_1.encodeBase64toUint8)(keysetId)) % BigInt(2 ** 31 - 1);\n  }\n  return keysetIdInt;\n};\nexports.getKeysetIdInt = getKeysetIdInt;\nfunction createRandomPrivateKey() {\n  return secp256k1_1.secp256k1.utils.randomPrivateKey();\n}\nexports.createRandomPrivateKey = createRandomPrivateKey;\nfunction serializeMintKeys(mintKeys) {\n  const serializedMintKeys = {};\n  Object.keys(mintKeys).forEach(p => {\n    serializedMintKeys[p] = (0, utils_1.bytesToHex)(mintKeys[p]);\n  });\n  return serializedMintKeys;\n}\nexports.serializeMintKeys = serializeMintKeys;\nfunction deserializeMintKeys(serializedMintKeys) {\n  const mintKeys = {};\n  Object.keys(serializedMintKeys).forEach(p => {\n    mintKeys[p] = (0, utils_1.hexToBytes)(serializedMintKeys[p]);\n  });\n  return mintKeys;\n}\nexports.deserializeMintKeys = deserializeMintKeys;\nfunction deriveKeysetId(keys) {\n  const KEYSET_VERSION = '00';\n  const mapBigInt = k => {\n    return [BigInt(k[0]), k[1]];\n  };\n  const pubkeysConcat = Object.entries(serializeMintKeys(keys)).map(mapBigInt).sort((a, b) => a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0).map(([, pubKey]) => (0, utils_1.hexToBytes)(pubKey)).reduce((prev, curr) => mergeUInt8Arrays(prev, curr), new Uint8Array());\n  const hash = (0, sha256_1.sha256)(pubkeysConcat);\n  const hashHex = buffer_1.Buffer.from(hash).toString('hex').slice(0, 14);\n  return '00' + hashHex;\n}\nexports.deriveKeysetId = deriveKeysetId;\nfunction mergeUInt8Arrays(a1, a2) {\n  // sum of individual array lengths\n  const mergedArray = new Uint8Array(a1.length + a2.length);\n  mergedArray.set(a1);\n  mergedArray.set(a2, a1.length);\n  return mergedArray;\n}\n//# sourceMappingURL=index.js.map","map":null,"metadata":{},"sourceType":"script","externalDependencies":[]}