@midnight-ntwrk/wallet-sdk-address-format 1.0.0 → 3.0.0-beta.10
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 +66 -16
- package/dist/index.js +233 -0
- package/package.json +29 -21
- package/dist/index.cjs +0 -146
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.mjs +0 -139
- package/dist/index.mjs.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,35 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DustPublicKey, EncryptionSecretKey, UserAddress } from '@midnight-ntwrk/ledger-v7';
|
|
2
|
+
export declare const mainnet: unique symbol;
|
|
3
|
+
export type NetworkId = string | typeof mainnet;
|
|
2
4
|
export type FormatContext = {
|
|
3
|
-
networkId:
|
|
5
|
+
networkId: NetworkId;
|
|
4
6
|
};
|
|
7
|
+
export type Field = {
|
|
8
|
+
bytes: number;
|
|
9
|
+
modulus: bigint;
|
|
10
|
+
};
|
|
11
|
+
export declare const BLSScalar: Field;
|
|
12
|
+
export declare const ScaleBigInt: {
|
|
13
|
+
encode: (data: bigint) => Buffer;
|
|
14
|
+
decode: (repr: Uint8Array) => bigint;
|
|
15
|
+
};
|
|
16
|
+
export declare const Bech32mSymbol: unique symbol;
|
|
17
|
+
export type HasCodec<T> = {
|
|
18
|
+
[Bech32mSymbol]: Bech32mCodec<T>;
|
|
19
|
+
};
|
|
20
|
+
export type CodecTarget<T> = T extends HasCodec<infer U> ? U : never;
|
|
5
21
|
export declare class MidnightBech32m {
|
|
6
|
-
readonly type: string;
|
|
7
|
-
readonly network: string | null;
|
|
8
|
-
readonly data: Buffer;
|
|
9
22
|
static readonly prefix = "mn";
|
|
23
|
+
static encode<T extends HasCodec<T>>(networkId: NetworkId, item: T): MidnightBech32m;
|
|
10
24
|
static validateSegment(segmentName: string, segment: string): void;
|
|
11
25
|
static parse(bech32string: string): MidnightBech32m;
|
|
12
|
-
|
|
26
|
+
readonly type: string;
|
|
27
|
+
readonly network: NetworkId;
|
|
28
|
+
readonly data: Buffer;
|
|
29
|
+
constructor(type: string, network: NetworkId, data: Buffer);
|
|
30
|
+
decode<TClass extends HasCodec<any>>(tclass: TClass, networkId: NetworkId): CodecTarget<TClass>;
|
|
13
31
|
asString(): string;
|
|
32
|
+
toString(): string;
|
|
14
33
|
}
|
|
15
34
|
export declare class Bech32mCodec<T> {
|
|
16
35
|
readonly type: string;
|
|
@@ -18,33 +37,64 @@ export declare class Bech32mCodec<T> {
|
|
|
18
37
|
readonly dataFromBytes: (bytes: Buffer) => T;
|
|
19
38
|
constructor(type: string, dataToBytes: (data: T) => Buffer, dataFromBytes: (bytes: Buffer) => T);
|
|
20
39
|
encode(networkId: NetworkId, data: T): MidnightBech32m;
|
|
21
|
-
decode(networkId: NetworkId
|
|
22
|
-
static createContext(networkId: NetworkId
|
|
23
|
-
static createContextFromZswap(networkId: NetworkId): FormatContext;
|
|
40
|
+
decode(networkId: NetworkId, repr: MidnightBech32m): T;
|
|
41
|
+
static createContext(networkId: NetworkId): FormatContext;
|
|
24
42
|
}
|
|
25
43
|
export declare class ShieldedAddress {
|
|
44
|
+
static readonly codec: Bech32mCodec<ShieldedAddress>;
|
|
45
|
+
static readonly [Bech32mSymbol]: Bech32mCodec<ShieldedAddress>;
|
|
46
|
+
readonly [Bech32mSymbol]: Bech32mCodec<ShieldedAddress>;
|
|
26
47
|
readonly coinPublicKey: ShieldedCoinPublicKey;
|
|
27
48
|
readonly encryptionPublicKey: ShieldedEncryptionPublicKey;
|
|
28
|
-
static codec: Bech32mCodec<ShieldedAddress>;
|
|
29
49
|
constructor(coinPublicKey: ShieldedCoinPublicKey, encryptionPublicKey: ShieldedEncryptionPublicKey);
|
|
30
50
|
coinPublicKeyString(): string;
|
|
31
51
|
encryptionPublicKeyString(): string;
|
|
52
|
+
equals(other: ShieldedAddress): boolean;
|
|
32
53
|
}
|
|
33
54
|
export declare class ShieldedEncryptionSecretKey {
|
|
55
|
+
static readonly codec: Bech32mCodec<ShieldedEncryptionSecretKey>;
|
|
34
56
|
readonly zswap: EncryptionSecretKey;
|
|
35
|
-
static codec: Bech32mCodec<ShieldedEncryptionSecretKey>;
|
|
36
57
|
constructor(zswap: EncryptionSecretKey);
|
|
37
58
|
}
|
|
38
59
|
export declare class ShieldedCoinPublicKey {
|
|
60
|
+
static readonly keyLength = 32;
|
|
61
|
+
static readonly codec: Bech32mCodec<ShieldedCoinPublicKey>;
|
|
62
|
+
static fromHexString(hexString: string): ShieldedCoinPublicKey;
|
|
39
63
|
readonly data: Buffer;
|
|
40
|
-
static readonly key_length = 32;
|
|
41
|
-
static codec: Bech32mCodec<ShieldedCoinPublicKey>;
|
|
42
64
|
constructor(data: Buffer);
|
|
65
|
+
toHexString(): string;
|
|
66
|
+
equals(other: string): boolean;
|
|
67
|
+
equals(other: ShieldedCoinPublicKey): boolean;
|
|
43
68
|
}
|
|
44
69
|
export declare class ShieldedEncryptionPublicKey {
|
|
70
|
+
static readonly keyLength = 32;
|
|
71
|
+
static readonly codec: Bech32mCodec<ShieldedEncryptionPublicKey>;
|
|
72
|
+
static fromHexString(hexString: string): ShieldedEncryptionPublicKey;
|
|
45
73
|
readonly data: Buffer;
|
|
46
|
-
static readonly key_length = 32;
|
|
47
|
-
static codec: Bech32mCodec<ShieldedEncryptionPublicKey>;
|
|
48
74
|
constructor(data: Buffer);
|
|
75
|
+
toHexString(): string;
|
|
76
|
+
equals(other: string): boolean;
|
|
77
|
+
equals(other: ShieldedEncryptionPublicKey): boolean;
|
|
78
|
+
}
|
|
79
|
+
export declare class UnshieldedAddress {
|
|
80
|
+
readonly data: Buffer;
|
|
81
|
+
static readonly keyLength = 32;
|
|
82
|
+
static readonly codec: Bech32mCodec<UnshieldedAddress>;
|
|
83
|
+
static readonly [Bech32mSymbol]: Bech32mCodec<UnshieldedAddress>;
|
|
84
|
+
readonly [Bech32mSymbol]: Bech32mCodec<UnshieldedAddress>;
|
|
85
|
+
constructor(data: Buffer);
|
|
86
|
+
get hexString(): UserAddress;
|
|
87
|
+
equals(other: string): boolean;
|
|
88
|
+
equals(other: UnshieldedAddress): boolean;
|
|
89
|
+
}
|
|
90
|
+
export declare class DustAddress {
|
|
91
|
+
readonly data: bigint;
|
|
92
|
+
static readonly codec: Bech32mCodec<DustAddress>;
|
|
93
|
+
static readonly [Bech32mSymbol]: Bech32mCodec<DustAddress>;
|
|
94
|
+
readonly [Bech32mSymbol]: Bech32mCodec<DustAddress>;
|
|
95
|
+
static readonly encodePublicKey: (networkId: string, publicKey: DustPublicKey) => string;
|
|
96
|
+
constructor(data: bigint);
|
|
97
|
+
serialize(): Buffer;
|
|
98
|
+
equals(other: bigint): boolean;
|
|
99
|
+
equals(other: DustAddress): boolean;
|
|
49
100
|
}
|
|
50
|
-
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
// This file is part of MIDNIGHT-WALLET-SDK.
|
|
2
|
+
// Copyright (C) 2025 Midnight Foundation
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
// You may not use this file except in compliance with the License.
|
|
6
|
+
// You may obtain a copy of the License at
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
// See the License for the specific language governing permissions and
|
|
12
|
+
// limitations under the License.
|
|
13
|
+
import { EncryptionSecretKey } from '@midnight-ntwrk/ledger-v7';
|
|
14
|
+
import { bech32m } from '@scure/base';
|
|
15
|
+
import * as subsquidScale from '@subsquid/scale-codec';
|
|
16
|
+
export const mainnet = Symbol('Mainnet');
|
|
17
|
+
const NetworkId = {
|
|
18
|
+
toString: (networkId) => {
|
|
19
|
+
return networkId === mainnet ? 'mainnet' : networkId;
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
export const BLSScalar = {
|
|
23
|
+
bytes: 32,
|
|
24
|
+
modulus: BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001'),
|
|
25
|
+
};
|
|
26
|
+
export const ScaleBigInt = {
|
|
27
|
+
encode: (data) => {
|
|
28
|
+
const sink = new subsquidScale.ByteSink();
|
|
29
|
+
sink.compact(data);
|
|
30
|
+
return Buffer.from(sink.toBytes());
|
|
31
|
+
},
|
|
32
|
+
decode: (repr) => {
|
|
33
|
+
const src = new subsquidScale.Src(repr);
|
|
34
|
+
const res = src.compact();
|
|
35
|
+
src.assertEOF();
|
|
36
|
+
return BigInt(res);
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
export const Bech32mSymbol = Symbol('MidnightBech32m');
|
|
40
|
+
export class MidnightBech32m {
|
|
41
|
+
static prefix = 'mn';
|
|
42
|
+
static encode(networkId, item) {
|
|
43
|
+
return item[Bech32mSymbol].encode(networkId, item);
|
|
44
|
+
}
|
|
45
|
+
static validateSegment(segmentName, segment) {
|
|
46
|
+
const result = /^[A-Za-z1-9-]+$/.test(segment);
|
|
47
|
+
if (!result) {
|
|
48
|
+
throw new Error(`Segment ${segmentName}: ${segment} contains disallowed characters. Allowed characters are only numbers, latin letters and a hyphen`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
static parse(bech32string) {
|
|
52
|
+
const bech32parsed = bech32m.decodeToBytes(bech32string);
|
|
53
|
+
const [prefix, type, network = mainnet] = bech32parsed.prefix.split('_');
|
|
54
|
+
if (prefix != MidnightBech32m.prefix) {
|
|
55
|
+
throw new Error(`Expected prefix ${MidnightBech32m.prefix}`);
|
|
56
|
+
}
|
|
57
|
+
MidnightBech32m.validateSegment('type', type);
|
|
58
|
+
if (network != mainnet) {
|
|
59
|
+
MidnightBech32m.validateSegment('network', network);
|
|
60
|
+
}
|
|
61
|
+
return new MidnightBech32m(type, network, Buffer.from(bech32parsed.bytes));
|
|
62
|
+
}
|
|
63
|
+
type;
|
|
64
|
+
network;
|
|
65
|
+
data;
|
|
66
|
+
constructor(type, network, data) {
|
|
67
|
+
this.data = data;
|
|
68
|
+
this.network = network;
|
|
69
|
+
this.type = type;
|
|
70
|
+
MidnightBech32m.validateSegment('type', type);
|
|
71
|
+
if (network != mainnet) {
|
|
72
|
+
MidnightBech32m.validateSegment('network', network);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
76
|
+
decode(tclass, networkId) {
|
|
77
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
78
|
+
return tclass[Bech32mSymbol].decode(networkId, this);
|
|
79
|
+
}
|
|
80
|
+
asString() {
|
|
81
|
+
const networkSegment = this.network == mainnet ? '' : `_${this.network}`;
|
|
82
|
+
return bech32m.encode(`${MidnightBech32m.prefix}_${this.type}${networkSegment}`, bech32m.toWords(this.data), false);
|
|
83
|
+
}
|
|
84
|
+
toString() {
|
|
85
|
+
return this.asString();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
export class Bech32mCodec {
|
|
89
|
+
type;
|
|
90
|
+
dataToBytes;
|
|
91
|
+
dataFromBytes;
|
|
92
|
+
constructor(type, dataToBytes, dataFromBytes) {
|
|
93
|
+
this.dataFromBytes = dataFromBytes;
|
|
94
|
+
this.dataToBytes = dataToBytes;
|
|
95
|
+
this.type = type;
|
|
96
|
+
}
|
|
97
|
+
encode(networkId, data) {
|
|
98
|
+
const context = Bech32mCodec.createContext(networkId);
|
|
99
|
+
return new MidnightBech32m(this.type, context.networkId, this.dataToBytes(data));
|
|
100
|
+
}
|
|
101
|
+
decode(networkId, repr) {
|
|
102
|
+
const context = Bech32mCodec.createContext(networkId);
|
|
103
|
+
if (repr.type != this.type) {
|
|
104
|
+
throw new Error(`Expected type ${this.type}, got ${repr.type}`);
|
|
105
|
+
}
|
|
106
|
+
if (context.networkId != repr.network) {
|
|
107
|
+
throw new Error(`Expected ${NetworkId.toString(context.networkId)} address, got ${NetworkId.toString(repr.network)} one`);
|
|
108
|
+
}
|
|
109
|
+
return this.dataFromBytes(repr.data);
|
|
110
|
+
}
|
|
111
|
+
static createContext(networkId) {
|
|
112
|
+
if (networkId === 'mainnet') {
|
|
113
|
+
return { networkId: mainnet };
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
return { networkId };
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
export class ShieldedAddress {
|
|
121
|
+
static codec = new Bech32mCodec('shield-addr', (addr) => Buffer.concat([addr.coinPublicKey.data, addr.encryptionPublicKey.data]), (bytes) => {
|
|
122
|
+
const coinPublicKey = new ShieldedCoinPublicKey(bytes.subarray(0, ShieldedCoinPublicKey.keyLength));
|
|
123
|
+
const encryptionPublicKey = new ShieldedEncryptionPublicKey(bytes.subarray(ShieldedCoinPublicKey.keyLength));
|
|
124
|
+
return new ShieldedAddress(coinPublicKey, encryptionPublicKey);
|
|
125
|
+
});
|
|
126
|
+
static [Bech32mSymbol] = ShieldedAddress.codec;
|
|
127
|
+
[Bech32mSymbol] = ShieldedAddress.codec;
|
|
128
|
+
coinPublicKey;
|
|
129
|
+
encryptionPublicKey;
|
|
130
|
+
constructor(coinPublicKey, encryptionPublicKey) {
|
|
131
|
+
this.encryptionPublicKey = encryptionPublicKey;
|
|
132
|
+
this.coinPublicKey = coinPublicKey;
|
|
133
|
+
}
|
|
134
|
+
coinPublicKeyString() {
|
|
135
|
+
return this.coinPublicKey.data.toString('hex');
|
|
136
|
+
}
|
|
137
|
+
encryptionPublicKeyString() {
|
|
138
|
+
return this.encryptionPublicKey.data.toString('hex');
|
|
139
|
+
}
|
|
140
|
+
equals(other) {
|
|
141
|
+
return this.coinPublicKey.equals(other.coinPublicKey) && this.encryptionPublicKey.equals(other.encryptionPublicKey);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
export class ShieldedEncryptionSecretKey {
|
|
145
|
+
static codec = new Bech32mCodec('shield-esk', (esk) => Buffer.from(esk.zswap.yesIKnowTheSecurityImplicationsOfThis_serialize()), (repr) => new ShieldedEncryptionSecretKey(EncryptionSecretKey.deserialize(repr)));
|
|
146
|
+
// There are some bits in serialization of field elements and elliptic curve points, that are hard to replicate
|
|
147
|
+
// Thus using zswap implementation directly for serialization purposes
|
|
148
|
+
zswap;
|
|
149
|
+
constructor(zswap) {
|
|
150
|
+
this.zswap = zswap;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
export class ShieldedCoinPublicKey {
|
|
154
|
+
static keyLength = 32;
|
|
155
|
+
static codec = new Bech32mCodec('shield-cpk', (cpk) => cpk.data, (repr) => new ShieldedCoinPublicKey(repr));
|
|
156
|
+
static fromHexString(hexString) {
|
|
157
|
+
return new ShieldedCoinPublicKey(Buffer.from(hexString, 'hex'));
|
|
158
|
+
}
|
|
159
|
+
data;
|
|
160
|
+
constructor(data) {
|
|
161
|
+
this.data = data;
|
|
162
|
+
if (data.length != ShieldedCoinPublicKey.keyLength) {
|
|
163
|
+
throw new Error('Coin public key needs to be 32 bytes long');
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
toHexString() {
|
|
167
|
+
return this.data.toString('hex');
|
|
168
|
+
}
|
|
169
|
+
equals(other) {
|
|
170
|
+
const otherKey = typeof other === 'string' ? ShieldedCoinPublicKey.fromHexString(other) : other;
|
|
171
|
+
return otherKey.data.equals(this.data);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
export class ShieldedEncryptionPublicKey {
|
|
175
|
+
static keyLength = 32;
|
|
176
|
+
static codec = new Bech32mCodec('shield-epk', (cpk) => cpk.data, (repr) => new ShieldedEncryptionPublicKey(repr));
|
|
177
|
+
static fromHexString(hexString) {
|
|
178
|
+
return new ShieldedEncryptionPublicKey(Buffer.from(hexString, 'hex'));
|
|
179
|
+
}
|
|
180
|
+
data;
|
|
181
|
+
constructor(data) {
|
|
182
|
+
this.data = data;
|
|
183
|
+
}
|
|
184
|
+
toHexString() {
|
|
185
|
+
return this.data.toString('hex');
|
|
186
|
+
}
|
|
187
|
+
equals(other) {
|
|
188
|
+
const otherKey = typeof other === 'string' ? ShieldedEncryptionPublicKey.fromHexString(other) : other;
|
|
189
|
+
return otherKey.data.equals(this.data);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
export class UnshieldedAddress {
|
|
193
|
+
data;
|
|
194
|
+
static keyLength = 32;
|
|
195
|
+
static codec = new Bech32mCodec('addr', (addr) => addr.data, (repr) => new UnshieldedAddress(repr));
|
|
196
|
+
static [Bech32mSymbol] = UnshieldedAddress.codec;
|
|
197
|
+
[Bech32mSymbol] = UnshieldedAddress.codec;
|
|
198
|
+
constructor(data) {
|
|
199
|
+
if (data.length != UnshieldedAddress.keyLength) {
|
|
200
|
+
throw new Error('Unshielded address needs to be 32 bytes long');
|
|
201
|
+
}
|
|
202
|
+
this.data = data;
|
|
203
|
+
}
|
|
204
|
+
get hexString() {
|
|
205
|
+
return this.data.toString('hex');
|
|
206
|
+
}
|
|
207
|
+
equals(other) {
|
|
208
|
+
const otherAddress = typeof other === 'string' ? new UnshieldedAddress(Buffer.from(other, 'hex')) : other;
|
|
209
|
+
return otherAddress.data.equals(this.data);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
export class DustAddress {
|
|
213
|
+
data;
|
|
214
|
+
static codec = new Bech32mCodec('dust', (daddr) => daddr.serialize(), (repr) => new DustAddress(ScaleBigInt.decode(repr)));
|
|
215
|
+
static [Bech32mSymbol] = DustAddress.codec;
|
|
216
|
+
[Bech32mSymbol] = DustAddress.codec;
|
|
217
|
+
static encodePublicKey = (networkId, publicKey) => {
|
|
218
|
+
return DustAddress.codec.encode(networkId, new DustAddress(publicKey)).asString();
|
|
219
|
+
};
|
|
220
|
+
constructor(data) {
|
|
221
|
+
if (data >= BLSScalar.modulus) {
|
|
222
|
+
throw new Error('Dust address is too large');
|
|
223
|
+
}
|
|
224
|
+
this.data = data;
|
|
225
|
+
}
|
|
226
|
+
serialize() {
|
|
227
|
+
return ScaleBigInt.encode(this.data);
|
|
228
|
+
}
|
|
229
|
+
equals(other) {
|
|
230
|
+
const otherAddress = typeof other === 'bigint' ? other : other.data;
|
|
231
|
+
return otherAddress === this.data;
|
|
232
|
+
}
|
|
233
|
+
}
|
package/package.json
CHANGED
|
@@ -1,43 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midnight-ntwrk/wallet-sdk-address-format",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-beta.10",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"main": "./dist/index.
|
|
6
|
-
"module": "./dist/index.
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
|
-
"author": "
|
|
8
|
+
"author": "Midnight Foundation",
|
|
9
9
|
"license": "Apache-2.0",
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"registry": "https://npm.pkg.github.com/"
|
|
12
12
|
},
|
|
13
|
-
"repository": {
|
|
14
|
-
"type": "git",
|
|
15
|
-
"url": "https://github.com/midnight-ntwrk/artifacts.git"
|
|
16
|
-
},
|
|
17
13
|
"files": [
|
|
18
14
|
"dist/"
|
|
19
15
|
],
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/midnight-ntwrk/artifacts.git"
|
|
19
|
+
},
|
|
20
20
|
"exports": {
|
|
21
21
|
".": {
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"types": "./dist/index.d.ts"
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"import": "./dist/index.js"
|
|
25
24
|
}
|
|
26
25
|
},
|
|
27
26
|
"dependencies": {
|
|
28
|
-
"@
|
|
27
|
+
"@midnight-ntwrk/ledger-v7": "7.0.0-alpha.1",
|
|
28
|
+
"@scure/base": "^1.2.6",
|
|
29
|
+
"@subsquid/scale-codec": "^4.0.1"
|
|
29
30
|
},
|
|
30
31
|
"devDependencies": {
|
|
31
|
-
"@
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
"
|
|
32
|
+
"@types/node": "22.17.0",
|
|
33
|
+
"eslint": "^9.37.0",
|
|
34
|
+
"prettier": "^3.7.0",
|
|
35
|
+
"publint": "~0.3.14",
|
|
36
|
+
"rimraf": "^6.0.1",
|
|
37
|
+
"typescript": "^5.9.3",
|
|
38
|
+
"vitest": "^4.0.16"
|
|
36
39
|
},
|
|
37
40
|
"scripts": {
|
|
38
41
|
"typecheck": "tsc -b ./tsconfig.json --noEmit",
|
|
39
|
-
"test": "
|
|
40
|
-
"lint": "eslint",
|
|
41
|
-
"
|
|
42
|
+
"test": "vitest run",
|
|
43
|
+
"lint": "eslint --max-warnings 0",
|
|
44
|
+
"format": "prettier --write \"**/*.{ts,js,json,yaml,yml,md}\"",
|
|
45
|
+
"format:check": "prettier --check \"**/*.{ts,js,json,yaml,yml,md}\"",
|
|
46
|
+
"dist": "tsc -b ./tsconfig.build.json",
|
|
47
|
+
"dist:publish": "tsc -b ./tsconfig.publish.json",
|
|
48
|
+
"clean": "rimraf --glob dist 'tsconfig.*.tsbuildinfo' && date +%s > .clean-timestamp",
|
|
49
|
+
"publint": "publint --strict"
|
|
42
50
|
}
|
|
43
|
-
}
|
|
51
|
+
}
|
package/dist/index.cjs
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var zswap = require('@midnight-ntwrk/zswap');
|
|
4
|
-
var base = require('@scure/base');
|
|
5
|
-
|
|
6
|
-
class MidnightBech32m {
|
|
7
|
-
type;
|
|
8
|
-
network;
|
|
9
|
-
data;
|
|
10
|
-
static prefix = 'mn';
|
|
11
|
-
static validateSegment(segmentName, segment) {
|
|
12
|
-
const result = /^[A-Za-z1-9-]+$/.test(segment);
|
|
13
|
-
if (!result) {
|
|
14
|
-
throw new Error(`Segment ${segmentName}: ${segment} contains disallowed characters. Allowed characters are only numbers, latin letters and a hyphen`);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
static parse(bech32string) {
|
|
18
|
-
const bech32parsed = base.bech32m.decodeToBytes(bech32string);
|
|
19
|
-
const [prefix, type, network = null] = bech32parsed.prefix.split('_');
|
|
20
|
-
if (prefix != MidnightBech32m.prefix) {
|
|
21
|
-
throw new Error(`Expected prefix ${MidnightBech32m.prefix}`);
|
|
22
|
-
}
|
|
23
|
-
MidnightBech32m.validateSegment('type', type);
|
|
24
|
-
if (network != null) {
|
|
25
|
-
MidnightBech32m.validateSegment('network', network);
|
|
26
|
-
}
|
|
27
|
-
return new MidnightBech32m(type, network, Buffer.from(bech32parsed.bytes));
|
|
28
|
-
}
|
|
29
|
-
constructor(type, network, data) {
|
|
30
|
-
this.type = type;
|
|
31
|
-
this.network = network;
|
|
32
|
-
this.data = data;
|
|
33
|
-
MidnightBech32m.validateSegment('type', type);
|
|
34
|
-
if (network != null) {
|
|
35
|
-
MidnightBech32m.validateSegment('network', network);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
asString() {
|
|
39
|
-
const networkSegment = this.network == null ? '' : `_${this.network}`;
|
|
40
|
-
return base.bech32m.encode(`${MidnightBech32m.prefix}_${this.type}${networkSegment}`, base.bech32m.toWords(this.data), false);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
class Bech32mCodec {
|
|
44
|
-
type;
|
|
45
|
-
dataToBytes;
|
|
46
|
-
dataFromBytes;
|
|
47
|
-
constructor(type, dataToBytes, dataFromBytes) {
|
|
48
|
-
this.type = type;
|
|
49
|
-
this.dataToBytes = dataToBytes;
|
|
50
|
-
this.dataFromBytes = dataFromBytes;
|
|
51
|
-
}
|
|
52
|
-
encode(networkId, data) {
|
|
53
|
-
const context = Bech32mCodec.createContext(networkId);
|
|
54
|
-
return new MidnightBech32m(this.type, context.networkId, this.dataToBytes(data));
|
|
55
|
-
}
|
|
56
|
-
decode(networkId, repr) {
|
|
57
|
-
const context = Bech32mCodec.createContext(networkId);
|
|
58
|
-
if (repr.type != this.type) {
|
|
59
|
-
throw new Error(`Expected type ${this.type}, got ${repr.type}`);
|
|
60
|
-
}
|
|
61
|
-
if (context.networkId != repr.network) {
|
|
62
|
-
throw new Error(`Expected ${context.networkId ?? 'mainnet'} address, got ${repr.network ?? 'mainnet'} one`);
|
|
63
|
-
}
|
|
64
|
-
return this.dataFromBytes(repr.data);
|
|
65
|
-
}
|
|
66
|
-
static createContext(networkId) {
|
|
67
|
-
if (networkId === null) {
|
|
68
|
-
return { networkId: null };
|
|
69
|
-
}
|
|
70
|
-
else if (typeof networkId === 'string') {
|
|
71
|
-
return { networkId };
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
return Bech32mCodec.createContextFromZswap(networkId);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
static createContextFromZswap(networkId) {
|
|
78
|
-
switch (networkId) {
|
|
79
|
-
case zswap.NetworkId.DevNet:
|
|
80
|
-
return { networkId: 'dev' };
|
|
81
|
-
case zswap.NetworkId.MainNet:
|
|
82
|
-
return { networkId: null };
|
|
83
|
-
case zswap.NetworkId.TestNet:
|
|
84
|
-
return { networkId: 'test' };
|
|
85
|
-
case zswap.NetworkId.Undeployed:
|
|
86
|
-
return { networkId: 'undeployed' };
|
|
87
|
-
default:
|
|
88
|
-
return { networkId: null };
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
class ShieldedAddress {
|
|
93
|
-
coinPublicKey;
|
|
94
|
-
encryptionPublicKey;
|
|
95
|
-
static codec = new Bech32mCodec('shield-addr', (addr) => Buffer.concat([addr.coinPublicKey.data, addr.encryptionPublicKey.data]), (bytes) => {
|
|
96
|
-
const coinPublicKey = new ShieldedCoinPublicKey(bytes.subarray(0, ShieldedCoinPublicKey.key_length));
|
|
97
|
-
const encryptionPublicKey = new ShieldedEncryptionPublicKey(bytes.subarray(ShieldedCoinPublicKey.key_length));
|
|
98
|
-
return new ShieldedAddress(coinPublicKey, encryptionPublicKey);
|
|
99
|
-
});
|
|
100
|
-
constructor(coinPublicKey, encryptionPublicKey) {
|
|
101
|
-
this.coinPublicKey = coinPublicKey;
|
|
102
|
-
this.encryptionPublicKey = encryptionPublicKey;
|
|
103
|
-
}
|
|
104
|
-
coinPublicKeyString() {
|
|
105
|
-
return this.coinPublicKey.data.toString('hex');
|
|
106
|
-
}
|
|
107
|
-
encryptionPublicKeyString() {
|
|
108
|
-
return this.encryptionPublicKey.data.toString('hex');
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
class ShieldedEncryptionSecretKey {
|
|
112
|
-
zswap;
|
|
113
|
-
static codec = new Bech32mCodec('shield-esk', (esk) => Buffer.from(esk.zswap.yesIKnowTheSecurityImplicationsOfThis_serialize(zswap.NetworkId.Undeployed).subarray(1)), (repr) => new ShieldedEncryptionSecretKey(zswap.EncryptionSecretKey.deserialize(Buffer.concat([Buffer.of(0), repr]), zswap.NetworkId.Undeployed)));
|
|
114
|
-
// There are some bits in serialization of field elements and elliptic curve points, that are hard to replicate
|
|
115
|
-
// Thus using zswap implementation directly for serialization purposes
|
|
116
|
-
constructor(zswap) {
|
|
117
|
-
this.zswap = zswap;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
class ShieldedCoinPublicKey {
|
|
121
|
-
data;
|
|
122
|
-
static key_length = 32;
|
|
123
|
-
static codec = new Bech32mCodec('shield-cpk', (cpk) => cpk.data, (repr) => new ShieldedCoinPublicKey(repr));
|
|
124
|
-
constructor(data) {
|
|
125
|
-
this.data = data;
|
|
126
|
-
if (data.length != ShieldedCoinPublicKey.key_length) {
|
|
127
|
-
throw new Error('Coin public key needs to be 32 bytes long');
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
class ShieldedEncryptionPublicKey {
|
|
132
|
-
data;
|
|
133
|
-
static key_length = 32;
|
|
134
|
-
static codec = new Bech32mCodec('shield-epk', (cpk) => cpk.data, (repr) => new ShieldedEncryptionPublicKey(repr));
|
|
135
|
-
constructor(data) {
|
|
136
|
-
this.data = data;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
exports.Bech32mCodec = Bech32mCodec;
|
|
141
|
-
exports.MidnightBech32m = MidnightBech32m;
|
|
142
|
-
exports.ShieldedAddress = ShieldedAddress;
|
|
143
|
-
exports.ShieldedCoinPublicKey = ShieldedCoinPublicKey;
|
|
144
|
-
exports.ShieldedEncryptionPublicKey = ShieldedEncryptionPublicKey;
|
|
145
|
-
exports.ShieldedEncryptionSecretKey = ShieldedEncryptionSecretKey;
|
|
146
|
-
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":[null],"names":["bech32m","NetworkId","EncryptionSecretKey"],"mappings":";;;;;MAOa,eAAe,CAAA;AA2BR,IAAA,IAAA;AACA,IAAA,OAAA;AACA,IAAA,IAAA;AA5BX,IAAA,OAAgB,MAAM,GAAG,IAAI;AAEpC,IAAA,OAAO,eAAe,CAAC,WAAmB,EAAE,OAAe,EAAA;QACzD,MAAM,MAAM,GAAG,iBAAiB,CAAC,IAAI,CAAC,OAAO,CAAC;QAC9C,IAAI,CAAC,MAAM,EAAE;YACX,MAAM,IAAI,KAAK,CACb,CAAA,QAAA,EAAW,WAAW,CAAK,EAAA,EAAA,OAAO,CAAkG,gGAAA,CAAA,CACrI;;;IAIL,OAAO,KAAK,CAAC,YAAoB,EAAA;QAC/B,MAAM,YAAY,GAAGA,YAAO,CAAC,aAAa,CAAC,YAAY,CAAC;AACxD,QAAA,MAAM,CAAC,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,IAAI,CAAC,GAAG,YAAY,CAAC,MA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;IAKtD,MAAM,CAAC,SAAS,EAAE,SAAS,GAAG,MAAM,GAAG,IAAI,EAAE,IAAI,EAAE,eAAe,GAAG,CAAC;IAWtE,MAAM,CAAC,aAAa,CAAC,SAAS,EAAE,SAAS,GAAG,MAAM,GAAG,IAAI,GAAG,aAAa;IAUzE,MAAM,CAAC,sBAAsB,CAAC,SAAS,EAAE,SAAS,GAAG,aAAa;CAcnE;AAED,qBAAa,eAAe;aAYR,aAAa,EAAE,qBAAqB;aACpC,mBAAmB,EAAE,2BAA2B;IAZlE,MAAM,CAAC,KAAK,gCAQV;gBAGgB,aAAa,EAAE,qBAAqB,EACpC,mBAAmB,EAAE,2BAA2B;IAGlE,mBAAmB,IAAI,MAAM;IAI7B,yBAAyB,IAAI,MAAM;CAGpC;AAED,qBAAa,2BAA2B;aAYV,KAAK,EAAE,mBAAmB;IAXtD,MAAM,CAAC,KAAK,4CAOV;gBAI0B,KAAK,EAAE,mBAAmB;CACvD;AAED,qBAAa,qBAAqB;aASJ,IAAI,EAAE,MAAM;IARxC,MAAM,CAAC,QAAQ,CAAC,UAAU,MAAM;IAEhC,MAAM,CAAC,KAAK,EAAE,YAAY,CAAC,qBAAqB,CAAC,CAI/C;gBAE0B,IAAI,EAAE,MAAM;CAKzC;AAED,qBAAa,2BAA2B;aASV,IAAI,EAAE,MAAM;IARxC,MAAM,CAAC,QAAQ,CAAC,UAAU,MAAM;IAEhC,MAAM,CAAC,KAAK,EAAE,YAAY,CAAC,2BAA2B,CAAC,CAIrD;gBAE0B,IAAI,EAAE,MAAM;CACzC"}
|
package/dist/index.mjs
DELETED
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
import { NetworkId, EncryptionSecretKey } from '@midnight-ntwrk/zswap';
|
|
2
|
-
import { bech32m } from '@scure/base';
|
|
3
|
-
|
|
4
|
-
class MidnightBech32m {
|
|
5
|
-
type;
|
|
6
|
-
network;
|
|
7
|
-
data;
|
|
8
|
-
static prefix = 'mn';
|
|
9
|
-
static validateSegment(segmentName, segment) {
|
|
10
|
-
const result = /^[A-Za-z1-9-]+$/.test(segment);
|
|
11
|
-
if (!result) {
|
|
12
|
-
throw new Error(`Segment ${segmentName}: ${segment} contains disallowed characters. Allowed characters are only numbers, latin letters and a hyphen`);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
static parse(bech32string) {
|
|
16
|
-
const bech32parsed = bech32m.decodeToBytes(bech32string);
|
|
17
|
-
const [prefix, type, network = null] = bech32parsed.prefix.split('_');
|
|
18
|
-
if (prefix != MidnightBech32m.prefix) {
|
|
19
|
-
throw new Error(`Expected prefix ${MidnightBech32m.prefix}`);
|
|
20
|
-
}
|
|
21
|
-
MidnightBech32m.validateSegment('type', type);
|
|
22
|
-
if (network != null) {
|
|
23
|
-
MidnightBech32m.validateSegment('network', network);
|
|
24
|
-
}
|
|
25
|
-
return new MidnightBech32m(type, network, Buffer.from(bech32parsed.bytes));
|
|
26
|
-
}
|
|
27
|
-
constructor(type, network, data) {
|
|
28
|
-
this.type = type;
|
|
29
|
-
this.network = network;
|
|
30
|
-
this.data = data;
|
|
31
|
-
MidnightBech32m.validateSegment('type', type);
|
|
32
|
-
if (network != null) {
|
|
33
|
-
MidnightBech32m.validateSegment('network', network);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
asString() {
|
|
37
|
-
const networkSegment = this.network == null ? '' : `_${this.network}`;
|
|
38
|
-
return bech32m.encode(`${MidnightBech32m.prefix}_${this.type}${networkSegment}`, bech32m.toWords(this.data), false);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
class Bech32mCodec {
|
|
42
|
-
type;
|
|
43
|
-
dataToBytes;
|
|
44
|
-
dataFromBytes;
|
|
45
|
-
constructor(type, dataToBytes, dataFromBytes) {
|
|
46
|
-
this.type = type;
|
|
47
|
-
this.dataToBytes = dataToBytes;
|
|
48
|
-
this.dataFromBytes = dataFromBytes;
|
|
49
|
-
}
|
|
50
|
-
encode(networkId, data) {
|
|
51
|
-
const context = Bech32mCodec.createContext(networkId);
|
|
52
|
-
return new MidnightBech32m(this.type, context.networkId, this.dataToBytes(data));
|
|
53
|
-
}
|
|
54
|
-
decode(networkId, repr) {
|
|
55
|
-
const context = Bech32mCodec.createContext(networkId);
|
|
56
|
-
if (repr.type != this.type) {
|
|
57
|
-
throw new Error(`Expected type ${this.type}, got ${repr.type}`);
|
|
58
|
-
}
|
|
59
|
-
if (context.networkId != repr.network) {
|
|
60
|
-
throw new Error(`Expected ${context.networkId ?? 'mainnet'} address, got ${repr.network ?? 'mainnet'} one`);
|
|
61
|
-
}
|
|
62
|
-
return this.dataFromBytes(repr.data);
|
|
63
|
-
}
|
|
64
|
-
static createContext(networkId) {
|
|
65
|
-
if (networkId === null) {
|
|
66
|
-
return { networkId: null };
|
|
67
|
-
}
|
|
68
|
-
else if (typeof networkId === 'string') {
|
|
69
|
-
return { networkId };
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
return Bech32mCodec.createContextFromZswap(networkId);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
static createContextFromZswap(networkId) {
|
|
76
|
-
switch (networkId) {
|
|
77
|
-
case NetworkId.DevNet:
|
|
78
|
-
return { networkId: 'dev' };
|
|
79
|
-
case NetworkId.MainNet:
|
|
80
|
-
return { networkId: null };
|
|
81
|
-
case NetworkId.TestNet:
|
|
82
|
-
return { networkId: 'test' };
|
|
83
|
-
case NetworkId.Undeployed:
|
|
84
|
-
return { networkId: 'undeployed' };
|
|
85
|
-
default:
|
|
86
|
-
return { networkId: null };
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
class ShieldedAddress {
|
|
91
|
-
coinPublicKey;
|
|
92
|
-
encryptionPublicKey;
|
|
93
|
-
static codec = new Bech32mCodec('shield-addr', (addr) => Buffer.concat([addr.coinPublicKey.data, addr.encryptionPublicKey.data]), (bytes) => {
|
|
94
|
-
const coinPublicKey = new ShieldedCoinPublicKey(bytes.subarray(0, ShieldedCoinPublicKey.key_length));
|
|
95
|
-
const encryptionPublicKey = new ShieldedEncryptionPublicKey(bytes.subarray(ShieldedCoinPublicKey.key_length));
|
|
96
|
-
return new ShieldedAddress(coinPublicKey, encryptionPublicKey);
|
|
97
|
-
});
|
|
98
|
-
constructor(coinPublicKey, encryptionPublicKey) {
|
|
99
|
-
this.coinPublicKey = coinPublicKey;
|
|
100
|
-
this.encryptionPublicKey = encryptionPublicKey;
|
|
101
|
-
}
|
|
102
|
-
coinPublicKeyString() {
|
|
103
|
-
return this.coinPublicKey.data.toString('hex');
|
|
104
|
-
}
|
|
105
|
-
encryptionPublicKeyString() {
|
|
106
|
-
return this.encryptionPublicKey.data.toString('hex');
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
class ShieldedEncryptionSecretKey {
|
|
110
|
-
zswap;
|
|
111
|
-
static codec = new Bech32mCodec('shield-esk', (esk) => Buffer.from(esk.zswap.yesIKnowTheSecurityImplicationsOfThis_serialize(NetworkId.Undeployed).subarray(1)), (repr) => new ShieldedEncryptionSecretKey(EncryptionSecretKey.deserialize(Buffer.concat([Buffer.of(0), repr]), NetworkId.Undeployed)));
|
|
112
|
-
// There are some bits in serialization of field elements and elliptic curve points, that are hard to replicate
|
|
113
|
-
// Thus using zswap implementation directly for serialization purposes
|
|
114
|
-
constructor(zswap) {
|
|
115
|
-
this.zswap = zswap;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
class ShieldedCoinPublicKey {
|
|
119
|
-
data;
|
|
120
|
-
static key_length = 32;
|
|
121
|
-
static codec = new Bech32mCodec('shield-cpk', (cpk) => cpk.data, (repr) => new ShieldedCoinPublicKey(repr));
|
|
122
|
-
constructor(data) {
|
|
123
|
-
this.data = data;
|
|
124
|
-
if (data.length != ShieldedCoinPublicKey.key_length) {
|
|
125
|
-
throw new Error('Coin public key needs to be 32 bytes long');
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
class ShieldedEncryptionPublicKey {
|
|
130
|
-
data;
|
|
131
|
-
static key_length = 32;
|
|
132
|
-
static codec = new Bech32mCodec('shield-epk', (cpk) => cpk.data, (repr) => new ShieldedEncryptionPublicKey(repr));
|
|
133
|
-
constructor(data) {
|
|
134
|
-
this.data = data;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export { Bech32mCodec, MidnightBech32m, ShieldedAddress, ShieldedCoinPublicKey, ShieldedEncryptionPublicKey, ShieldedEncryptionSecretKey };
|
|
139
|
-
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":[null],"names":[],"mappings":";;;MAOa,eAAe,CAAA;AA2BR,IAAA,IAAA;AACA,IAAA,OAAA;AACA,IAAA,IAAA;AA5BX,IAAA,OAAgB,MAAM,GAAG,IAAI;AAEpC,IAAA,OAAO,eAAe,CAAC,WAAmB,EAAE,OAAe,EAAA;QACzD,MAAM,MAAM,GAAG,iBAAiB,CAAC,IAAI,CAAC,OAAO,CAAC;QAC9C,IAAI,CAAC,MAAM,EAAE;YACX,MAAM,IAAI,KAAK,CACb,CAAA,QAAA,EAAW,WAAW,CAAK,EAAA,EAAA,OAAO,CAAkG,gGAAA,CAAA,CACrI;;;IAIL,OAAO,KAAK,CAAC,YAAoB,EAAA;QAC/B,MAAM,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC,YAAY,CAAC;AACxD,QAAA,MAAM,CAAC,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,IAAI,CAAC,GAAG,YAAY,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC;AACrE,QAAA,IAAI,MAAM,IAAI,eAAe,CAAC,MAAM,EAAE;YACpC,MAAM,IAAI,KAAK,CAAC,CAAA,gBAAA,EAAmB,eAAe,CAAC,MAAM,CAAE,CAAA,CAAC;;AAE9D,QAAA,eAAe,CAAC,eAAe,CAAC,MAAM,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