@campnetwork/origin 1.3.0-alpha.8 → 1.3.0-alpha.9

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.
@@ -6972,17 +6972,61 @@ function base (ALPHABET) {
6972
6972
  var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
6973
6973
  var bs58 = base(ALPHABET);
6974
6974
 
6975
+ // RFC 4648 base32 alphabet (lowercase)
6976
+ const BASE32_ALPHABET = "abcdefghijklmnopqrstuvwxyz234567";
6977
+ function base32Decode(str) {
6978
+ const lookup = {};
6979
+ for (let i = 0; i < BASE32_ALPHABET.length; i++) {
6980
+ lookup[BASE32_ALPHABET[i]] = i;
6981
+ }
6982
+ let bits = 0;
6983
+ let value = 0;
6984
+ const result = [];
6985
+ for (const char of str.toLowerCase()) {
6986
+ if (!(char in lookup)) {
6987
+ throw new Error(`Invalid base32 character: ${char}`);
6988
+ }
6989
+ value = (value << 5) | lookup[char];
6990
+ bits += 5;
6991
+ if (bits >= 8) {
6992
+ bits -= 8;
6993
+ result.push((value >> bits) & 0xff);
6994
+ }
6995
+ }
6996
+ return new Uint8Array(result);
6997
+ }
6975
6998
  /**
6976
- * Encode CIDv0 to bytes32 by stripping the 0x1220 multihash prefix.
6977
- * Only works with CIDv0 (SHA-256 hash, starts with "Qm").
6999
+ * Encode a CID to bytes32 by extracting the 32-byte SHA-256 digest.
7000
+ * Supports both CIDv0 (starts with "Qm") and CIDv1 (starts with "bafy").
6978
7001
  */
6979
7002
  function encodeCidToBytes32(cid) {
6980
- const decoded = bs58.decode(cid);
6981
- if (decoded[0] !== 0x12 || decoded[1] !== 0x20 || decoded.length !== 34) {
6982
- throw new Error("Only CIDv0 with SHA-256 is supported");
7003
+ let digest;
7004
+ if (cid.startsWith("Qm")) {
7005
+ // CIDv0: base58-encoded multihash (0x1220 + 32-byte digest)
7006
+ const decoded = bs58.decode(cid);
7007
+ if (decoded[0] !== 0x12 || decoded[1] !== 0x20 || decoded.length !== 34) {
7008
+ throw new Error("Invalid CIDv0: expected SHA-256 multihash");
7009
+ }
7010
+ digest = decoded.slice(2);
7011
+ }
7012
+ else if (cid.startsWith("b")) {
7013
+ // CIDv1: multibase 'b' prefix + base32-encoded (version + codec + multihash)
7014
+ const decoded = base32Decode(cid.slice(1));
7015
+ // CIDv1 structure: version (1 byte) + codec (varint) + multihash
7016
+ // For dag-pb with sha2-256: 0x01 + 0x70 + 0x12 + 0x20 + 32-byte digest
7017
+ if (decoded[0] !== 0x01 ||
7018
+ decoded[1] !== 0x70 ||
7019
+ decoded[2] !== 0x12 ||
7020
+ decoded[3] !== 0x20 ||
7021
+ decoded.length !== 36) {
7022
+ throw new Error("Invalid CIDv1: expected version 1, dag-pb codec, SHA-256 hash");
7023
+ }
7024
+ digest = decoded.slice(4);
7025
+ }
7026
+ else {
7027
+ throw new Error("Unsupported CID format: must start with 'Qm' or 'b'");
6983
7028
  }
6984
- // Return only the 32-byte digest (skip 0x1220 prefix)
6985
- return `0x${Buffer.from(decoded.slice(2)).toString("hex")}`;
7029
+ return `0x${Buffer.from(digest).toString("hex")}`;
6986
7030
  }
6987
7031
 
6988
7032
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@campnetwork/origin",
3
- "version": "1.3.0-alpha.8",
3
+ "version": "1.3.0-alpha.9",
4
4
  "main": "dist/core.cjs",
5
5
  "exports": {
6
6
  ".": {