@neuraiproject/neurai-key 3.0.0 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -21504,6 +21504,13 @@ function uint32ToBytesBE(value) {
21504
21504
  function hash160(data) {
21505
21505
  return ripemd160(sha256(data));
21506
21506
  }
21507
+ function sha256Hash(data) {
21508
+ return sha256(data);
21509
+ }
21510
+ function taggedHash(tag, data) {
21511
+ const tagHash = sha256(utf8ToBytes(tag));
21512
+ return sha256(concatBytes(tagHash, tagHash, data));
21513
+ }
21507
21514
  function doubleSha256(data) {
21508
21515
  return sha256(sha256(data));
21509
21516
  }
@@ -21544,7 +21551,6 @@ function mnemonicToSeedBytes(mnemonicToSeedSync, mnemonic, passphrase) {
21544
21551
  return Uint8Array.from(mnemonicToSeedSync(mnemonic, passphrase));
21545
21552
  }
21546
21553
  const BITCOIN_SEED_KEY = utf8ToBytes("Bitcoin seed");
21547
- const HASH160_PREFIX = Uint8Array.from([0x05]);
21548
21554
 
21549
21555
  /**
21550
21556
  * Utils for modular division and fields.
@@ -23709,6 +23715,10 @@ function requireDist () {
23709
23715
 
23710
23716
  var distExports = requireDist();
23711
23717
 
23718
+ const AUTHSCRIPT_TAG = "NeuraiAuthScript";
23719
+ const AUTHSCRIPT_VERSION = 0x01;
23720
+ const PQ_AUTH_TYPE = 0x01;
23721
+ const DEFAULT_WITNESS_SCRIPT = Uint8Array.from([0x51]);
23712
23722
  function encodeWIF(privateKey, version, compressed = true) {
23713
23723
  const payload = compressed
23714
23724
  ? concatBytes(Uint8Array.from([version]), privateKey, Uint8Array.from([0x01]))
@@ -23765,9 +23775,29 @@ function publicKeyHexFromWIF(wif, compressed = true) {
23765
23775
  function bech32mEncode(hrp, witnessVersion, hash) {
23766
23776
  return distExports.bech32m.encode(hrp, [witnessVersion, ...distExports.bech32m.toWords(hash)]);
23767
23777
  }
23768
- function pqPublicKeyToAddressBytes(publicKey, network) {
23769
- const serialized = concatBytes(HASH160_PREFIX, publicKey);
23770
- return bech32mEncode(network.hrp, network.witnessVersion, hash160(serialized));
23778
+ function normalizeWitnessScript(input) {
23779
+ return input ? ensureBytes(input) : Uint8Array.from(DEFAULT_WITNESS_SCRIPT);
23780
+ }
23781
+ function pqPublicKeyToAuthDescriptor(publicKey) {
23782
+ return concatBytes(Uint8Array.from([PQ_AUTH_TYPE]), hash160(publicKey));
23783
+ }
23784
+ function pqPublicKeyToCommitment(publicKey, options = {}) {
23785
+ return pqPublicKeyToCommitmentParts(publicKey, options).commitment;
23786
+ }
23787
+ function pqPublicKeyToCommitmentParts(publicKey, options = {}) {
23788
+ const witnessScript = normalizeWitnessScript(options.witnessScript);
23789
+ const authDescriptor = pqPublicKeyToAuthDescriptor(publicKey);
23790
+ const witnessScriptHash = sha256Hash(witnessScript);
23791
+ const commitment = taggedHash(AUTHSCRIPT_TAG, concatBytes(Uint8Array.from([AUTHSCRIPT_VERSION]), authDescriptor, witnessScriptHash));
23792
+ return {
23793
+ authDescriptor,
23794
+ authType: PQ_AUTH_TYPE,
23795
+ commitment,
23796
+ witnessScript,
23797
+ };
23798
+ }
23799
+ function pqPublicKeyToAddressBytes(publicKey, network, options = {}) {
23800
+ return bech32mEncode(network.hrp, network.witnessVersion, pqPublicKeyToCommitment(publicKey, options));
23771
23801
  }
23772
23802
  function normalizePublicKey(input) {
23773
23803
  return ensureBytes(input);
@@ -23929,8 +23959,8 @@ const pqNetworks = {
23929
23959
  hrp: "tnq",
23930
23960
  witnessVersion: 1,
23931
23961
  purpose: 100,
23932
- coinType: 1900,
23933
- changeIndex: 1,
23962
+ coinType: 1,
23963
+ changeIndex: 0,
23934
23964
  bip32: { private: 70615956, public: 70617039 },
23935
23965
  },
23936
23966
  };
@@ -24027,7 +24057,7 @@ function getPQHDKey(network, mnemonic, passphrase = "") {
24027
24057
  const seed = mnemonicToSeedBytes(mnemonicToSeedSync, mnemonic, passphrase);
24028
24058
  return HDKey.fromMasterSeed(seed, chain.bip32);
24029
24059
  }
24030
- function getPQAddressByPath(network, hdKey, path) {
24060
+ function getPQAddressByPath(network, hdKey, path, options = {}) {
24031
24061
  const chain = getPQNetwork(network);
24032
24062
  const derived = hdKey.derive(path);
24033
24063
  if (!derived.privateKey) {
@@ -24035,30 +24065,50 @@ function getPQAddressByPath(network, hdKey, path) {
24035
24065
  }
24036
24066
  const seed32 = Uint8Array.from(derived.privateKey);
24037
24067
  const { publicKey, secretKey } = ml_dsa44.keygen(seed32);
24068
+ const authScript = pqPublicKeyToCommitmentParts(publicKey, options);
24038
24069
  return {
24039
- address: pqPublicKeyToAddressBytes(publicKey, chain),
24070
+ address: pqPublicKeyToAddressBytes(publicKey, chain, options),
24071
+ authType: authScript.authType,
24072
+ authDescriptor: bytesToHex(authScript.authDescriptor),
24073
+ commitment: bytesToHex(authScript.commitment),
24040
24074
  path,
24041
24075
  publicKey: bytesToHex(publicKey),
24042
24076
  privateKey: bytesToHex(secretKey),
24043
24077
  seedKey: bytesToHex(seed32),
24078
+ witnessScript: bytesToHex(authScript.witnessScript),
24044
24079
  };
24045
24080
  }
24046
- function getPQAddress(network, mnemonic, account, index, passphrase = "") {
24081
+ function getPQAddress(network, mnemonic, account, index, passphrase = "", options = {}) {
24047
24082
  const chain = getPQNetwork(network);
24048
24083
  const hdKey = getPQHDKey(network, mnemonic, passphrase);
24049
24084
  const path = `m/${chain.purpose}'/${chain.coinType}'/${account}'/${chain.changeIndex}/${index}`;
24050
- return getPQAddressByPath(network, hdKey, path);
24085
+ return getPQAddressByPath(network, hdKey, path, options);
24086
+ }
24087
+ function pqPublicKeyToAddress(network, publicKey, options = {}) {
24088
+ const keyBytes = ensureBytes(publicKey);
24089
+ if (keyBytes.length !== 1312) {
24090
+ throw new Error("ML-DSA-44 public key must be 1312 bytes");
24091
+ }
24092
+ normalizeWitnessScript(options.witnessScript);
24093
+ return pqPublicKeyToAddressBytes(keyBytes, getPQNetwork(network), options);
24094
+ }
24095
+ function pqPublicKeyToCommitmentHex(publicKey, options = {}) {
24096
+ const keyBytes = ensureBytes(publicKey);
24097
+ if (keyBytes.length !== 1312) {
24098
+ throw new Error("ML-DSA-44 public key must be 1312 bytes");
24099
+ }
24100
+ return bytesToHex(pqPublicKeyToCommitment(keyBytes, options));
24051
24101
  }
24052
- function pqPublicKeyToAddress(network, publicKey) {
24102
+ function pqPublicKeyToAuthDescriptorHex(publicKey) {
24053
24103
  const keyBytes = ensureBytes(publicKey);
24054
24104
  if (keyBytes.length !== 1312) {
24055
24105
  throw new Error("ML-DSA-44 public key must be 1312 bytes");
24056
24106
  }
24057
- return pqPublicKeyToAddressBytes(keyBytes, getPQNetwork(network));
24107
+ return bytesToHex(pqPublicKeyToAuthDescriptor(keyBytes));
24058
24108
  }
24059
- function generatePQAddressObject(network = "xna-pq", passphrase = "") {
24109
+ function generatePQAddressObject(network = "xna-pq", passphrase = "", options = {}) {
24060
24110
  const mnemonic = generateMnemonic();
24061
- const addressObj = getPQAddress(network, mnemonic, 0, 0, passphrase);
24111
+ const addressObj = getPQAddress(network, mnemonic, 0, 0, passphrase, options);
24062
24112
  return {
24063
24113
  ...addressObj,
24064
24114
  mnemonic,
@@ -24081,6 +24131,8 @@ const NeuraiKey = {
24081
24131
  getPQAddressByPath,
24082
24132
  getPQHDKey,
24083
24133
  pqPublicKeyToAddress,
24134
+ pqPublicKeyToAuthDescriptorHex,
24135
+ pqPublicKeyToCommitmentHex,
24084
24136
  generatePQAddressObject,
24085
24137
  };
24086
24138
 
@@ -24101,5 +24153,7 @@ exports.getPQHDKey = getPQHDKey;
24101
24153
  exports.getPubkeyByWIF = getPubkeyByWIF;
24102
24154
  exports.isMnemonicValid = isMnemonicValid;
24103
24155
  exports.pqPublicKeyToAddress = pqPublicKeyToAddress;
24156
+ exports.pqPublicKeyToAuthDescriptorHex = pqPublicKeyToAuthDescriptorHex;
24157
+ exports.pqPublicKeyToCommitmentHex = pqPublicKeyToCommitmentHex;
24104
24158
  exports.publicKeyToAddress = publicKeyToAddress;
24105
24159
  //# sourceMappingURL=index.cjs.map