Repository navigation
Expand file tree
/
Copy pathZcashUnifiedAddress.ts
More file actions
75 lines (68 loc) · 2.63 KB
/
Copy pathZcashUnifiedAddress.ts
File metadata and controls
75 lines (68 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { ZcashUnifiedAddress as WasmZcashUnifiedAddress } from "../wasm/wasm_utxo.js";
import type { ZcashNetworkName } from "./ZcashBitGoPsbt.js";
/**
* A parsed ZIP-316 Unified Address.
*
* Decode once with {@link ZcashUnifiedAddress.parse}, then read each component
* through its accessor (returns `undefined` when the receiver is absent). Membership
* of another address is answered by {@link contains}.
*
* Ironwood reuses the Orchard receiver, so {@link orchardReceiver} is the shielded
* receiver used to construct an Ironwood output note.
*
* @example
* ```typescript
* const ua = ZcashUnifiedAddress.parse(uaString, "zec");
* const ironwood = ua.orchardReceiver; // 43 bytes, or undefined
* const script = ua.transparentScript; // scriptPubKey bytes, or undefined
* ua.contains(transparentAddress); // is it one of this UA's receivers?
* ```
*/
export class ZcashUnifiedAddress {
private constructor(private _wasm: WasmZcashUnifiedAddress) {}
/**
* Parse a Unified Address.
*
* @param address - The Bech32m unified address string
* @param network - Zcash network name ("zcash", "zcashTest", "zec", "tzec")
* @throws If the address is malformed or on the wrong network
*/
static parse(address: string, network: ZcashNetworkName): ZcashUnifiedAddress {
return new ZcashUnifiedAddress(WasmZcashUnifiedAddress.parse(address, network));
}
/**
* The Orchard (a.k.a. Ironwood) receiver — 43 raw bytes (11-byte diversifier +
* 32-byte pk_d) — or `undefined` if the UA has no Orchard receiver.
*/
get orchardReceiver(): Uint8Array | undefined {
return this._wasm.orchardReceiver;
}
/**
* The Sapling receiver — 43 raw bytes (diversifier + pk_d) — or `undefined`.
*/
get saplingReceiver(): Uint8Array | undefined {
return this._wasm.saplingReceiver;
}
/**
* The transparent receiver as scriptPubKey bytes (P2PKH or P2SH), ready to use as
* a `TxOut.scriptPubKey`, or `undefined` if the UA has no transparent receiver.
*/
get transparentScript(): Uint8Array | undefined {
return this._wasm.transparentScript;
}
/**
* Whether `candidate` is a receiver of this unified address.
*
* @param candidate - Another Unified Address (matches if all of its receivers are
* contained here) or a transparent Zcash address (matches this UA's transparent
* receiver). Must be on the same network as this UA.
* @throws If `candidate` is malformed or on the wrong network
*/
contains(candidate: string): boolean {
return this._wasm.contains(candidate);
}
/** @internal */
get wasm(): WasmZcashUnifiedAddress {
return this._wasm;
}
}