@midnightntwrk/wallet-sdk-address-format 3.1.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/README.md +175 -0
- package/dist/index.d.ts +104 -0
- package/dist/index.js +233 -0
- package/package.json +44 -0
package/README.md
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
# @midnightntwrk/wallet-sdk-address-format
|
|
2
|
+
|
|
3
|
+
Bech32m address encoding and decoding for the Midnight network.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @midnightntwrk/wallet-sdk-address-format
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Overview
|
|
12
|
+
|
|
13
|
+
This package provides Bech32m address formatting for the Midnight blockchain. It supports encoding and decoding of
|
|
14
|
+
various address types including shielded addresses (for ZK transactions), unshielded addresses (for transparent
|
|
15
|
+
transactions), and dust addresses (for fee tokens).
|
|
16
|
+
|
|
17
|
+
Key features:
|
|
18
|
+
|
|
19
|
+
- Bech32m encoding/decoding with Midnight-specific prefix (`mn_`)
|
|
20
|
+
- Network-aware address formatting (mainnet vs testnet)
|
|
21
|
+
- Support for shielded, unshielded, and dust address types
|
|
22
|
+
- Type-safe codec system for custom address types
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
### Unshielded Address
|
|
27
|
+
|
|
28
|
+
For transparent transactions on the Midnight network.
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { UnshieldedAddress, MidnightBech32m, mainnet } from '@midnightntwrk/wallet-sdk-address-format';
|
|
32
|
+
import type { NetworkId } from '@midnightntwrk/wallet-sdk-address-format';
|
|
33
|
+
import { addressFromKey, signatureVerifyingKey } from '@midnight-ntwrk/ledger-v7';
|
|
34
|
+
import { randomBytes } from 'node:crypto';
|
|
35
|
+
|
|
36
|
+
const networkId: NetworkId = 'preview';
|
|
37
|
+
|
|
38
|
+
// Derive an unshielded address from a random unshielded secret key
|
|
39
|
+
const secretKey = randomBytes(32);
|
|
40
|
+
const publicKey = signatureVerifyingKey(secretKey.toString('hex'));
|
|
41
|
+
const addressHex = addressFromKey(publicKey);
|
|
42
|
+
const unshieldedAddress = new UnshieldedAddress(Buffer.from(addressHex, 'hex'));
|
|
43
|
+
|
|
44
|
+
// Encode to Bech32m string: mn_addr_preview1...
|
|
45
|
+
const encoded = MidnightBech32m.encode(networkId, unshieldedAddress).toString();
|
|
46
|
+
|
|
47
|
+
// Parse and decode back to UnshieldedAddress
|
|
48
|
+
const decoded = MidnightBech32m.parse(encoded).decode(UnshieldedAddress, networkId);
|
|
49
|
+
|
|
50
|
+
// Compare addresses
|
|
51
|
+
unshieldedAddress.equals(decoded); // true
|
|
52
|
+
|
|
53
|
+
// Mainnet addresses omit the network suffix: mn_addr1...
|
|
54
|
+
const mainnetEncoded = MidnightBech32m.encode(mainnet, unshieldedAddress).toString();
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Shielded Address
|
|
58
|
+
|
|
59
|
+
For zero-knowledge transactions on the Midnight network.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import {
|
|
63
|
+
ShieldedAddress,
|
|
64
|
+
ShieldedCoinPublicKey,
|
|
65
|
+
ShieldedEncryptionPublicKey,
|
|
66
|
+
MidnightBech32m,
|
|
67
|
+
} from '@midnightntwrk/wallet-sdk-address-format';
|
|
68
|
+
import type { NetworkId } from '@midnightntwrk/wallet-sdk-address-format';
|
|
69
|
+
import * as ledger from '@midnight-ntwrk/ledger-v7';
|
|
70
|
+
import { randomBytes } from 'node:crypto';
|
|
71
|
+
|
|
72
|
+
const networkId: NetworkId = 'preview';
|
|
73
|
+
|
|
74
|
+
// Create shielded keys from a seed
|
|
75
|
+
const shieldedSeed = randomBytes(32); // Your 32-byte seed
|
|
76
|
+
const shieldedKeys = ledger.ZswapSecretKeys.fromSeed(shieldedSeed);
|
|
77
|
+
|
|
78
|
+
// Create a shielded address from the keys
|
|
79
|
+
const shieldedAddress = new ShieldedAddress(
|
|
80
|
+
new ShieldedCoinPublicKey(Buffer.from(shieldedKeys.coinPublicKey, 'hex')),
|
|
81
|
+
new ShieldedEncryptionPublicKey(Buffer.from(shieldedKeys.encryptionPublicKey, 'hex')),
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
// Encode to Bech32m string: mn_shield-addr_preview1...
|
|
85
|
+
const encoded = MidnightBech32m.encode(networkId, shieldedAddress).toString();
|
|
86
|
+
|
|
87
|
+
// Parse and decode back to ShieldedAddress
|
|
88
|
+
const decoded = MidnightBech32m.parse(encoded).decode(ShieldedAddress, networkId);
|
|
89
|
+
|
|
90
|
+
// Access individual key components
|
|
91
|
+
decoded.coinPublicKeyString();
|
|
92
|
+
decoded.encryptionPublicKeyString();
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Dust Address
|
|
96
|
+
|
|
97
|
+
For fee token operations on the Midnight network.
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
import { DustAddress, MidnightBech32m } from '@midnightntwrk/wallet-sdk-address-format';
|
|
101
|
+
import type { NetworkId } from '@midnightntwrk/wallet-sdk-address-format';
|
|
102
|
+
import * as ledger from '@midnight-ntwrk/ledger-v7';
|
|
103
|
+
import { randomBytes } from 'node:crypto';
|
|
104
|
+
|
|
105
|
+
const networkId: NetworkId = 'preview';
|
|
106
|
+
|
|
107
|
+
// Create a dust secret key from a seed
|
|
108
|
+
const dustSeed = randomBytes(32); // Your 32-byte seed
|
|
109
|
+
const dustSecretKey = ledger.DustSecretKey.fromSeed(dustSeed);
|
|
110
|
+
|
|
111
|
+
// Create a dust address from the public key
|
|
112
|
+
const dustAddress = new DustAddress(dustSecretKey.publicKey);
|
|
113
|
+
|
|
114
|
+
// Encode to Bech32m string: mn_dust_preview1...
|
|
115
|
+
const encoded = MidnightBech32m.encode(networkId, dustAddress).toString();
|
|
116
|
+
|
|
117
|
+
// Parse and decode back to DustAddress
|
|
118
|
+
const decoded = MidnightBech32m.parse(encoded).decode(DustAddress, networkId);
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Address Format
|
|
122
|
+
|
|
123
|
+
All Midnight addresses use the Bech32m encoding with the following structure:
|
|
124
|
+
|
|
125
|
+
```
|
|
126
|
+
mn_<type>[_<network>]1<data>
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
- `mn` - Midnight prefix
|
|
130
|
+
- `type` - Address type identifier (e.g., `shield-addr`, `addr`, `dust`)
|
|
131
|
+
- `network` - Optional network identifier (omitted for mainnet)
|
|
132
|
+
- `data` - Bech32m-encoded payload
|
|
133
|
+
|
|
134
|
+
### Address Types
|
|
135
|
+
|
|
136
|
+
| Type | Bech32m Type | Description |
|
|
137
|
+
| ----------------- | ------------- | ---------------------------------------------------------- |
|
|
138
|
+
| Shielded | `shield-addr` | Combined coin + encryption public keys for ZK transactions |
|
|
139
|
+
| Coin Public Key | `shield-cpk` | Shielded coin public key only |
|
|
140
|
+
| Encryption Key | `shield-epk` | Shielded encryption public key only |
|
|
141
|
+
| Encryption Secret | `shield-esk` | Shielded encryption secret key |
|
|
142
|
+
| Unshielded | `addr` | Public address for transparent transactions |
|
|
143
|
+
| Dust | `dust` | Address for fee token operations |
|
|
144
|
+
|
|
145
|
+
## Exports
|
|
146
|
+
|
|
147
|
+
### Core Classes
|
|
148
|
+
|
|
149
|
+
- `MidnightBech32m` - Main class for parsing and encoding Bech32m addresses
|
|
150
|
+
- `Bech32mCodec` - Generic codec for creating custom address types
|
|
151
|
+
|
|
152
|
+
### Address Types
|
|
153
|
+
|
|
154
|
+
- `ShieldedAddress` - Full shielded address with coin and encryption keys
|
|
155
|
+
- `ShieldedCoinPublicKey` - 32-byte coin public key
|
|
156
|
+
- `ShieldedEncryptionPublicKey` - 32-byte encryption public key
|
|
157
|
+
- `ShieldedEncryptionSecretKey` - Encryption secret key wrapper
|
|
158
|
+
- `UnshieldedAddress` - 32-byte transparent address
|
|
159
|
+
- `DustAddress` - Fee token address using BLS scalar
|
|
160
|
+
|
|
161
|
+
### Types and Constants
|
|
162
|
+
|
|
163
|
+
- `NetworkId` - Type for network identification (`mainnet` symbol or string)
|
|
164
|
+
- `mainnet` - Symbol representing the mainnet network
|
|
165
|
+
- `FormatContext` - Context type for encoding/decoding
|
|
166
|
+
- `BLSScalar` - BLS curve scalar field definition
|
|
167
|
+
- `ScaleBigInt` - SCALE codec utilities for bigint encoding
|
|
168
|
+
- `Bech32mSymbol` - Symbol for codec attachment
|
|
169
|
+
- `HasCodec` - Type helper for codec-enabled classes
|
|
170
|
+
- `CodecTarget` - Type helper for extracting codec target type
|
|
171
|
+
- `Field` - Type for field definitions
|
|
172
|
+
|
|
173
|
+
## License
|
|
174
|
+
|
|
175
|
+
Apache-2.0
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { type DustPublicKey, EncryptionSecretKey, type UserAddress } from '@midnight-ntwrk/ledger-v8';
|
|
2
|
+
export declare const mainnet: unique symbol;
|
|
3
|
+
type NetworkId = string | typeof mainnet;
|
|
4
|
+
declare const NetworkId: {
|
|
5
|
+
toString: (networkId: NetworkId) => string;
|
|
6
|
+
};
|
|
7
|
+
export type FormatContext = {
|
|
8
|
+
networkId: NetworkId;
|
|
9
|
+
};
|
|
10
|
+
export type Field = {
|
|
11
|
+
bytes: number;
|
|
12
|
+
modulus: bigint;
|
|
13
|
+
};
|
|
14
|
+
export declare const BLSScalar: Field;
|
|
15
|
+
export declare const ScaleBigInt: {
|
|
16
|
+
encode: (data: bigint) => Buffer;
|
|
17
|
+
decode: (repr: Uint8Array) => bigint;
|
|
18
|
+
};
|
|
19
|
+
export declare const Bech32mSymbol: unique symbol;
|
|
20
|
+
export type HasCodec<T> = {
|
|
21
|
+
[Bech32mSymbol]: Bech32mCodec<T>;
|
|
22
|
+
};
|
|
23
|
+
export type CodecTarget<T> = T extends HasCodec<infer U> ? U : never;
|
|
24
|
+
export declare class MidnightBech32m {
|
|
25
|
+
static readonly prefix = "mn";
|
|
26
|
+
static encode<T extends HasCodec<T>>(networkId: NetworkId, item: T): MidnightBech32m;
|
|
27
|
+
static validateSegment(segmentName: string, segment: string): void;
|
|
28
|
+
static parse(bech32string: string): MidnightBech32m;
|
|
29
|
+
readonly type: string;
|
|
30
|
+
readonly network: NetworkId;
|
|
31
|
+
readonly data: Buffer;
|
|
32
|
+
constructor(type: string, network: NetworkId, data: Buffer);
|
|
33
|
+
decode<TClass extends HasCodec<any>>(tclass: TClass, networkId: NetworkId): CodecTarget<TClass>;
|
|
34
|
+
asString(): string;
|
|
35
|
+
toString(): string;
|
|
36
|
+
}
|
|
37
|
+
export declare class Bech32mCodec<T> {
|
|
38
|
+
readonly type: string;
|
|
39
|
+
readonly dataToBytes: (data: T) => Buffer;
|
|
40
|
+
readonly dataFromBytes: (bytes: Buffer) => T;
|
|
41
|
+
constructor(type: string, dataToBytes: (data: T) => Buffer, dataFromBytes: (bytes: Buffer) => T);
|
|
42
|
+
encode(networkId: NetworkId, data: T): MidnightBech32m;
|
|
43
|
+
decode(networkId: NetworkId, repr: MidnightBech32m): T;
|
|
44
|
+
static createContext(networkId: NetworkId): FormatContext;
|
|
45
|
+
}
|
|
46
|
+
export declare class ShieldedAddress {
|
|
47
|
+
static readonly codec: Bech32mCodec<ShieldedAddress>;
|
|
48
|
+
static readonly [Bech32mSymbol]: Bech32mCodec<ShieldedAddress>;
|
|
49
|
+
readonly [Bech32mSymbol]: Bech32mCodec<ShieldedAddress>;
|
|
50
|
+
readonly coinPublicKey: ShieldedCoinPublicKey;
|
|
51
|
+
readonly encryptionPublicKey: ShieldedEncryptionPublicKey;
|
|
52
|
+
constructor(coinPublicKey: ShieldedCoinPublicKey, encryptionPublicKey: ShieldedEncryptionPublicKey);
|
|
53
|
+
coinPublicKeyString(): string;
|
|
54
|
+
encryptionPublicKeyString(): string;
|
|
55
|
+
equals(other: ShieldedAddress): boolean;
|
|
56
|
+
}
|
|
57
|
+
export declare class ShieldedEncryptionSecretKey {
|
|
58
|
+
static readonly codec: Bech32mCodec<ShieldedEncryptionSecretKey>;
|
|
59
|
+
readonly zswap: EncryptionSecretKey;
|
|
60
|
+
constructor(zswap: EncryptionSecretKey);
|
|
61
|
+
}
|
|
62
|
+
export declare class ShieldedCoinPublicKey {
|
|
63
|
+
static readonly keyLength = 32;
|
|
64
|
+
static readonly codec: Bech32mCodec<ShieldedCoinPublicKey>;
|
|
65
|
+
static fromHexString(hexString: string): ShieldedCoinPublicKey;
|
|
66
|
+
readonly data: Buffer;
|
|
67
|
+
constructor(data: Buffer);
|
|
68
|
+
toHexString(): string;
|
|
69
|
+
equals(other: string): boolean;
|
|
70
|
+
equals(other: ShieldedCoinPublicKey): boolean;
|
|
71
|
+
}
|
|
72
|
+
export declare class ShieldedEncryptionPublicKey {
|
|
73
|
+
static readonly keyLength = 32;
|
|
74
|
+
static readonly codec: Bech32mCodec<ShieldedEncryptionPublicKey>;
|
|
75
|
+
static fromHexString(hexString: string): ShieldedEncryptionPublicKey;
|
|
76
|
+
readonly data: Buffer;
|
|
77
|
+
constructor(data: Buffer);
|
|
78
|
+
toHexString(): string;
|
|
79
|
+
equals(other: string): boolean;
|
|
80
|
+
equals(other: ShieldedEncryptionPublicKey): boolean;
|
|
81
|
+
}
|
|
82
|
+
export declare class UnshieldedAddress {
|
|
83
|
+
readonly data: Buffer;
|
|
84
|
+
static readonly keyLength = 32;
|
|
85
|
+
static readonly codec: Bech32mCodec<UnshieldedAddress>;
|
|
86
|
+
static readonly [Bech32mSymbol]: Bech32mCodec<UnshieldedAddress>;
|
|
87
|
+
readonly [Bech32mSymbol]: Bech32mCodec<UnshieldedAddress>;
|
|
88
|
+
constructor(data: Buffer);
|
|
89
|
+
get hexString(): UserAddress;
|
|
90
|
+
equals(other: string): boolean;
|
|
91
|
+
equals(other: UnshieldedAddress): boolean;
|
|
92
|
+
}
|
|
93
|
+
export declare class DustAddress {
|
|
94
|
+
readonly data: bigint;
|
|
95
|
+
static readonly codec: Bech32mCodec<DustAddress>;
|
|
96
|
+
static readonly [Bech32mSymbol]: Bech32mCodec<DustAddress>;
|
|
97
|
+
readonly [Bech32mSymbol]: Bech32mCodec<DustAddress>;
|
|
98
|
+
static readonly encodePublicKey: (networkId: string, publicKey: DustPublicKey) => string;
|
|
99
|
+
constructor(data: bigint);
|
|
100
|
+
serialize(): Buffer;
|
|
101
|
+
equals(other: bigint): boolean;
|
|
102
|
+
equals(other: DustAddress): boolean;
|
|
103
|
+
}
|
|
104
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
// This file is part of MIDNIGHT-WALLET-SDK.
|
|
2
|
+
// Copyright (C) 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-v8';
|
|
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
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@midnightntwrk/wallet-sdk-address-format",
|
|
3
|
+
"version": "3.1.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"author": "Midnight Foundation",
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"registry": "https://registry.npmjs.org/",
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist/"
|
|
16
|
+
],
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/midnightntwrk/midnight-wallet.git",
|
|
20
|
+
"directory": "packages/address-format"
|
|
21
|
+
},
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"import": "./dist/index.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@midnight-ntwrk/ledger-v8": "^8.1.0",
|
|
30
|
+
"@scure/base": "^2.0.0",
|
|
31
|
+
"@subsquid/scale-codec": "^4.0.1"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"typecheck": "tsc -b ./tsconfig.json --noEmit --force",
|
|
35
|
+
"test": "vitest run",
|
|
36
|
+
"lint": "eslint --max-warnings 0",
|
|
37
|
+
"format": "prettier --write \"**/*.{ts,js,json,yaml,yml,md}\"",
|
|
38
|
+
"format:check": "prettier --check \"**/*.{ts,js,json,yaml,yml,md}\"",
|
|
39
|
+
"dist": "tsc -b ./tsconfig.build.json",
|
|
40
|
+
"dist:publish": "tsc -b ./tsconfig.publish.json",
|
|
41
|
+
"clean": "rimraf --glob dist 'tsconfig.*.tsbuildinfo' && date +%s > .clean-timestamp",
|
|
42
|
+
"publint": "publint --strict"
|
|
43
|
+
}
|
|
44
|
+
}
|