@midnightntwrk/wallet-sdk-address-format 4.0.0-rc.1 → 4.0.0-rc.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.d.ts +17 -0
- package/dist/index.js +24 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -25,6 +25,23 @@ export declare class MidnightBech32m {
|
|
|
25
25
|
static readonly prefix = "mn";
|
|
26
26
|
static encode<T extends HasCodec<T>>(networkId: NetworkId, item: T): MidnightBech32m;
|
|
27
27
|
static validateSegment(segmentName: string, segment: string): void;
|
|
28
|
+
/**
|
|
29
|
+
* Parses any `mn_`-prefixed Bech32m string, whatever its length, into its credential type, network and payload.
|
|
30
|
+
*
|
|
31
|
+
* Midnight addresses exceed BIP-173's 90-character limit by design (a shielded address is 124 characters on mainnet,
|
|
32
|
+
* and the network identifier is unbounded), so no overall length is enforced here: each credential type's codec
|
|
33
|
+
* checks its own payload size when the result is decoded.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* const parsed = MidnightBech32m.parse(encoded);
|
|
37
|
+
* // Equivalent to ShieldedAddress.codec.decode('testnet', parsed)
|
|
38
|
+
* const address = parsed.decode(ShieldedAddress, 'testnet');
|
|
39
|
+
*
|
|
40
|
+
* @param bech32string - The Bech32m string to parse, such as `mn_shield-addr_testnet1…`.
|
|
41
|
+
* @returns The credential type, network (`mainnet` when the string has no network segment) and raw payload.
|
|
42
|
+
* @throws Error if the checksum or characters are invalid, the prefix is not `mn`, or the type or network segment
|
|
43
|
+
* contains a disallowed character.
|
|
44
|
+
*/
|
|
28
45
|
static parse(bech32string: string): MidnightBech32m;
|
|
29
46
|
readonly type: string;
|
|
30
47
|
readonly network: NetworkId;
|
package/dist/index.js
CHANGED
|
@@ -48,8 +48,25 @@ export class MidnightBech32m {
|
|
|
48
48
|
throw new Error(`Segment ${segmentName}: ${segment} contains disallowed characters. Allowed characters are only numbers, latin letters and a hyphen`);
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* Parses any `mn_`-prefixed Bech32m string, whatever its length, into its credential type, network and payload.
|
|
53
|
+
*
|
|
54
|
+
* Midnight addresses exceed BIP-173's 90-character limit by design (a shielded address is 124 characters on mainnet,
|
|
55
|
+
* and the network identifier is unbounded), so no overall length is enforced here: each credential type's codec
|
|
56
|
+
* checks its own payload size when the result is decoded.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* const parsed = MidnightBech32m.parse(encoded);
|
|
60
|
+
* // Equivalent to ShieldedAddress.codec.decode('testnet', parsed)
|
|
61
|
+
* const address = parsed.decode(ShieldedAddress, 'testnet');
|
|
62
|
+
*
|
|
63
|
+
* @param bech32string - The Bech32m string to parse, such as `mn_shield-addr_testnet1…`.
|
|
64
|
+
* @returns The credential type, network (`mainnet` when the string has no network segment) and raw payload.
|
|
65
|
+
* @throws Error if the checksum or characters are invalid, the prefix is not `mn`, or the type or network segment
|
|
66
|
+
* contains a disallowed character.
|
|
67
|
+
*/
|
|
51
68
|
static parse(bech32string) {
|
|
52
|
-
const bech32parsed = bech32m.decodeToBytes(bech32string);
|
|
69
|
+
const bech32parsed = bech32m.decodeToBytes(bech32string, false);
|
|
53
70
|
const [prefix, type, network = mainnet] = bech32parsed.prefix.split('_');
|
|
54
71
|
if (prefix != MidnightBech32m.prefix) {
|
|
55
72
|
throw new Error(`Expected prefix ${MidnightBech32m.prefix}`);
|
|
@@ -119,6 +136,9 @@ export class Bech32mCodec {
|
|
|
119
136
|
}
|
|
120
137
|
export class ShieldedAddress {
|
|
121
138
|
static codec = new Bech32mCodec('shield-addr', (addr) => Buffer.concat([addr.coinPublicKey.data, addr.encryptionPublicKey.data]), (bytes) => {
|
|
139
|
+
if (bytes.length != ShieldedCoinPublicKey.keyLength + ShieldedEncryptionPublicKey.keyLength) {
|
|
140
|
+
throw new Error('Shielded address needs to be 64 bytes long');
|
|
141
|
+
}
|
|
122
142
|
const coinPublicKey = new ShieldedCoinPublicKey(bytes.subarray(0, ShieldedCoinPublicKey.keyLength));
|
|
123
143
|
const encryptionPublicKey = new ShieldedEncryptionPublicKey(bytes.subarray(ShieldedCoinPublicKey.keyLength));
|
|
124
144
|
return new ShieldedAddress(coinPublicKey, encryptionPublicKey);
|
|
@@ -180,6 +200,9 @@ export class ShieldedEncryptionPublicKey {
|
|
|
180
200
|
data;
|
|
181
201
|
constructor(data) {
|
|
182
202
|
this.data = data;
|
|
203
|
+
if (data.length != ShieldedEncryptionPublicKey.keyLength) {
|
|
204
|
+
throw new Error('Encryption public key needs to be 32 bytes long');
|
|
205
|
+
}
|
|
183
206
|
}
|
|
184
207
|
toHexString() {
|
|
185
208
|
return this.data.toString('hex');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midnightntwrk/wallet-sdk-address-format",
|
|
3
|
-
"version": "4.0.0-rc.
|
|
3
|
+
"version": "4.0.0-rc.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@midnightntwrk/ledger-v9": "1.0.0-rc.7",
|
|
30
|
-
"@scure/base": "^2.
|
|
30
|
+
"@scure/base": "^2.4.0",
|
|
31
31
|
"@subsquid/scale-codec": "^4.0.1"
|
|
32
32
|
},
|
|
33
33
|
"scripts": {
|