@lifi/sdk-provider-zcash 0.0.0 → 4.0.1

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/CHANGELOG.md ADDED
@@ -0,0 +1,19 @@
1
+ # @lifi/sdk-provider-zcash
2
+
3
+ ## 4.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [[`5dc9cc5`](https://github.com/lifinance/sdk/commit/5dc9cc5f52b0308962b822cfacd766dbf27bab3c)]:
8
+ - @lifi/sdk@4.12.0
9
+
10
+ ## 4.0.0
11
+
12
+ ### Minor Changes
13
+
14
+ - [#492](https://github.com/lifinance/sdk/pull/492) [`48af3ae`](https://github.com/lifinance/sdk/commit/48af3aeb250299ef9d2ff9eb79a38a6b8f52d409) Thanks [@chybisov](https://github.com/chybisov)! - New package. `ZcashProvider()` validates receivers on ZEC, a destination-only chain: transparent `t1` and `t3` addresses with a valid checksum, and unified `u1` addresses that decode under ZIP 316 and carry an Orchard receiver, the ones the API pays to a shielded balance. Sapling, TEX and testnet addresses, and a unified address without an Orchard receiver, are refused. It serves `ChainId.ZEC` beside `BitcoinProvider`, leaves balances unknown and cannot execute a step.
15
+
16
+ ### Patch Changes
17
+
18
+ - Updated dependencies [[`bb964c0`](https://github.com/lifinance/sdk/commit/bb964c03d8ea1770303ab1bba803373a80e6388f), [`48af3ae`](https://github.com/lifinance/sdk/commit/48af3aeb250299ef9d2ff9eb79a38a6b8f52d409), [`14f4ecc`](https://github.com/lifinance/sdk/commit/14f4ecc2714799449d57b61ff06b040df505184e)]:
19
+ - @lifi/sdk@4.11.0
package/README.md CHANGED
@@ -88,7 +88,7 @@ xBull, or Lobstr, we recommend
88
88
  pnpm add @lifi/sdk-provider-zcash
89
89
  ```
90
90
 
91
- ZEC is a destination-only chain: `ZcashProvider` validates transparent `t1` and `t3` receivers and needs no wallet. Register it after `BitcoinProvider`; it serves ZEC only.
91
+ ZEC is a destination-only chain: `ZcashProvider` validates transparent `t1` and `t3` receivers, and unified `u1` receivers that carry an Orchard receiver, and needs no wallet. Register it after `BitcoinProvider`; it serves ZEC only.
92
92
 
93
93
  ## Architecture
94
94
 
@@ -0,0 +1,13 @@
1
+ //#region src/utils/unifiedAddress.d.ts
2
+ /** The inverse of F4Jumble, the Feistel jumble ZIP 316 applies before Bech32m. */
3
+ export declare const f4JumbleInverse: (jumbled: Uint8Array) => Uint8Array;
4
+ /**
5
+ * Whether `address` is a mainnet unified address (`u1`) that the API pays to a
6
+ * shielded balance. It must decode — Bech32m checksum, F4Jumble, padding —
7
+ * into P2PKH, P2SH, Sapling and Orchard receivers only, each at its length, in
8
+ * ascending typecode order, with at most one transparent receiver, and one of
9
+ * them must be Orchard: without it a bridge pays the transparent receiver.
10
+ */
11
+ export declare const isOrchardUnifiedAddress: (address: string) => boolean;
12
+ //#endregion
13
+ //# sourceMappingURL=unifiedAddress.d.ts.map
@@ -0,0 +1,100 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let _scure_base = require("@scure/base");
3
+ let _noble_hashes_blake2b = require("@noble/hashes/blake2b");
4
+ //#region src/utils/unifiedAddress.ts
5
+ const ORCHARD = 3;
6
+ /** Byte length of each receiver typecode the API pays to (ZIP 316). */
7
+ const RECEIVER_LENGTHS = /* @__PURE__ */ new Map([
8
+ [0, 20],
9
+ [1, 20],
10
+ [2, 43],
11
+ [ORCHARD, 43]
12
+ ]);
13
+ const BECH32M_LIMIT = 4096;
14
+ const MIN_JUMBLED_LENGTH = 48;
15
+ const PADDING = Uint8Array.from({ length: 16 }, (_, k) => k === 0 ? 117 : 0);
16
+ const BLAKE2B_MAX_LENGTH = 64;
17
+ const textEncoder = new TextEncoder();
18
+ /** 16-byte BLAKE2b personalization: `UA_F4Jumble_<tag>`, then `i` and `j` as LE u16. */
19
+ const personalization = (tag, i, j = 0) => {
20
+ const bytes = /* @__PURE__ */ new Uint8Array(16);
21
+ bytes.set(textEncoder.encode(`UA_F4Jumble_${tag}`));
22
+ bytes[13] = i;
23
+ bytes[14] = j & 255;
24
+ bytes[15] = j >> 8;
25
+ return bytes;
26
+ };
27
+ const hashH = (i, input, length) => (0, _noble_hashes_blake2b.blake2b)(input, {
28
+ dkLen: length,
29
+ personalization: personalization("H", i)
30
+ });
31
+ const hashG = (i, input, length) => {
32
+ const output = new Uint8Array(length);
33
+ for (let j = 0; j * BLAKE2B_MAX_LENGTH < length; j++) {
34
+ const block = (0, _noble_hashes_blake2b.blake2b)(input, {
35
+ dkLen: BLAKE2B_MAX_LENGTH,
36
+ personalization: personalization("G", i, j)
37
+ });
38
+ const offset = j * BLAKE2B_MAX_LENGTH;
39
+ output.set(block.subarray(0, length - offset), offset);
40
+ }
41
+ return output;
42
+ };
43
+ const xor = (a, b) => a.map((byte, k) => byte ^ b[k]);
44
+ /** The inverse of F4Jumble, the Feistel jumble ZIP 316 applies before Bech32m. */
45
+ const f4JumbleInverse = (jumbled) => {
46
+ const leftLength = Math.min(BLAKE2B_MAX_LENGTH, Math.floor(jumbled.length / 2));
47
+ const rightLength = jumbled.length - leftLength;
48
+ const c = jumbled.subarray(0, leftLength);
49
+ const d = jumbled.subarray(leftLength);
50
+ const y = xor(c, hashH(1, d, leftLength));
51
+ const x = xor(d, hashG(1, y, rightLength));
52
+ const a = xor(y, hashH(0, x, leftLength));
53
+ const b = xor(x, hashG(0, a, rightLength));
54
+ const raw = new Uint8Array(jumbled.length);
55
+ raw.set(a);
56
+ raw.set(b, leftLength);
57
+ return raw;
58
+ };
59
+ /**
60
+ * The typecodes of a decoded receiver list, or `undefined` when a receiver is
61
+ * not one the API pays to, has another length, breaks the ascending order or
62
+ * overruns. Every accepted typecode and length fits one CompactSize byte, so a
63
+ * wider CompactSize can only encode a typecode the API refuses.
64
+ */
65
+ const receiverTypecodes = (body) => {
66
+ const typecodes = [];
67
+ for (let offset = 0; offset < body.length;) {
68
+ const typecode = body[offset];
69
+ const length = RECEIVER_LENGTHS.get(typecode);
70
+ const end = offset + 2 + (length ?? 0);
71
+ if (length === void 0 || body[offset + 1] !== length || end > body.length || typecode <= (typecodes.at(-1) ?? -1)) return;
72
+ typecodes.push(typecode);
73
+ offset = end;
74
+ }
75
+ return typecodes;
76
+ };
77
+ /**
78
+ * Whether `address` is a mainnet unified address (`u1`) that the API pays to a
79
+ * shielded balance. It must decode — Bech32m checksum, F4Jumble, padding —
80
+ * into P2PKH, P2SH, Sapling and Orchard receivers only, each at its length, in
81
+ * ascending typecode order, with at most one transparent receiver, and one of
82
+ * them must be Orchard: without it a bridge pays the transparent receiver.
83
+ */
84
+ const isOrchardUnifiedAddress = (address) => {
85
+ if (!address.startsWith("u1")) return false;
86
+ const decoded = _scure_base.bech32m.decodeUnsafe(address, BECH32M_LIMIT);
87
+ if (decoded?.prefix !== "u") return false;
88
+ const jumbled = _scure_base.bech32m.fromWordsUnsafe(decoded.words);
89
+ if (!jumbled || jumbled.length < MIN_JUMBLED_LENGTH) return false;
90
+ const raw = f4JumbleInverse(jumbled);
91
+ const paddingStart = raw.length - PADDING.length;
92
+ if (PADDING.some((byte, k) => raw[paddingStart + k] !== byte)) return false;
93
+ const typecodes = receiverTypecodes(raw.subarray(0, paddingStart)) ?? [];
94
+ return typecodes.includes(ORCHARD) && !(typecodes.includes(0) && typecodes.includes(1));
95
+ };
96
+ //#endregion
97
+ exports.f4JumbleInverse = f4JumbleInverse;
98
+ exports.isOrchardUnifiedAddress = isOrchardUnifiedAddress;
99
+
100
+ //# sourceMappingURL=unifiedAddress.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unifiedAddress.js","names":["blake2b","bech32m"],"sources":["../../../src/utils/unifiedAddress.ts"],"sourcesContent":["import { blake2b } from '@noble/hashes/blake2b'\nimport { bech32m } from '@scure/base'\n\nconst ORCHARD = 0x03\n/** Byte length of each receiver typecode the API pays to (ZIP 316). */\nconst RECEIVER_LENGTHS: ReadonlyMap<number, number> = new Map([\n [0x00, 20], // P2PKH\n [0x01, 20], // P2SH\n [0x02, 43], // Sapling\n [ORCHARD, 43],\n])\n// Bech32 caps decoding at 90 characters; a unified address runs to hundreds.\nconst BECH32M_LIMIT = 4096\nconst MIN_JUMBLED_LENGTH = 48\n// The decoded payload ends with the HRP `u`, zero-padded to 16 bytes.\nconst PADDING = Uint8Array.from({ length: 16 }, (_, k) => (k === 0 ? 0x75 : 0))\nconst BLAKE2B_MAX_LENGTH = 64\n\nconst textEncoder = new TextEncoder()\n\n/** 16-byte BLAKE2b personalization: `UA_F4Jumble_<tag>`, then `i` and `j` as LE u16. */\nconst personalization = (tag: 'H' | 'G', i: number, j = 0): Uint8Array => {\n const bytes = new Uint8Array(16)\n bytes.set(textEncoder.encode(`UA_F4Jumble_${tag}`))\n bytes[13] = i\n bytes[14] = j & 0xff\n bytes[15] = j >> 8\n return bytes\n}\n\nconst hashH = (i: number, input: Uint8Array, length: number): Uint8Array =>\n blake2b(input, { dkLen: length, personalization: personalization('H', i) })\n\nconst hashG = (i: number, input: Uint8Array, length: number): Uint8Array => {\n const output = new Uint8Array(length)\n for (let j = 0; j * BLAKE2B_MAX_LENGTH < length; j++) {\n const block = blake2b(input, {\n dkLen: BLAKE2B_MAX_LENGTH,\n personalization: personalization('G', i, j),\n })\n const offset = j * BLAKE2B_MAX_LENGTH\n output.set(block.subarray(0, length - offset), offset)\n }\n return output\n}\n\nconst xor = (a: Uint8Array, b: Uint8Array): Uint8Array =>\n a.map((byte, k) => byte ^ b[k])\n\n/** The inverse of F4Jumble, the Feistel jumble ZIP 316 applies before Bech32m. */\nexport const f4JumbleInverse = (jumbled: Uint8Array): Uint8Array => {\n const leftLength = Math.min(\n BLAKE2B_MAX_LENGTH,\n Math.floor(jumbled.length / 2)\n )\n const rightLength = jumbled.length - leftLength\n const c = jumbled.subarray(0, leftLength)\n const d = jumbled.subarray(leftLength)\n const y = xor(c, hashH(1, d, leftLength))\n const x = xor(d, hashG(1, y, rightLength))\n const a = xor(y, hashH(0, x, leftLength))\n const b = xor(x, hashG(0, a, rightLength))\n const raw = new Uint8Array(jumbled.length)\n raw.set(a)\n raw.set(b, leftLength)\n return raw\n}\n\n/**\n * The typecodes of a decoded receiver list, or `undefined` when a receiver is\n * not one the API pays to, has another length, breaks the ascending order or\n * overruns. Every accepted typecode and length fits one CompactSize byte, so a\n * wider CompactSize can only encode a typecode the API refuses.\n */\nconst receiverTypecodes = (body: Uint8Array): number[] | undefined => {\n const typecodes: number[] = []\n for (let offset = 0; offset < body.length; ) {\n const typecode = body[offset]\n const length = RECEIVER_LENGTHS.get(typecode)\n const end = offset + 2 + (length ?? 0)\n if (\n length === undefined ||\n body[offset + 1] !== length ||\n end > body.length ||\n typecode <= (typecodes.at(-1) ?? -1)\n ) {\n return undefined\n }\n typecodes.push(typecode)\n offset = end\n }\n return typecodes\n}\n\n/**\n * Whether `address` is a mainnet unified address (`u1`) that the API pays to a\n * shielded balance. It must decode — Bech32m checksum, F4Jumble, padding —\n * into P2PKH, P2SH, Sapling and Orchard receivers only, each at its length, in\n * ascending typecode order, with at most one transparent receiver, and one of\n * them must be Orchard: without it a bridge pays the transparent receiver.\n */\nexport const isOrchardUnifiedAddress = (address: string): boolean => {\n // The API takes the lowercase form only.\n if (!address.startsWith('u1')) {\n return false\n }\n const decoded = bech32m.decodeUnsafe(address, BECH32M_LIMIT)\n if (decoded?.prefix !== 'u') {\n return false\n }\n const jumbled = bech32m.fromWordsUnsafe(decoded.words)\n if (!jumbled || jumbled.length < MIN_JUMBLED_LENGTH) {\n return false\n }\n const raw = f4JumbleInverse(jumbled)\n const paddingStart = raw.length - PADDING.length\n if (PADDING.some((byte, k) => raw[paddingStart + k] !== byte)) {\n return false\n }\n const typecodes = receiverTypecodes(raw.subarray(0, paddingStart)) ?? []\n return (\n typecodes.includes(ORCHARD) &&\n !(typecodes.includes(0x00) && typecodes.includes(0x01))\n )\n}\n"],"mappings":";;;;AAGA,MAAM,UAAU;;AAEhB,MAAM,mCAAgD,IAAI,IAAI;CAC5D,CAAC,GAAM,EAAE;CACT,CAAC,GAAM,EA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CAAC;CACvE,OACE,UAAU,SAAS,OAAO,KAC1B,EAAE,UAAU,SAAS,CAAI,KAAK,UAAU,SAAS,CAAI;AAEzD"}
@@ -1,8 +1,9 @@
1
1
  //#region src/utils/zcashAddress.d.ts
2
2
  /**
3
- * Whether `address` is a mainnet transparent Zcash address (`t1` P2PKH or `t3`
4
- * P2SH) with a valid checksum. The API handles transparent receivers only, so
5
- * TEX, Sapling and unified addresses, and testnet forms, return `false`.
3
+ * Whether `address` is a mainnet Zcash address the API pays to: a transparent
4
+ * `t1` (P2PKH) or `t3` (P2SH) address with a valid checksum, or a unified `u1`
5
+ * address with an Orchard receiver. Sapling, TEX and testnet addresses, and a
6
+ * unified address without an Orchard receiver, return `false`.
6
7
  */
7
8
  export declare const isZcashAddress: (address: string) => boolean;
8
9
  //#endregion
@@ -1,13 +1,9 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_utils_unifiedAddress = require("./unifiedAddress.js");
2
3
  let _noble_hashes_sha2 = require("@noble/hashes/sha2");
3
4
  //#region src/utils/zcashAddress.ts
4
5
  const base58check = (0, require("@scure/base").createBase58check)(_noble_hashes_sha2.sha256);
5
- /**
6
- * Whether `address` is a mainnet transparent Zcash address (`t1` P2PKH or `t3`
7
- * P2SH) with a valid checksum. The API handles transparent receivers only, so
8
- * TEX, Sapling and unified addresses, and testnet forms, return `false`.
9
- */
10
- const isZcashAddress = (address) => {
6
+ const isTransparentZcashAddress = (address) => {
11
7
  let payload;
12
8
  try {
13
9
  payload = base58check.decode(address);
@@ -16,6 +12,13 @@ const isZcashAddress = (address) => {
16
12
  }
17
13
  return payload.length === 22 && payload[0] === 28 && (payload[1] === 184 || payload[1] === 189);
18
14
  };
15
+ /**
16
+ * Whether `address` is a mainnet Zcash address the API pays to: a transparent
17
+ * `t1` (P2PKH) or `t3` (P2SH) address with a valid checksum, or a unified `u1`
18
+ * address with an Orchard receiver. Sapling, TEX and testnet addresses, and a
19
+ * unified address without an Orchard receiver, return `false`.
20
+ */
21
+ const isZcashAddress = (address) => isTransparentZcashAddress(address) || require_utils_unifiedAddress.isOrchardUnifiedAddress(address);
19
22
  //#endregion
20
23
  exports.isZcashAddress = isZcashAddress;
21
24
 
@@ -1 +1 @@
1
- {"version":3,"file":"zcashAddress.js","names":["createBase58check","sha256"],"sources":["../../../src/utils/zcashAddress.ts"],"sourcesContent":["import { sha256 } from '@noble/hashes/sha2'\nimport { createBase58check } from '@scure/base'\n\nconst base58check = createBase58check(sha256)\n\n/**\n * Whether `address` is a mainnet transparent Zcash address (`t1` P2PKH or `t3`\n * P2SH) with a valid checksum. The API handles transparent receivers only, so\n * TEX, Sapling and unified addresses, and testnet forms, return `false`.\n */\nexport const isZcashAddress = (address: string): boolean => {\n let payload: Uint8Array\n try {\n payload = base58check.decode(address)\n } catch {\n return false\n }\n // A 2-byte version prefix (0x1CB8 or 0x1CBD) and a 20-byte hash.\n return (\n payload.length === 22 &&\n payload[0] === 0x1c &&\n (payload[1] === 0xb8 || payload[1] === 0xbd)\n )\n}\n"],"mappings":";;;AAGA,MAAM,eAAA,wBAAcA,CAAAA,CAAAA,kBAAAA,CAAkBC,mBAAAA,MAAM;;;;;;AAO5C,MAAa,kBAAkB,YAA6B;CAC1D,IAAI;CACJ,IAAI;EACF,UAAU,YAAY,OAAO,OAAO;CACtC,QAAQ;EACN,OAAO;CACT;CAEA,OACE,QAAQ,WAAW,MACnB,QAAQ,OAAO,OACd,QAAQ,OAAO,OAAQ,QAAQ,OAAO;AAE3C"}
1
+ {"version":3,"file":"zcashAddress.js","names":["createBase58check","sha256","isOrchardUnifiedAddress"],"sources":["../../../src/utils/zcashAddress.ts"],"sourcesContent":["import { sha256 } from '@noble/hashes/sha2'\nimport { createBase58check } from '@scure/base'\nimport { isOrchardUnifiedAddress } from './unifiedAddress.js'\n\nconst base58check = createBase58check(sha256)\n\nconst isTransparentZcashAddress = (address: string): boolean => {\n let payload: Uint8Array\n try {\n payload = base58check.decode(address)\n } catch {\n return false\n }\n // A 2-byte version prefix (0x1CB8 or 0x1CBD) and a 20-byte hash.\n return (\n payload.length === 22 &&\n payload[0] === 0x1c &&\n (payload[1] === 0xb8 || payload[1] === 0xbd)\n )\n}\n\n/**\n * Whether `address` is a mainnet Zcash address the API pays to: a transparent\n * `t1` (P2PKH) or `t3` (P2SH) address with a valid checksum, or a unified `u1`\n * address with an Orchard receiver. Sapling, TEX and testnet addresses, and a\n * unified address without an Orchard receiver, return `false`.\n */\nexport const isZcashAddress = (address: string): boolean =>\n isTransparentZcashAddress(address) || isOrchardUnifiedAddress(address)\n"],"mappings":";;;;AAIA,MAAM,eAAA,wBAAcA,CAAAA,CAAAA,kBAAAA,CAAkBC,mBAAAA,MAAM;AAE5C,MAAM,6BAA6B,YAA6B;CAC9D,IAAI;CACJ,IAAI;EACF,UAAU,YAAY,OAAO,OAAO;CACtC,QAAQ;EACN,OAAO;CACT;CAEA,OACE,QAAQ,WAAW,MACnB,QAAQ,OAAO,OACd,QAAQ,OAAO,OAAQ,QAAQ,OAAO;AAE3C;;;;;;;AAQA,MAAa,kBAAkB,YAC7B,0BAA0B,OAAO,KAAKC,6BAAAA,wBAAwB,OAAO"}
@@ -1,5 +1,5 @@
1
1
  //#region src/version.d.ts
2
2
  export declare const name = "@lifi/sdk-provider-zcash";
3
- export declare const version = "0.0.0";
3
+ export declare const version = "4.0.1";
4
4
  //#endregion
5
5
  //# sourceMappingURL=version.d.ts.map
@@ -1,7 +1,7 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  //#region src/version.ts
3
3
  const name = "@lifi/sdk-provider-zcash";
4
- const version = "0.0.0";
4
+ const version = "4.0.1";
5
5
  //#endregion
6
6
  exports.name = name;
7
7
  exports.version = version;
@@ -1 +1 @@
1
- {"version":3,"file":"version.js","names":[],"sources":["../../src/version.ts"],"sourcesContent":["export const name = '@lifi/sdk-provider-zcash'\nexport const version = '0.0.0'\n"],"mappings":";;AAAA,MAAa,OAAO;AACpB,MAAa,UAAU"}
1
+ {"version":3,"file":"version.js","names":[],"sources":["../../src/version.ts"],"sourcesContent":["export const name = '@lifi/sdk-provider-zcash'\nexport const version = '4.0.1'\n"],"mappings":";;AAAA,MAAa,OAAO;AACpB,MAAa,UAAU"}
@@ -0,0 +1,13 @@
1
+ //#region src/utils/unifiedAddress.d.ts
2
+ /** The inverse of F4Jumble, the Feistel jumble ZIP 316 applies before Bech32m. */
3
+ export declare const f4JumbleInverse: (jumbled: Uint8Array) => Uint8Array;
4
+ /**
5
+ * Whether `address` is a mainnet unified address (`u1`) that the API pays to a
6
+ * shielded balance. It must decode — Bech32m checksum, F4Jumble, padding —
7
+ * into P2PKH, P2SH, Sapling and Orchard receivers only, each at its length, in
8
+ * ascending typecode order, with at most one transparent receiver, and one of
9
+ * them must be Orchard: without it a bridge pays the transparent receiver.
10
+ */
11
+ export declare const isOrchardUnifiedAddress: (address: string) => boolean;
12
+ //#endregion
13
+ //# sourceMappingURL=unifiedAddress.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unifiedAddress.d.ts","names":[],"sources":["../../../src/utils/unifiedAddress.ts"],"mappings":";;qBAkDa,kBAAmB,SAAS,eAAa;;;;;;;;qBAmDzC,0BAA2B"}
@@ -0,0 +1,98 @@
1
+ import { bech32m } from "@scure/base";
2
+ import { blake2b } from "@noble/hashes/blake2b";
3
+ //#region src/utils/unifiedAddress.ts
4
+ const ORCHARD = 3;
5
+ /** Byte length of each receiver typecode the API pays to (ZIP 316). */
6
+ const RECEIVER_LENGTHS = /* @__PURE__ */ new Map([
7
+ [0, 20],
8
+ [1, 20],
9
+ [2, 43],
10
+ [ORCHARD, 43]
11
+ ]);
12
+ const BECH32M_LIMIT = 4096;
13
+ const MIN_JUMBLED_LENGTH = 48;
14
+ const PADDING = Uint8Array.from({ length: 16 }, (_, k) => k === 0 ? 117 : 0);
15
+ const BLAKE2B_MAX_LENGTH = 64;
16
+ const textEncoder = new TextEncoder();
17
+ /** 16-byte BLAKE2b personalization: `UA_F4Jumble_<tag>`, then `i` and `j` as LE u16. */
18
+ const personalization = (tag, i, j = 0) => {
19
+ const bytes = /* @__PURE__ */ new Uint8Array(16);
20
+ bytes.set(textEncoder.encode(`UA_F4Jumble_${tag}`));
21
+ bytes[13] = i;
22
+ bytes[14] = j & 255;
23
+ bytes[15] = j >> 8;
24
+ return bytes;
25
+ };
26
+ const hashH = (i, input, length) => blake2b(input, {
27
+ dkLen: length,
28
+ personalization: personalization("H", i)
29
+ });
30
+ const hashG = (i, input, length) => {
31
+ const output = new Uint8Array(length);
32
+ for (let j = 0; j * BLAKE2B_MAX_LENGTH < length; j++) {
33
+ const block = blake2b(input, {
34
+ dkLen: BLAKE2B_MAX_LENGTH,
35
+ personalization: personalization("G", i, j)
36
+ });
37
+ const offset = j * BLAKE2B_MAX_LENGTH;
38
+ output.set(block.subarray(0, length - offset), offset);
39
+ }
40
+ return output;
41
+ };
42
+ const xor = (a, b) => a.map((byte, k) => byte ^ b[k]);
43
+ /** The inverse of F4Jumble, the Feistel jumble ZIP 316 applies before Bech32m. */
44
+ const f4JumbleInverse = (jumbled) => {
45
+ const leftLength = Math.min(BLAKE2B_MAX_LENGTH, Math.floor(jumbled.length / 2));
46
+ const rightLength = jumbled.length - leftLength;
47
+ const c = jumbled.subarray(0, leftLength);
48
+ const d = jumbled.subarray(leftLength);
49
+ const y = xor(c, hashH(1, d, leftLength));
50
+ const x = xor(d, hashG(1, y, rightLength));
51
+ const a = xor(y, hashH(0, x, leftLength));
52
+ const b = xor(x, hashG(0, a, rightLength));
53
+ const raw = new Uint8Array(jumbled.length);
54
+ raw.set(a);
55
+ raw.set(b, leftLength);
56
+ return raw;
57
+ };
58
+ /**
59
+ * The typecodes of a decoded receiver list, or `undefined` when a receiver is
60
+ * not one the API pays to, has another length, breaks the ascending order or
61
+ * overruns. Every accepted typecode and length fits one CompactSize byte, so a
62
+ * wider CompactSize can only encode a typecode the API refuses.
63
+ */
64
+ const receiverTypecodes = (body) => {
65
+ const typecodes = [];
66
+ for (let offset = 0; offset < body.length;) {
67
+ const typecode = body[offset];
68
+ const length = RECEIVER_LENGTHS.get(typecode);
69
+ const end = offset + 2 + (length ?? 0);
70
+ if (length === void 0 || body[offset + 1] !== length || end > body.length || typecode <= (typecodes.at(-1) ?? -1)) return;
71
+ typecodes.push(typecode);
72
+ offset = end;
73
+ }
74
+ return typecodes;
75
+ };
76
+ /**
77
+ * Whether `address` is a mainnet unified address (`u1`) that the API pays to a
78
+ * shielded balance. It must decode — Bech32m checksum, F4Jumble, padding —
79
+ * into P2PKH, P2SH, Sapling and Orchard receivers only, each at its length, in
80
+ * ascending typecode order, with at most one transparent receiver, and one of
81
+ * them must be Orchard: without it a bridge pays the transparent receiver.
82
+ */
83
+ const isOrchardUnifiedAddress = (address) => {
84
+ if (!address.startsWith("u1")) return false;
85
+ const decoded = bech32m.decodeUnsafe(address, BECH32M_LIMIT);
86
+ if (decoded?.prefix !== "u") return false;
87
+ const jumbled = bech32m.fromWordsUnsafe(decoded.words);
88
+ if (!jumbled || jumbled.length < MIN_JUMBLED_LENGTH) return false;
89
+ const raw = f4JumbleInverse(jumbled);
90
+ const paddingStart = raw.length - PADDING.length;
91
+ if (PADDING.some((byte, k) => raw[paddingStart + k] !== byte)) return false;
92
+ const typecodes = receiverTypecodes(raw.subarray(0, paddingStart)) ?? [];
93
+ return typecodes.includes(ORCHARD) && !(typecodes.includes(0) && typecodes.includes(1));
94
+ };
95
+ //#endregion
96
+ export { f4JumbleInverse, isOrchardUnifiedAddress };
97
+
98
+ //# sourceMappingURL=unifiedAddress.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unifiedAddress.js","names":[],"sources":["../../../src/utils/unifiedAddress.ts"],"sourcesContent":["import { blake2b } from '@noble/hashes/blake2b'\nimport { bech32m } from '@scure/base'\n\nconst ORCHARD = 0x03\n/** Byte length of each receiver typecode the API pays to (ZIP 316). */\nconst RECEIVER_LENGTHS: ReadonlyMap<number, number> = new Map([\n [0x00, 20], // P2PKH\n [0x01, 20], // P2SH\n [0x02, 43], // Sapling\n [ORCHARD, 43],\n])\n// Bech32 caps decoding at 90 characters; a unified address runs to hundreds.\nconst BECH32M_LIMIT = 4096\nconst MIN_JUMBLED_LENGTH = 48\n// The decoded payload ends with the HRP `u`, zero-padded to 16 bytes.\nconst PADDING = Uint8Array.from({ length: 16 }, (_, k) => (k === 0 ? 0x75 : 0))\nconst BLAKE2B_MAX_LENGTH = 64\n\nconst textEncoder = new TextEncoder()\n\n/** 16-byte BLAKE2b personalization: `UA_F4Jumble_<tag>`, then `i` and `j` as LE u16. */\nconst personalization = (tag: 'H' | 'G', i: number, j = 0): Uint8Array => {\n const bytes = new Uint8Array(16)\n bytes.set(textEncoder.encode(`UA_F4Jumble_${tag}`))\n bytes[13] = i\n bytes[14] = j & 0xff\n bytes[15] = j >> 8\n return bytes\n}\n\nconst hashH = (i: number, input: Uint8Array, length: number): Uint8Array =>\n blake2b(input, { dkLen: length, personalization: personalization('H', i) })\n\nconst hashG = (i: number, input: Uint8Array, length: number): Uint8Array => {\n const output = new Uint8Array(length)\n for (let j = 0; j * BLAKE2B_MAX_LENGTH < length; j++) {\n const block = blake2b(input, {\n dkLen: BLAKE2B_MAX_LENGTH,\n personalization: personalization('G', i, j),\n })\n const offset = j * BLAKE2B_MAX_LENGTH\n output.set(block.subarray(0, length - offset), offset)\n }\n return output\n}\n\nconst xor = (a: Uint8Array, b: Uint8Array): Uint8Array =>\n a.map((byte, k) => byte ^ b[k])\n\n/** The inverse of F4Jumble, the Feistel jumble ZIP 316 applies before Bech32m. */\nexport const f4JumbleInverse = (jumbled: Uint8Array): Uint8Array => {\n const leftLength = Math.min(\n BLAKE2B_MAX_LENGTH,\n Math.floor(jumbled.length / 2)\n )\n const rightLength = jumbled.length - leftLength\n const c = jumbled.subarray(0, leftLength)\n const d = jumbled.subarray(leftLength)\n const y = xor(c, hashH(1, d, leftLength))\n const x = xor(d, hashG(1, y, rightLength))\n const a = xor(y, hashH(0, x, leftLength))\n const b = xor(x, hashG(0, a, rightLength))\n const raw = new Uint8Array(jumbled.length)\n raw.set(a)\n raw.set(b, leftLength)\n return raw\n}\n\n/**\n * The typecodes of a decoded receiver list, or `undefined` when a receiver is\n * not one the API pays to, has another length, breaks the ascending order or\n * overruns. Every accepted typecode and length fits one CompactSize byte, so a\n * wider CompactSize can only encode a typecode the API refuses.\n */\nconst receiverTypecodes = (body: Uint8Array): number[] | undefined => {\n const typecodes: number[] = []\n for (let offset = 0; offset < body.length; ) {\n const typecode = body[offset]\n const length = RECEIVER_LENGTHS.get(typecode)\n const end = offset + 2 + (length ?? 0)\n if (\n length === undefined ||\n body[offset + 1] !== length ||\n end > body.length ||\n typecode <= (typecodes.at(-1) ?? -1)\n ) {\n return undefined\n }\n typecodes.push(typecode)\n offset = end\n }\n return typecodes\n}\n\n/**\n * Whether `address` is a mainnet unified address (`u1`) that the API pays to a\n * shielded balance. It must decode — Bech32m checksum, F4Jumble, padding —\n * into P2PKH, P2SH, Sapling and Orchard receivers only, each at its length, in\n * ascending typecode order, with at most one transparent receiver, and one of\n * them must be Orchard: without it a bridge pays the transparent receiver.\n */\nexport const isOrchardUnifiedAddress = (address: string): boolean => {\n // The API takes the lowercase form only.\n if (!address.startsWith('u1')) {\n return false\n }\n const decoded = bech32m.decodeUnsafe(address, BECH32M_LIMIT)\n if (decoded?.prefix !== 'u') {\n return false\n }\n const jumbled = bech32m.fromWordsUnsafe(decoded.words)\n if (!jumbled || jumbled.length < MIN_JUMBLED_LENGTH) {\n return false\n }\n const raw = f4JumbleInverse(jumbled)\n const paddingStart = raw.length - PADDING.length\n if (PADDING.some((byte, k) => raw[paddingStart + k] !== byte)) {\n return false\n }\n const typecodes = receiverTypecodes(raw.subarray(0, paddingStart)) ?? []\n return (\n typecodes.includes(ORCHARD) &&\n !(typecodes.includes(0x00) && typecodes.includes(0x01))\n )\n}\n"],"mappings":";;;AAGA,MAAM,UAAU;;AAEhB,MAAM,mCAAgD,IAAI,IAAI;CA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AU,SAAS,OAAO,KAC1B,EAAE,UAAU,SAAS,CAAI,KAAK,UAAU,SAAS,CAAI;AAEzD"}
@@ -1,8 +1,9 @@
1
1
  //#region src/utils/zcashAddress.d.ts
2
2
  /**
3
- * Whether `address` is a mainnet transparent Zcash address (`t1` P2PKH or `t3`
4
- * P2SH) with a valid checksum. The API handles transparent receivers only, so
5
- * TEX, Sapling and unified addresses, and testnet forms, return `false`.
3
+ * Whether `address` is a mainnet Zcash address the API pays to: a transparent
4
+ * `t1` (P2PKH) or `t3` (P2SH) address with a valid checksum, or a unified `u1`
5
+ * address with an Orchard receiver. Sapling, TEX and testnet addresses, and a
6
+ * unified address without an Orchard receiver, return `false`.
6
7
  */
7
8
  export declare const isZcashAddress: (address: string) => boolean;
8
9
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"zcashAddress.d.ts","names":[],"sources":["../../../src/utils/zcashAddress.ts"],"mappings":";;;;;;qBAUa,iBAAkB"}
1
+ {"version":3,"file":"zcashAddress.d.ts","names":[],"sources":["../../../src/utils/zcashAddress.ts"],"mappings":";;;;;;;qBA2Ba,iBAAkB"}
@@ -1,13 +1,9 @@
1
+ import { isOrchardUnifiedAddress } from "./unifiedAddress.js";
1
2
  import { sha256 } from "@noble/hashes/sha2";
2
3
  import { createBase58check } from "@scure/base";
3
4
  //#region src/utils/zcashAddress.ts
4
5
  const base58check = createBase58check(sha256);
5
- /**
6
- * Whether `address` is a mainnet transparent Zcash address (`t1` P2PKH or `t3`
7
- * P2SH) with a valid checksum. The API handles transparent receivers only, so
8
- * TEX, Sapling and unified addresses, and testnet forms, return `false`.
9
- */
10
- const isZcashAddress = (address) => {
6
+ const isTransparentZcashAddress = (address) => {
11
7
  let payload;
12
8
  try {
13
9
  payload = base58check.decode(address);
@@ -16,6 +12,13 @@ const isZcashAddress = (address) => {
16
12
  }
17
13
  return payload.length === 22 && payload[0] === 28 && (payload[1] === 184 || payload[1] === 189);
18
14
  };
15
+ /**
16
+ * Whether `address` is a mainnet Zcash address the API pays to: a transparent
17
+ * `t1` (P2PKH) or `t3` (P2SH) address with a valid checksum, or a unified `u1`
18
+ * address with an Orchard receiver. Sapling, TEX and testnet addresses, and a
19
+ * unified address without an Orchard receiver, return `false`.
20
+ */
21
+ const isZcashAddress = (address) => isTransparentZcashAddress(address) || isOrchardUnifiedAddress(address);
19
22
  //#endregion
20
23
  export { isZcashAddress };
21
24
 
@@ -1 +1 @@
1
- {"version":3,"file":"zcashAddress.js","names":[],"sources":["../../../src/utils/zcashAddress.ts"],"sourcesContent":["import { sha256 } from '@noble/hashes/sha2'\nimport { createBase58check } from '@scure/base'\n\nconst base58check = createBase58check(sha256)\n\n/**\n * Whether `address` is a mainnet transparent Zcash address (`t1` P2PKH or `t3`\n * P2SH) with a valid checksum. The API handles transparent receivers only, so\n * TEX, Sapling and unified addresses, and testnet forms, return `false`.\n */\nexport const isZcashAddress = (address: string): boolean => {\n let payload: Uint8Array\n try {\n payload = base58check.decode(address)\n } catch {\n return false\n }\n // A 2-byte version prefix (0x1CB8 or 0x1CBD) and a 20-byte hash.\n return (\n payload.length === 22 &&\n payload[0] === 0x1c &&\n (payload[1] === 0xb8 || payload[1] === 0xbd)\n )\n}\n"],"mappings":";;;AAGA,MAAM,cAAc,kBAAkB,MAAM;;;;;;AAO5C,MAAa,kBAAkB,YAA6B;CAC1D,IAAI;CACJ,IAAI;EACF,UAAU,YAAY,OAAO,OAAO;CACtC,QAAQ;EACN,OAAO;CACT;CAEA,OACE,QAAQ,WAAW,MACnB,QAAQ,OAAO,OACd,QAAQ,OAAO,OAAQ,QAAQ,OAAO;AAE3C"}
1
+ {"version":3,"file":"zcashAddress.js","names":[],"sources":["../../../src/utils/zcashAddress.ts"],"sourcesContent":["import { sha256 } from '@noble/hashes/sha2'\nimport { createBase58check } from '@scure/base'\nimport { isOrchardUnifiedAddress } from './unifiedAddress.js'\n\nconst base58check = createBase58check(sha256)\n\nconst isTransparentZcashAddress = (address: string): boolean => {\n let payload: Uint8Array\n try {\n payload = base58check.decode(address)\n } catch {\n return false\n }\n // A 2-byte version prefix (0x1CB8 or 0x1CBD) and a 20-byte hash.\n return (\n payload.length === 22 &&\n payload[0] === 0x1c &&\n (payload[1] === 0xb8 || payload[1] === 0xbd)\n )\n}\n\n/**\n * Whether `address` is a mainnet Zcash address the API pays to: a transparent\n * `t1` (P2PKH) or `t3` (P2SH) address with a valid checksum, or a unified `u1`\n * address with an Orchard receiver. Sapling, TEX and testnet addresses, and a\n * unified address without an Orchard receiver, return `false`.\n */\nexport const isZcashAddress = (address: string): boolean =>\n isTransparentZcashAddress(address) || isOrchardUnifiedAddress(address)\n"],"mappings":";;;;AAIA,MAAM,cAAc,kBAAkB,MAAM;AAE5C,MAAM,6BAA6B,YAA6B;CAC9D,IAAI;CACJ,IAAI;EACF,UAAU,YAAY,OAAO,OAAO;CACtC,QAAQ;EACN,OAAO;CACT;CAEA,OACE,QAAQ,WAAW,MACnB,QAAQ,OAAO,OACd,QAAQ,OAAO,OAAQ,QAAQ,OAAO;AAE3C;;;;;;;AAQA,MAAa,kBAAkB,YAC7B,0BAA0B,OAAO,KAAK,wBAAwB,OAAO"}
@@ -1,5 +1,5 @@
1
1
  //#region src/version.d.ts
2
2
  export declare const name = "@lifi/sdk-provider-zcash";
3
- export declare const version = "0.0.0";
3
+ export declare const version = "4.0.1";
4
4
  //#endregion
5
5
  //# sourceMappingURL=version.d.ts.map
@@ -1,6 +1,6 @@
1
1
  //#region src/version.ts
2
2
  const name = "@lifi/sdk-provider-zcash";
3
- const version = "0.0.0";
3
+ const version = "4.0.1";
4
4
  //#endregion
5
5
  export { name, version };
6
6
 
@@ -1 +1 @@
1
- {"version":3,"file":"version.js","names":[],"sources":["../../src/version.ts"],"sourcesContent":["export const name = '@lifi/sdk-provider-zcash'\nexport const version = '0.0.0'\n"],"mappings":";AAAA,MAAa,OAAO;AACpB,MAAa,UAAU"}
1
+ {"version":3,"file":"version.js","names":[],"sources":["../../src/version.ts"],"sourcesContent":["export const name = '@lifi/sdk-provider-zcash'\nexport const version = '4.0.1'\n"],"mappings":";AAAA,MAAa,OAAO;AACpB,MAAa,UAAU"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lifi/sdk-provider-zcash",
3
- "version": "0.0.0",
3
+ "version": "4.0.1",
4
4
  "description": "LI.FI Zcash SDK Provider for Any-to-Any Cross-Chain-Swap",
5
5
  "homepage": "https://github.com/lifinance/sdk",
6
6
  "bugs": {
@@ -28,7 +28,7 @@
28
28
  "./package.json": "./package.json"
29
29
  },
30
30
  "dependencies": {
31
- "@lifi/sdk": "4.10.0",
31
+ "@lifi/sdk": "4.12.0",
32
32
  "@noble/hashes": "^1.8.0",
33
33
  "@scure/base": "^1.2.6"
34
34
  },
@@ -0,0 +1,125 @@
1
+ import { blake2b } from '@noble/hashes/blake2b'
2
+ import { bech32m } from '@scure/base'
3
+
4
+ const ORCHARD = 0x03
5
+ /** Byte length of each receiver typecode the API pays to (ZIP 316). */
6
+ const RECEIVER_LENGTHS: ReadonlyMap<number, number> = new Map([
7
+ [0x00, 20], // P2PKH
8
+ [0x01, 20], // P2SH
9
+ [0x02, 43], // Sapling
10
+ [ORCHARD, 43],
11
+ ])
12
+ // Bech32 caps decoding at 90 characters; a unified address runs to hundreds.
13
+ const BECH32M_LIMIT = 4096
14
+ const MIN_JUMBLED_LENGTH = 48
15
+ // The decoded payload ends with the HRP `u`, zero-padded to 16 bytes.
16
+ const PADDING = Uint8Array.from({ length: 16 }, (_, k) => (k === 0 ? 0x75 : 0))
17
+ const BLAKE2B_MAX_LENGTH = 64
18
+
19
+ const textEncoder = new TextEncoder()
20
+
21
+ /** 16-byte BLAKE2b personalization: `UA_F4Jumble_<tag>`, then `i` and `j` as LE u16. */
22
+ const personalization = (tag: 'H' | 'G', i: number, j = 0): Uint8Array => {
23
+ const bytes = new Uint8Array(16)
24
+ bytes.set(textEncoder.encode(`UA_F4Jumble_${tag}`))
25
+ bytes[13] = i
26
+ bytes[14] = j & 0xff
27
+ bytes[15] = j >> 8
28
+ return bytes
29
+ }
30
+
31
+ const hashH = (i: number, input: Uint8Array, length: number): Uint8Array =>
32
+ blake2b(input, { dkLen: length, personalization: personalization('H', i) })
33
+
34
+ const hashG = (i: number, input: Uint8Array, length: number): Uint8Array => {
35
+ const output = new Uint8Array(length)
36
+ for (let j = 0; j * BLAKE2B_MAX_LENGTH < length; j++) {
37
+ const block = blake2b(input, {
38
+ dkLen: BLAKE2B_MAX_LENGTH,
39
+ personalization: personalization('G', i, j),
40
+ })
41
+ const offset = j * BLAKE2B_MAX_LENGTH
42
+ output.set(block.subarray(0, length - offset), offset)
43
+ }
44
+ return output
45
+ }
46
+
47
+ const xor = (a: Uint8Array, b: Uint8Array): Uint8Array =>
48
+ a.map((byte, k) => byte ^ b[k])
49
+
50
+ /** The inverse of F4Jumble, the Feistel jumble ZIP 316 applies before Bech32m. */
51
+ export const f4JumbleInverse = (jumbled: Uint8Array): Uint8Array => {
52
+ const leftLength = Math.min(
53
+ BLAKE2B_MAX_LENGTH,
54
+ Math.floor(jumbled.length / 2)
55
+ )
56
+ const rightLength = jumbled.length - leftLength
57
+ const c = jumbled.subarray(0, leftLength)
58
+ const d = jumbled.subarray(leftLength)
59
+ const y = xor(c, hashH(1, d, leftLength))
60
+ const x = xor(d, hashG(1, y, rightLength))
61
+ const a = xor(y, hashH(0, x, leftLength))
62
+ const b = xor(x, hashG(0, a, rightLength))
63
+ const raw = new Uint8Array(jumbled.length)
64
+ raw.set(a)
65
+ raw.set(b, leftLength)
66
+ return raw
67
+ }
68
+
69
+ /**
70
+ * The typecodes of a decoded receiver list, or `undefined` when a receiver is
71
+ * not one the API pays to, has another length, breaks the ascending order or
72
+ * overruns. Every accepted typecode and length fits one CompactSize byte, so a
73
+ * wider CompactSize can only encode a typecode the API refuses.
74
+ */
75
+ const receiverTypecodes = (body: Uint8Array): number[] | undefined => {
76
+ const typecodes: number[] = []
77
+ for (let offset = 0; offset < body.length; ) {
78
+ const typecode = body[offset]
79
+ const length = RECEIVER_LENGTHS.get(typecode)
80
+ const end = offset + 2 + (length ?? 0)
81
+ if (
82
+ length === undefined ||
83
+ body[offset + 1] !== length ||
84
+ end > body.length ||
85
+ typecode <= (typecodes.at(-1) ?? -1)
86
+ ) {
87
+ return undefined
88
+ }
89
+ typecodes.push(typecode)
90
+ offset = end
91
+ }
92
+ return typecodes
93
+ }
94
+
95
+ /**
96
+ * Whether `address` is a mainnet unified address (`u1`) that the API pays to a
97
+ * shielded balance. It must decode — Bech32m checksum, F4Jumble, padding —
98
+ * into P2PKH, P2SH, Sapling and Orchard receivers only, each at its length, in
99
+ * ascending typecode order, with at most one transparent receiver, and one of
100
+ * them must be Orchard: without it a bridge pays the transparent receiver.
101
+ */
102
+ export const isOrchardUnifiedAddress = (address: string): boolean => {
103
+ // The API takes the lowercase form only.
104
+ if (!address.startsWith('u1')) {
105
+ return false
106
+ }
107
+ const decoded = bech32m.decodeUnsafe(address, BECH32M_LIMIT)
108
+ if (decoded?.prefix !== 'u') {
109
+ return false
110
+ }
111
+ const jumbled = bech32m.fromWordsUnsafe(decoded.words)
112
+ if (!jumbled || jumbled.length < MIN_JUMBLED_LENGTH) {
113
+ return false
114
+ }
115
+ const raw = f4JumbleInverse(jumbled)
116
+ const paddingStart = raw.length - PADDING.length
117
+ if (PADDING.some((byte, k) => raw[paddingStart + k] !== byte)) {
118
+ return false
119
+ }
120
+ const typecodes = receiverTypecodes(raw.subarray(0, paddingStart)) ?? []
121
+ return (
122
+ typecodes.includes(ORCHARD) &&
123
+ !(typecodes.includes(0x00) && typecodes.includes(0x01))
124
+ )
125
+ }
@@ -1,14 +1,10 @@
1
1
  import { sha256 } from '@noble/hashes/sha2'
2
2
  import { createBase58check } from '@scure/base'
3
+ import { isOrchardUnifiedAddress } from './unifiedAddress.js'
3
4
 
4
5
  const base58check = createBase58check(sha256)
5
6
 
6
- /**
7
- * Whether `address` is a mainnet transparent Zcash address (`t1` P2PKH or `t3`
8
- * P2SH) with a valid checksum. The API handles transparent receivers only, so
9
- * TEX, Sapling and unified addresses, and testnet forms, return `false`.
10
- */
11
- export const isZcashAddress = (address: string): boolean => {
7
+ const isTransparentZcashAddress = (address: string): boolean => {
12
8
  let payload: Uint8Array
13
9
  try {
14
10
  payload = base58check.decode(address)
@@ -22,3 +18,12 @@ export const isZcashAddress = (address: string): boolean => {
22
18
  (payload[1] === 0xb8 || payload[1] === 0xbd)
23
19
  )
24
20
  }
21
+
22
+ /**
23
+ * Whether `address` is a mainnet Zcash address the API pays to: a transparent
24
+ * `t1` (P2PKH) or `t3` (P2SH) address with a valid checksum, or a unified `u1`
25
+ * address with an Orchard receiver. Sapling, TEX and testnet addresses, and a
26
+ * unified address without an Orchard receiver, return `false`.
27
+ */
28
+ export const isZcashAddress = (address: string): boolean =>
29
+ isTransparentZcashAddress(address) || isOrchardUnifiedAddress(address)
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export const name = '@lifi/sdk-provider-zcash'
2
- export const version = '0.0.0'
2
+ export const version = '4.0.1'