@midnight-ntwrk/wallet-sdk-address-format 3.0.0-beta.9 → 3.0.0
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 +174 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -1,3 +1,175 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @midnight-ntwrk/wallet-sdk-address-format
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Bech32m address encoding and decoding for the Midnight network.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @midnight-ntwrk/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 '@midnight-ntwrk/wallet-sdk-address-format';
|
|
32
|
+
import type { NetworkId } from '@midnight-ntwrk/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 '@midnight-ntwrk/wallet-sdk-address-format';
|
|
68
|
+
import type { NetworkId } from '@midnight-ntwrk/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 '@midnight-ntwrk/wallet-sdk-address-format';
|
|
101
|
+
import type { NetworkId } from '@midnight-ntwrk/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
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DustPublicKey, EncryptionSecretKey, UserAddress } from '@midnight-ntwrk/ledger-
|
|
1
|
+
import { DustPublicKey, EncryptionSecretKey, UserAddress } from '@midnight-ntwrk/ledger-v7';
|
|
2
2
|
export declare const mainnet: unique symbol;
|
|
3
3
|
export type NetworkId = string | typeof mainnet;
|
|
4
4
|
export type FormatContext = {
|
package/dist/index.js
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
11
|
// See the License for the specific language governing permissions and
|
|
12
12
|
// limitations under the License.
|
|
13
|
-
import { EncryptionSecretKey } from '@midnight-ntwrk/ledger-
|
|
13
|
+
import { EncryptionSecretKey } from '@midnight-ntwrk/ledger-v7';
|
|
14
14
|
import { bech32m } from '@scure/base';
|
|
15
15
|
import * as subsquidScale from '@subsquid/scale-codec';
|
|
16
16
|
export const mainnet = Symbol('Mainnet');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midnight-ntwrk/wallet-sdk-address-format",
|
|
3
|
-
"version": "3.0.0
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -24,24 +24,25 @@
|
|
|
24
24
|
}
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@midnight-ntwrk/ledger-
|
|
27
|
+
"@midnight-ntwrk/ledger-v7": "7.0.0",
|
|
28
28
|
"@scure/base": "^1.2.6",
|
|
29
29
|
"@subsquid/scale-codec": "^4.0.1"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@types/node": "22.17.0",
|
|
33
33
|
"eslint": "^9.37.0",
|
|
34
|
+
"prettier": "^3.7.0",
|
|
34
35
|
"publint": "~0.3.14",
|
|
35
36
|
"rimraf": "^6.0.1",
|
|
36
37
|
"typescript": "^5.9.3",
|
|
37
|
-
"vitest": "^
|
|
38
|
+
"vitest": "^4.0.16"
|
|
38
39
|
},
|
|
39
40
|
"scripts": {
|
|
40
41
|
"typecheck": "tsc -b ./tsconfig.json --noEmit",
|
|
41
42
|
"test": "vitest run",
|
|
42
43
|
"lint": "eslint --max-warnings 0",
|
|
43
|
-
"format": "prettier --write \"**/*.{ts,js,json,yaml,yml}\"",
|
|
44
|
-
"format:check": "prettier --check \"**/*.{ts,js,json,yaml,yml}\"",
|
|
44
|
+
"format": "prettier --write \"**/*.{ts,js,json,yaml,yml,md}\"",
|
|
45
|
+
"format:check": "prettier --check \"**/*.{ts,js,json,yaml,yml,md}\"",
|
|
45
46
|
"dist": "tsc -b ./tsconfig.build.json",
|
|
46
47
|
"dist:publish": "tsc -b ./tsconfig.publish.json",
|
|
47
48
|
"clean": "rimraf --glob dist 'tsconfig.*.tsbuildinfo' && date +%s > .clean-timestamp",
|