@no-witness-labs/midday-sdk 0.2.0 → 0.2.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 +145 -78
- package/dist/Client.d.ts +542 -70
- package/dist/Client.d.ts.map +1 -1
- package/dist/Client.js +608 -143
- package/dist/Client.js.map +1 -1
- package/dist/Config.d.ts +83 -1
- package/dist/Config.d.ts.map +1 -1
- package/dist/Config.js +72 -15
- package/dist/Config.js.map +1 -1
- package/dist/Providers.d.ts +99 -9
- package/dist/Providers.d.ts.map +1 -1
- package/dist/Providers.js +142 -39
- package/dist/Providers.js.map +1 -1
- package/dist/Wallet.d.ts +88 -1
- package/dist/Wallet.d.ts.map +1 -1
- package/dist/Wallet.js +162 -51
- package/dist/Wallet.js.map +1 -1
- package/dist/index.d.ts +63 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +68 -4
- package/dist/index.js.map +1 -1
- package/dist/providers/HttpZkConfigProvider.d.ts +229 -0
- package/dist/providers/HttpZkConfigProvider.d.ts.map +1 -0
- package/dist/providers/HttpZkConfigProvider.js +275 -0
- package/dist/providers/HttpZkConfigProvider.js.map +1 -0
- package/dist/providers/IndexedDBPrivateStateProvider.d.ts +270 -0
- package/dist/providers/IndexedDBPrivateStateProvider.d.ts.map +1 -0
- package/dist/providers/IndexedDBPrivateStateProvider.js +513 -0
- package/dist/providers/IndexedDBPrivateStateProvider.js.map +1 -0
- package/dist/providers/errors.d.ts +50 -0
- package/dist/providers/errors.d.ts.map +1 -0
- package/dist/providers/errors.js +32 -0
- package/dist/providers/errors.js.map +1 -0
- package/dist/sdk/Type.d.ts +91 -0
- package/dist/sdk/Type.d.ts.map +1 -0
- package/dist/sdk/Type.js +8 -0
- package/dist/sdk/Type.js.map +1 -0
- package/dist/utils/address.d.ts +56 -0
- package/dist/utils/address.d.ts.map +1 -0
- package/dist/utils/address.js +135 -0
- package/dist/utils/address.js.map +1 -0
- package/dist/utils/coin.d.ts +55 -0
- package/dist/utils/coin.d.ts.map +1 -0
- package/dist/utils/coin.js +84 -0
- package/dist/utils/coin.js.map +1 -0
- package/dist/utils/effect-runtime.d.ts +66 -0
- package/dist/utils/effect-runtime.d.ts.map +1 -0
- package/dist/utils/effect-runtime.js +147 -0
- package/dist/utils/effect-runtime.js.map +1 -0
- package/dist/utils/hex.d.ts +62 -0
- package/dist/utils/hex.d.ts.map +1 -0
- package/dist/utils/hex.js +93 -0
- package/dist/utils/hex.js.map +1 -0
- package/dist/wallet/connector.d.ts +191 -0
- package/dist/wallet/connector.d.ts.map +1 -0
- package/dist/wallet/connector.js +183 -0
- package/dist/wallet/connector.js.map +1 -0
- package/dist/wallet/errors.d.ts +22 -0
- package/dist/wallet/errors.d.ts.map +1 -0
- package/dist/wallet/errors.js +16 -0
- package/dist/wallet/errors.js.map +1 -0
- package/dist/wallet/provider.d.ts +102 -0
- package/dist/wallet/provider.d.ts.map +1 -0
- package/dist/wallet/provider.js +139 -0
- package/dist/wallet/provider.js.map +1 -0
- package/package.json +10 -5
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type helpers for Effect-to-Promise API conversion.
|
|
3
|
+
*
|
|
4
|
+
* @since 0.3.0
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import type * as Effect from 'effect/Effect';
|
|
8
|
+
/**
|
|
9
|
+
* Utility to force TypeScript to expand and display computed types.
|
|
10
|
+
* Improves IDE hover display for complex mapped types.
|
|
11
|
+
*/
|
|
12
|
+
type Expand<T> = T extends (...args: infer A) => infer R ? (...args: A) => R : T extends object ? {
|
|
13
|
+
[K in keyof T]: T[K];
|
|
14
|
+
} : T;
|
|
15
|
+
/**
|
|
16
|
+
* Converts an Effect type to a Promise type by extracting the success value.
|
|
17
|
+
* Handles both direct Effect types and functions returning Effects.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* type EffectType = Effect.Effect<string, Error>;
|
|
22
|
+
* type PromiseType = EffectToPromise<EffectType>; // Promise<string>
|
|
23
|
+
*
|
|
24
|
+
* type EffectFn = (x: number) => Effect.Effect<string, Error>;
|
|
25
|
+
* type PromiseFn = EffectToPromise<EffectFn>; // (x: number) => Promise<string>
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export type EffectToPromise<T> = T extends Effect.Effect<infer Return, infer _Error, infer _Context> ? Promise<Return> : T extends (...args: Array<any>) => Effect.Effect<infer Return, infer _Error, infer _Context> ? (...args: Parameters<T>) => Promise<Return> : never;
|
|
29
|
+
/**
|
|
30
|
+
* Converts an API interface with Effect-returning methods to Promise-returning methods.
|
|
31
|
+
*
|
|
32
|
+
* Used to create the Promise-based wrapper API from the Effect-based API.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* interface MyAPIEffect {
|
|
37
|
+
* readonly foo: () => Effect.Effect<string, Error>;
|
|
38
|
+
* readonly bar: (n: number) => Effect.Effect<boolean, Error>;
|
|
39
|
+
* }
|
|
40
|
+
*
|
|
41
|
+
* // Results in:
|
|
42
|
+
* // {
|
|
43
|
+
* // readonly foo: () => Promise<string>;
|
|
44
|
+
* // readonly bar: (n: number) => Promise<boolean>;
|
|
45
|
+
* // }
|
|
46
|
+
* type MyAPI = EffectToPromiseAPI<MyAPIEffect>;
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export type EffectToPromiseAPI<T> = Expand<{
|
|
50
|
+
readonly [K in keyof T]: EffectToPromise<T[K]>;
|
|
51
|
+
}>;
|
|
52
|
+
/**
|
|
53
|
+
* Selective Promise conversion - specify which Effects become Promises, rest become sync.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* interface MyAPIEffect {
|
|
58
|
+
* readonly asyncOp: () => Effect.Effect<string, Error>;
|
|
59
|
+
* readonly syncOp: () => Effect.Effect<number, Error>;
|
|
60
|
+
* }
|
|
61
|
+
*
|
|
62
|
+
* // asyncOp becomes Promise, syncOp becomes sync (returns number directly)
|
|
63
|
+
* type MyAPI = SelectivePromiseAPI<MyAPIEffect, 'asyncOp'>;
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export type SelectivePromiseAPI<T, PromiseKeys extends keyof T = never> = {
|
|
67
|
+
readonly [K in PromiseKeys]: EffectToPromise<T[K]>;
|
|
68
|
+
} & {
|
|
69
|
+
readonly [K in Exclude<keyof T, PromiseKeys>]: T[K] extends Effect.Effect<infer Return, any, any> ? Return : T[K];
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Selective Sync conversion - specify which Effects become sync, rest become Promises.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* interface MyAPIEffect {
|
|
77
|
+
* readonly asyncOp: () => Effect.Effect<string, Error>;
|
|
78
|
+
* readonly syncOp: () => Effect.Effect<number, Error>;
|
|
79
|
+
* }
|
|
80
|
+
*
|
|
81
|
+
* // syncOp becomes sync (returns number), asyncOp becomes Promise
|
|
82
|
+
* type MyAPI = SelectiveSyncAPI<MyAPIEffect, 'syncOp'>;
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export type SelectiveSyncAPI<T, SyncKeys extends keyof T = never> = {
|
|
86
|
+
readonly [K in SyncKeys]: T[K] extends Effect.Effect<infer Return, any, any> ? Return : T[K];
|
|
87
|
+
} & {
|
|
88
|
+
readonly [K in Exclude<keyof T, SyncKeys>]: EffectToPromise<T[K]>;
|
|
89
|
+
};
|
|
90
|
+
export {};
|
|
91
|
+
//# sourceMappingURL=Type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Type.d.ts","sourceRoot":"","sources":["../../src/sdk/Type.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,KAAK,MAAM,MAAM,eAAe,CAAC;AAE7C;;;GAGG;AACH,KAAK,MAAM,CA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MAAM,MAAM,EAAE,GAAG,EAAE,GAAG,CAAC,GAAG,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;CAClH,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,gBAAgB,CAAC,CAAC,EAAE,QAAQ,SAAS,MAAM,CAAC,GAAG,KAAK,IAAI;IAGlE,QAAQ,EAAE,CAAC,IAAI,QAAQ,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,CAAC,MAAM,MAAM,EAAE,GAAG,EAAE,GAAG,CAAC,GAAG,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;CAC7F,GAAG;IAEF,QAAQ,EAAE,CAAC,IAAI,OAAO,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC,GAAG,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAClE,CAAC"}
|
package/dist/sdk/Type.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Type.js","sourceRoot":"","sources":["../../src/sdk/Type.ts"],"names":[],"mappings":"AAAA;;;;;GAKG"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Address parsing utilities for Midnight Network.
|
|
3
|
+
*
|
|
4
|
+
* Supports multiple address formats: Bech32m, hex, and base64.
|
|
5
|
+
*
|
|
6
|
+
* @since 0.2.0
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Parsed shielded address containing coin and encryption public keys.
|
|
11
|
+
*/
|
|
12
|
+
export interface ParsedAddress {
|
|
13
|
+
/** Coin public key as 64-character hex string */
|
|
14
|
+
coinPublicKey: string;
|
|
15
|
+
/** Encryption public key as 64-character hex string */
|
|
16
|
+
encryptionPublicKey: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Parse a shielded address from various formats.
|
|
20
|
+
*
|
|
21
|
+
* Supports:
|
|
22
|
+
* - Bech32m format (midnightxxxx...)
|
|
23
|
+
* - Hex string (128 characters = 64 bytes for both keys)
|
|
24
|
+
* - Base64 encoded bytes
|
|
25
|
+
*
|
|
26
|
+
* @param address - Address in any supported format
|
|
27
|
+
* @returns Parsed address with hex-encoded public keys
|
|
28
|
+
* @throws Error if address format is invalid
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* // Parse Bech32m address
|
|
33
|
+
* const parsed = parseShieldedAddress('midnight1...');
|
|
34
|
+
*
|
|
35
|
+
* // Parse hex address (64 bytes = 128 hex chars)
|
|
36
|
+
* const parsed = parseShieldedAddress('aabbcc...');
|
|
37
|
+
*
|
|
38
|
+
* console.log(parsed.coinPublicKey); // 64-char hex
|
|
39
|
+
* console.log(parsed.encryptionPublicKey); // 64-char hex
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare function parseShieldedAddress(address: string): ParsedAddress;
|
|
43
|
+
/**
|
|
44
|
+
* Convert a hex public key to Uint8Array for contract calls.
|
|
45
|
+
*
|
|
46
|
+
* @param hex - 64-character hex string (32 bytes)
|
|
47
|
+
* @returns Uint8Array of 32 bytes
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* const keyBytes = hexToPublicKey(parsed.coinPublicKey);
|
|
52
|
+
* // Use in contract calls
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export declare function hexToPublicKey(hex: string): Uint8Array;
|
|
56
|
+
//# sourceMappingURL=address.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../../src/utils/address.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,iDAAiD;IACjD,aAAa,EAAE,MAAM,CAAC;IACtB,uDAAuD;IACvD,mBAAmB,EAAE,MAAM,CAAC;CAC7B;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,aAAa,CA2BnE;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAMtD"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Address parsing utilities for Midnight Network.
|
|
3
|
+
*
|
|
4
|
+
* Supports multiple address formats: Bech32m, hex, and base64.
|
|
5
|
+
*
|
|
6
|
+
* @since 0.2.0
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import { hexToBytes, bytesToHex, base64ToBytes } from './hex.js';
|
|
10
|
+
/**
|
|
11
|
+
* Parse a shielded address from various formats.
|
|
12
|
+
*
|
|
13
|
+
* Supports:
|
|
14
|
+
* - Bech32m format (midnightxxxx...)
|
|
15
|
+
* - Hex string (128 characters = 64 bytes for both keys)
|
|
16
|
+
* - Base64 encoded bytes
|
|
17
|
+
*
|
|
18
|
+
* @param address - Address in any supported format
|
|
19
|
+
* @returns Parsed address with hex-encoded public keys
|
|
20
|
+
* @throws Error if address format is invalid
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* // Parse Bech32m address
|
|
25
|
+
* const parsed = parseShieldedAddress('midnight1...');
|
|
26
|
+
*
|
|
27
|
+
* // Parse hex address (64 bytes = 128 hex chars)
|
|
28
|
+
* const parsed = parseShieldedAddress('aabbcc...');
|
|
29
|
+
*
|
|
30
|
+
* console.log(parsed.coinPublicKey); // 64-char hex
|
|
31
|
+
* console.log(parsed.encryptionPublicKey); // 64-char hex
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export function parseShieldedAddress(address) {
|
|
35
|
+
let bytes;
|
|
36
|
+
// Try to detect format and decode
|
|
37
|
+
if (address.startsWith('midnight')) {
|
|
38
|
+
// Bech32m format - use the ledger's built-in parser
|
|
39
|
+
// The address contains the encoded public keys
|
|
40
|
+
bytes = decodeBech32m(address);
|
|
41
|
+
}
|
|
42
|
+
else if (/^[0-9a-fA-F]+$/.test(address)) {
|
|
43
|
+
// Hex format
|
|
44
|
+
bytes = hexToBytes(address);
|
|
45
|
+
}
|
|
46
|
+
else if (/^[A-Za-z0-9+/=]+$/.test(address)) {
|
|
47
|
+
// Base64 format
|
|
48
|
+
bytes = base64ToBytes(address);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
throw new Error(`Invalid address format: ${address.slice(0, 20)}...`);
|
|
52
|
+
}
|
|
53
|
+
// Shielded address should be 64 bytes (32 bytes each for coin and encryption keys)
|
|
54
|
+
if (bytes.length !== 64) {
|
|
55
|
+
throw new Error(`Invalid address length: expected 64 bytes, got ${bytes.length}`);
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
coinPublicKey: bytesToHex(bytes.slice(0, 32)),
|
|
59
|
+
encryptionPublicKey: bytesToHex(bytes.slice(32, 64)),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Convert a hex public key to Uint8Array for contract calls.
|
|
64
|
+
*
|
|
65
|
+
* @param hex - 64-character hex string (32 bytes)
|
|
66
|
+
* @returns Uint8Array of 32 bytes
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* const keyBytes = hexToPublicKey(parsed.coinPublicKey);
|
|
71
|
+
* // Use in contract calls
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export function hexToPublicKey(hex) {
|
|
75
|
+
const bytes = hexToBytes(hex);
|
|
76
|
+
if (bytes.length !== 32) {
|
|
77
|
+
throw new Error(`Invalid public key length: expected 32 bytes, got ${bytes.length}`);
|
|
78
|
+
}
|
|
79
|
+
return bytes;
|
|
80
|
+
}
|
|
81
|
+
// Bech32m character set
|
|
82
|
+
const BECH32M_CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l';
|
|
83
|
+
/**
|
|
84
|
+
* Decode a Bech32m address to bytes.
|
|
85
|
+
* This is a simplified implementation for Midnight addresses.
|
|
86
|
+
*/
|
|
87
|
+
function decodeBech32m(bech32) {
|
|
88
|
+
const lower = bech32.toLowerCase();
|
|
89
|
+
const sepIndex = lower.lastIndexOf('1');
|
|
90
|
+
if (sepIndex < 1) {
|
|
91
|
+
throw new Error('Invalid bech32m: no separator found');
|
|
92
|
+
}
|
|
93
|
+
const data = lower.slice(sepIndex + 1);
|
|
94
|
+
// Convert bech32 characters to 5-bit values
|
|
95
|
+
const values = [];
|
|
96
|
+
for (const char of data) {
|
|
97
|
+
const value = BECH32M_CHARSET.indexOf(char);
|
|
98
|
+
if (value === -1) {
|
|
99
|
+
throw new Error(`Invalid bech32m character: ${char}`);
|
|
100
|
+
}
|
|
101
|
+
values.push(value);
|
|
102
|
+
}
|
|
103
|
+
// Remove checksum (last 6 values)
|
|
104
|
+
const dataValues = values.slice(0, -6);
|
|
105
|
+
// Convert from 5-bit to 8-bit (skip first value which is witness version)
|
|
106
|
+
const bytes = convertBits(dataValues.slice(1), 5, 8, false);
|
|
107
|
+
return new Uint8Array(bytes);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Convert between bit sizes (used in bech32 encoding).
|
|
111
|
+
*/
|
|
112
|
+
function convertBits(data, fromBits, toBits, pad) {
|
|
113
|
+
let acc = 0;
|
|
114
|
+
let bits = 0;
|
|
115
|
+
const result = [];
|
|
116
|
+
const maxv = (1 << toBits) - 1;
|
|
117
|
+
for (const value of data) {
|
|
118
|
+
acc = (acc << fromBits) | value;
|
|
119
|
+
bits += fromBits;
|
|
120
|
+
while (bits >= toBits) {
|
|
121
|
+
bits -= toBits;
|
|
122
|
+
result.push((acc >> bits) & maxv);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (pad) {
|
|
126
|
+
if (bits > 0) {
|
|
127
|
+
result.push((acc << (toBits - bits)) & maxv);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
else if (bits >= fromBits || ((acc << (toBits - bits)) & maxv) !== 0) {
|
|
131
|
+
// Check for invalid padding
|
|
132
|
+
}
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
//# sourceMappingURL=address.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"address.js","sourceRoot":"","sources":["../../src/utils/address.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAYjE;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,oBAAoB,CAAC,OAAe;IAClD,IAAI,KAAiB,CAAC;IAEtB,kCAAkC;IAClC,IAAI,OAAO,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;QACnC,oDAAoD;QACpD,+CAA+C;QAC/C,KAAK,GAAG,aAAa,CAAC,OAAO,CAAC,CAAC;IACjC,CAAC;SAAM,IAAI,gBAAgB,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;QAC1C,aAAa;QACb,KAAK,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC;IAC9B,CAAC;SAAM,IAAI,mBAAmB,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;QAC7C,gBAAgB;QAChB,KAAK,GAAG,aAAa,CAAC,OAAO,CAAC,CAAC;IACjC,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,KAAK,CAAC,2BAA2B,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC;IACxE,CAAC;IAED,mFAAmF;IACnF,IAAI,KAAK,CAAC,MAAM,KAAK,EAAE,EAAE,CAAC;QACxB,MAAM,IAAI,KAAK,CAAC,kDAAkD,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC;IACpF,CAAC;IAED,OAAO;QACL,aAAa,EAAE,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,E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
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Coin creation utilities for Midnight Network.
|
|
3
|
+
*
|
|
4
|
+
* Creates coin structures for contract calls with nonce, color, and value.
|
|
5
|
+
*
|
|
6
|
+
* @since 0.2.0
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Coin information containing nonce, color, and value.
|
|
11
|
+
*/
|
|
12
|
+
export interface CoinInfo {
|
|
13
|
+
/** Random nonce (32 bytes) */
|
|
14
|
+
nonce: Uint8Array;
|
|
15
|
+
/** Token color/type (32 bytes) */
|
|
16
|
+
color: Uint8Array;
|
|
17
|
+
/** Token value */
|
|
18
|
+
value: bigint;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Get the native token color.
|
|
22
|
+
*
|
|
23
|
+
* @returns 32-byte Uint8Array representing the native token color
|
|
24
|
+
*/
|
|
25
|
+
export declare function getNativeTokenColor(): Uint8Array;
|
|
26
|
+
/**
|
|
27
|
+
* Create a new coin with random nonce and native token color.
|
|
28
|
+
*
|
|
29
|
+
* @param amount - Token amount (as bigint or number)
|
|
30
|
+
* @returns CoinInfo with nonce, color, and value
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* // Create a coin with 1 TNIGHT (10^6 units)
|
|
35
|
+
* const coin = createCoin(1_000_000n);
|
|
36
|
+
*
|
|
37
|
+
* // Use in contract deployment
|
|
38
|
+
* await contract.deploy({ coin });
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export declare function createCoin(amount: bigint | number): CoinInfo;
|
|
42
|
+
/**
|
|
43
|
+
* Create a coin with a custom token color.
|
|
44
|
+
*
|
|
45
|
+
* @param amount - Token amount
|
|
46
|
+
* @param color - Token color as Uint8Array (32 bytes)
|
|
47
|
+
* @returns CoinInfo with nonce, color, and value
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* const customCoin = createCustomCoin(100n, myTokenColor);
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare function createCustomCoin(amount: bigint | number, color: Uint8Array): CoinInfo;
|
|
55
|
+
//# sourceMappingURL=coin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coin.d.ts","sourceRoot":"","sources":["../../src/utils/coin.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH;;GAEG;AACH,MAAM,WAAW,QAAQ;IACvB,8BAA8B;IAC9B,KAAK,EAAE,UAAU,CAAC;IAClB,kCAAkC;IAClC,KAAK,EAAE,UAAU,CAAC;IAClB,kBAAkB;IAClB,KAAK,EAAE,MAAM,CAAC;CACf;AAeD;;;;GAIG;AACH,wBAAgB,mBAAmB,IAAI,UAAU,CAIhD;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,QAAQ,CAY5D;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,KAAK,EAAE,UAAU,GAAG,QAAQ,CAgBrF"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Coin creation utilities for Midnight Network.
|
|
3
|
+
*
|
|
4
|
+
* Creates coin structures for contract calls with nonce, color, and value.
|
|
5
|
+
*
|
|
6
|
+
* @since 0.2.0
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import { encodeRawTokenType, nativeToken } from '@midnight-ntwrk/ledger-v6';
|
|
10
|
+
/**
|
|
11
|
+
* Generate a cryptographically secure random nonce.
|
|
12
|
+
*
|
|
13
|
+
* Uses crypto.getRandomValues which is available in both browser and Node.js 18+.
|
|
14
|
+
*
|
|
15
|
+
* @returns Random 32-byte Uint8Array
|
|
16
|
+
*/
|
|
17
|
+
function generateNonce() {
|
|
18
|
+
const nonce = new Uint8Array(32);
|
|
19
|
+
crypto.getRandomValues(nonce);
|
|
20
|
+
return nonce;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Get the native token color.
|
|
24
|
+
*
|
|
25
|
+
* @returns 32-byte Uint8Array representing the native token color
|
|
26
|
+
*/
|
|
27
|
+
export function getNativeTokenColor() {
|
|
28
|
+
const rawType = nativeToken().raw;
|
|
29
|
+
const encoded = encodeRawTokenType(rawType);
|
|
30
|
+
return new Uint8Array(encoded);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Create a new coin with random nonce and native token color.
|
|
34
|
+
*
|
|
35
|
+
* @param amount - Token amount (as bigint or number)
|
|
36
|
+
* @returns CoinInfo with nonce, color, and value
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* // Create a coin with 1 TNIGHT (10^6 units)
|
|
41
|
+
* const coin = createCoin(1_000_000n);
|
|
42
|
+
*
|
|
43
|
+
* // Use in contract deployment
|
|
44
|
+
* await contract.deploy({ coin });
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export function createCoin(amount) {
|
|
48
|
+
const value = typeof amount === 'number' ? BigInt(amount) : amount;
|
|
49
|
+
if (value < 0n) {
|
|
50
|
+
throw new Error('Coin value cannot be negative');
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
nonce: generateNonce(),
|
|
54
|
+
color: getNativeTokenColor(),
|
|
55
|
+
value,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Create a coin with a custom token color.
|
|
60
|
+
*
|
|
61
|
+
* @param amount - Token amount
|
|
62
|
+
* @param color - Token color as Uint8Array (32 bytes)
|
|
63
|
+
* @returns CoinInfo with nonce, color, and value
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* const customCoin = createCustomCoin(100n, myTokenColor);
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export function createCustomCoin(amount, color) {
|
|
71
|
+
if (color.length !== 32) {
|
|
72
|
+
throw new Error(`Invalid color length: expected 32 bytes, got ${color.length}`);
|
|
73
|
+
}
|
|
74
|
+
const value = typeof amount === 'number' ? BigInt(amount) : amount;
|
|
75
|
+
if (value < 0n) {
|
|
76
|
+
throw new Error('Coin value cannot be negative');
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
nonce: generateNonce(),
|
|
80
|
+
color,
|
|
81
|
+
value,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=coin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coin.js","sourceRoot":"","sources":["../../src/utils/coin.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAc5E;;;;;;GAMG;AACH,SAAS,aAAa;IACpB,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC;IACjC,MAAM,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;IAC9B,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,mBAAmB;IACjC,MAAM,OAAO,GAAG,WAAW,EAAE,CAAC,GAAG,CAAC;IAClC,MAAM,OAAO,GAAG,kBAAkB,CAAC,OAAO,CAAC,CAAC;IAC5C,OAAO,IAAI,UAAU,CAAC,OAAO,CAAC,CAAC;AACjC,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,UAAU,CAAC,MAAuB;IAChD,MAAM,KAAK,GAAG,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;IAEnE,IAAI,KAAK,GAAG,EAAE,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;IACnD,CAAC;IAED,OAAO;QACL,KAAK,EAAE,aAAa,EAAE;QACtB,KAAK,EAAE,mBAAmB,EAAE;QAC5B,KAAK;KACN,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,gBAAgB,CAAC,MAAuB,EAAE,KAAiB;IACzE,IAAI,KAAK,CAAC,MAAM,KAAK,EAAE,EAAE,CAAC;QACxB,MAAM,IAAI,KAAK,CAAC,gDAAgD,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC;IAClF,CAAC;IAED,MAAM,KAAK,GAAG,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;IAEnE,IAAI,KAAK,GAAG,EAAE,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;IACnD,CAAC;IAED,OAAO;QACL,KAAK,EAAE,aAAa,EAAE;QACtB,KAAK;QACL,KAAK;KACN,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Effect runtime utilities for executing Effect programs.
|
|
3
|
+
*
|
|
4
|
+
* @since 0.3.0
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { Effect } from 'effect';
|
|
8
|
+
/**
|
|
9
|
+
* Run an Effect synchronously with clean error handling.
|
|
10
|
+
*
|
|
11
|
+
* - Executes the Effect using Effect.runSyncExit
|
|
12
|
+
* - On failure, extracts the error from the Exit and cleans stack traces
|
|
13
|
+
* - Removes Effect.ts internal stack frames for cleaner error messages
|
|
14
|
+
* - Throws the cleaned error for standard error handling
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { Effect } from 'effect';
|
|
19
|
+
* import { runEffect } from '@no-witness-labs/midday-sdk';
|
|
20
|
+
*
|
|
21
|
+
* const myEffect = Effect.succeed(42);
|
|
22
|
+
*
|
|
23
|
+
* try {
|
|
24
|
+
* const result = runEffect(myEffect);
|
|
25
|
+
* console.log(result);
|
|
26
|
+
* } catch (error) {
|
|
27
|
+
* // Error with clean stack trace, no Effect.ts internals
|
|
28
|
+
* console.error(error);
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @since 0.3.0
|
|
33
|
+
* @category utilities
|
|
34
|
+
*/
|
|
35
|
+
export declare function runEffect<A, E>(effect: Effect.Effect<A, E>): A;
|
|
36
|
+
/**
|
|
37
|
+
* Run an Effect asynchronously and convert it to a Promise with clean error handling.
|
|
38
|
+
*
|
|
39
|
+
* - Executes the Effect using Effect.runPromiseExit
|
|
40
|
+
* - On failure, extracts the error from the Exit and cleans stack traces
|
|
41
|
+
* - Removes Effect.ts internal stack frames for cleaner error messages
|
|
42
|
+
* - Throws the cleaned error for standard Promise error handling
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* import { Effect } from 'effect';
|
|
47
|
+
* import { runEffectPromise } from '@no-witness-labs/midday-sdk';
|
|
48
|
+
*
|
|
49
|
+
* const myEffect = Effect.succeed(42);
|
|
50
|
+
*
|
|
51
|
+
* async function example() {
|
|
52
|
+
* try {
|
|
53
|
+
* const result = await runEffectPromise(myEffect);
|
|
54
|
+
* console.log(result);
|
|
55
|
+
* } catch (error) {
|
|
56
|
+
* // Error with clean stack trace, no Effect.ts internals
|
|
57
|
+
* console.error(error);
|
|
58
|
+
* }
|
|
59
|
+
* }
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @since 0.3.0
|
|
63
|
+
* @category utilities
|
|
64
|
+
*/
|
|
65
|
+
export declare function runEffectPromise<A, E>(effect: Effect.Effect<A, E>): Promise<A>;
|
|
66
|
+
//# sourceMappingURL=effect-runtime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"effect-runtime.d.ts","sourceRoot":"","sources":["../../src/utils/effect-runtime.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAS,MAAM,EAAQ,MAAM,QAAQ,CAAC;AAsE7C;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAc9D;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAsB,gBAAgB,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAcpF"}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Effect runtime utilities for executing Effect programs.
|
|
3
|
+
*
|
|
4
|
+
* @since 0.3.0
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { Cause, Effect, Exit } from 'effect';
|
|
8
|
+
/**
|
|
9
|
+
* Patterns to filter from stack traces - Effect.ts internal implementation details
|
|
10
|
+
*/
|
|
11
|
+
const EFFECT_INTERNAL_PATTERNS = [
|
|
12
|
+
/node_modules\/.pnpm\/effect@.*\/node_modules\/effect\//,
|
|
13
|
+
/at FiberRuntime\./,
|
|
14
|
+
/at EffectPrimitive\./,
|
|
15
|
+
/at Object\.Iterator/,
|
|
16
|
+
/at runLoop/,
|
|
17
|
+
/at evaluateEffect/,
|
|
18
|
+
/at body \(/,
|
|
19
|
+
/effect_instruction_i\d+/,
|
|
20
|
+
/at pipeArguments/,
|
|
21
|
+
/at pipe \(/,
|
|
22
|
+
/at Arguments\./,
|
|
23
|
+
/at Module\./,
|
|
24
|
+
/at issue \(/,
|
|
25
|
+
/at \.\.\.$/, // Lines like "... 7 lines matching cause stack trace ..."
|
|
26
|
+
];
|
|
27
|
+
/**
|
|
28
|
+
* Clean a single error's stack trace by removing Effect.ts internals
|
|
29
|
+
*/
|
|
30
|
+
function cleanStackTrace(stack) {
|
|
31
|
+
if (!stack)
|
|
32
|
+
return '';
|
|
33
|
+
const lines = stack.split('\n');
|
|
34
|
+
const cleaned = lines.filter((line) => {
|
|
35
|
+
// Keep the error message line (first line)
|
|
36
|
+
if (!line.trim().startsWith('at '))
|
|
37
|
+
return true;
|
|
38
|
+
// Filter out Effect.ts internal lines
|
|
39
|
+
return !EFFECT_INTERNAL_PATTERNS.some((pattern) => pattern.test(line));
|
|
40
|
+
});
|
|
41
|
+
return cleaned.join('\n');
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Recursively clean error chain (error and all causes)
|
|
45
|
+
*/
|
|
46
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
47
|
+
function cleanErrorChain(error) {
|
|
48
|
+
if (!error)
|
|
49
|
+
return error;
|
|
50
|
+
// Clean current error's stack
|
|
51
|
+
if (error.stack) {
|
|
52
|
+
error.stack = cleanStackTrace(error.stack);
|
|
53
|
+
}
|
|
54
|
+
// Recursively clean cause chain
|
|
55
|
+
if (error.cause) {
|
|
56
|
+
error.cause = cleanErrorChain(error.cause);
|
|
57
|
+
}
|
|
58
|
+
// Handle Effect.ts internal cause field
|
|
59
|
+
if (error[Symbol.for('effect/Runtime/FiberFailure/Cause')]) {
|
|
60
|
+
const cause = error[Symbol.for('effect/Runtime/FiberFailure/Cause')];
|
|
61
|
+
if (cause && typeof cause === 'object') {
|
|
62
|
+
if (cause.error) {
|
|
63
|
+
cause.error = cleanErrorChain(cause.error);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return error;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Run an Effect synchronously with clean error handling.
|
|
71
|
+
*
|
|
72
|
+
* - Executes the Effect using Effect.runSyncExit
|
|
73
|
+
* - On failure, extracts the error from the Exit and cleans stack traces
|
|
74
|
+
* - Removes Effect.ts internal stack frames for cleaner error messages
|
|
75
|
+
* - Throws the cleaned error for standard error handling
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* import { Effect } from 'effect';
|
|
80
|
+
* import { runEffect } from '@no-witness-labs/midday-sdk';
|
|
81
|
+
*
|
|
82
|
+
* const myEffect = Effect.succeed(42);
|
|
83
|
+
*
|
|
84
|
+
* try {
|
|
85
|
+
* const result = runEffect(myEffect);
|
|
86
|
+
* console.log(result);
|
|
87
|
+
* } catch (error) {
|
|
88
|
+
* // Error with clean stack trace, no Effect.ts internals
|
|
89
|
+
* console.error(error);
|
|
90
|
+
* }
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
* @since 0.3.0
|
|
94
|
+
* @category utilities
|
|
95
|
+
*/
|
|
96
|
+
export function runEffect(effect) {
|
|
97
|
+
const exit = Effect.runSyncExit(effect);
|
|
98
|
+
if (Exit.isFailure(exit)) {
|
|
99
|
+
// Extract the error from the failure
|
|
100
|
+
const error = Cause.squash(exit.cause);
|
|
101
|
+
// Clean the error's stack trace
|
|
102
|
+
const cleanedError = cleanErrorChain(error);
|
|
103
|
+
throw cleanedError;
|
|
104
|
+
}
|
|
105
|
+
return exit.value;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Run an Effect asynchronously and convert it to a Promise with clean error handling.
|
|
109
|
+
*
|
|
110
|
+
* - Executes the Effect using Effect.runPromiseExit
|
|
111
|
+
* - On failure, extracts the error from the Exit and cleans stack traces
|
|
112
|
+
* - Removes Effect.ts internal stack frames for cleaner error messages
|
|
113
|
+
* - Throws the cleaned error for standard Promise error handling
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```typescript
|
|
117
|
+
* import { Effect } from 'effect';
|
|
118
|
+
* import { runEffectPromise } from '@no-witness-labs/midday-sdk';
|
|
119
|
+
*
|
|
120
|
+
* const myEffect = Effect.succeed(42);
|
|
121
|
+
*
|
|
122
|
+
* async function example() {
|
|
123
|
+
* try {
|
|
124
|
+
* const result = await runEffectPromise(myEffect);
|
|
125
|
+
* console.log(result);
|
|
126
|
+
* } catch (error) {
|
|
127
|
+
* // Error with clean stack trace, no Effect.ts internals
|
|
128
|
+
* console.error(error);
|
|
129
|
+
* }
|
|
130
|
+
* }
|
|
131
|
+
* ```
|
|
132
|
+
*
|
|
133
|
+
* @since 0.3.0
|
|
134
|
+
* @category utilities
|
|
135
|
+
*/
|
|
136
|
+
export async function runEffectPromise(effect) {
|
|
137
|
+
const exit = await Effect.runPromiseExit(effect);
|
|
138
|
+
if (Exit.isFailure(exit)) {
|
|
139
|
+
// Extract the error from the failure
|
|
140
|
+
const error = Cause.squash(exit.cause);
|
|
141
|
+
// Clean the error's stack trace
|
|
142
|
+
const cleanedError = cleanErrorChain(error);
|
|
143
|
+
throw cleanedError;
|
|
144
|
+
}
|
|
145
|
+
return exit.value;
|
|
146
|
+
}
|
|
147
|
+
//# sourceMappingURL=effect-runtime.js.map
|