@totemsdk/core 1.0.9 → 1.0.11
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/LICENSE +21 -0
- package/README.md +5 -5
- package/dist/Streamable.js +54 -31
- package/dist/adapters/index.js +18 -8
- package/dist/adapters/types.js +19 -8
- package/dist/binio.js +5 -1
- package/dist/bip39.js +24 -14
- package/dist/derive.js +11 -7
- package/dist/index.js +130 -22
- package/dist/javaStreamables.js +49 -30
- package/dist/lease/LeaseMonitor.js +7 -3
- package/dist/lease/LeaseStore.js +7 -3
- package/dist/lease/WatermarkStore.js +7 -3
- package/dist/lease/index.js +13 -4
- package/dist/lease-client.js +8 -3
- package/dist/minima32.js +22 -13
- package/dist/minimaWireSerializer.js +20 -14
- package/dist/mmr.js +35 -23
- package/dist/mx.js +10 -1
- package/dist/params.js +9 -5
- package/dist/script.js +12 -7
- package/dist/scripts/contract-helpers.js +37 -26
- package/dist/scripts/dapp-types.js +11 -6
- package/dist/scripts/index.js +38 -4
- package/dist/scripts/types.js +26 -12
- package/dist/scripts/witness-serializer.js +48 -30
- package/dist/transaction.js +55 -46
- package/dist/treekey.js +74 -58
- package/dist/tx/TransactionLifecycle.js +11 -5
- package/dist/tx/TransactionReceiptStore.js +7 -3
- package/dist/tx/TransactionService.js +14 -10
- package/dist/tx/index.js +11 -3
- package/dist/tx/types.js +2 -1
- package/dist/util.js +5 -1
- package/dist/utils.js +12 -5
- package/dist/verify.js +36 -25
- package/dist/version.js +5 -2
- package/dist/wots.d.ts +2 -2
- package/dist/wots.js +94 -64
- package/package.json +50 -6
- package/src/Streamable.d.ts +0 -268
- package/src/Streamable.js +0 -416
- package/src/Streamable.ts +0 -567
- package/src/__tests__/Streamable.parity.test.ts +0 -422
- package/src/__tests__/perAddressDerivation.parity.test.ts +0 -152
- package/src/__tests__/roottree-prepare.test.ts +0 -101
- package/src/__tests__/wots-parity.test.ts +0 -289
- package/src/adapters/index.d.ts +0 -190
- package/src/adapters/index.js +0 -117
- package/src/adapters/index.ts +0 -301
- package/src/adapters/types.ts +0 -235
- package/src/addr.oracle.test.ts +0 -25
- package/src/address.test.ts +0 -55
- package/src/binio.ts +0 -21
- package/src/bip39.d.ts +0 -91
- package/src/bip39.js +0 -427
- package/src/bip39.ts +0 -432
- package/src/derive.d.ts +0 -2
- package/src/derive.js +0 -13
- package/src/derive.ts +0 -11
- package/src/index.d.ts +0 -26
- package/src/index.js +0 -128
- package/src/index.test.ts +0 -5
- package/src/index.ts +0 -221
- package/src/javaStreamables.d.ts +0 -233
- package/src/javaStreamables.js +0 -332
- package/src/javaStreamables.ts +0 -403
- package/src/lease/LeaseMonitor.d.ts +0 -44
- package/src/lease/LeaseMonitor.js +0 -111
- package/src/lease/LeaseMonitor.ts +0 -157
- package/src/lease/LeaseStore.d.ts +0 -57
- package/src/lease/LeaseStore.js +0 -148
- package/src/lease/LeaseStore.ts +0 -203
- package/src/lease/WatermarkStore.d.ts +0 -61
- package/src/lease/WatermarkStore.js +0 -206
- package/src/lease/WatermarkStore.ts +0 -270
- package/src/lease/index.d.ts +0 -14
- package/src/lease/index.js +0 -22
- package/src/lease/index.ts +0 -41
- package/src/lease-client.d.ts +0 -31
- package/src/lease-client.js +0 -44
- package/src/lease-client.ts +0 -69
- package/src/minima32.d.ts +0 -10
- package/src/minima32.js +0 -113
- package/src/minima32.test.ts +0 -11
- package/src/minima32.ts +0 -127
- package/src/minimaWireSerializer.d.ts +0 -189
- package/src/minimaWireSerializer.js +0 -311
- package/src/minimaWireSerializer.test.ts +0 -466
- package/src/minimaWireSerializer.ts +0 -117
- package/src/mmr.d.ts +0 -157
- package/src/mmr.js +0 -340
- package/src/mmr.oracle.test.ts +0 -28
- package/src/mmr.ts +0 -429
- package/src/mx.test.ts +0 -25
- package/src/mx.ts +0 -1
- package/src/params.d.ts +0 -29
- package/src/params.d.ts.map +0 -1
- package/src/params.js +0 -20
- package/src/params.js.map +0 -1
- package/src/params.ts +0 -44
- package/src/script.d.ts +0 -7
- package/src/script.js +0 -27
- package/src/script.ts +0 -24
- package/src/scripts/contract-helpers.ts +0 -609
- package/src/scripts/dapp-types.ts +0 -240
- package/src/scripts/index.ts +0 -22
- package/src/scripts/types.d.ts +0 -81
- package/src/scripts/types.js +0 -145
- package/src/scripts/types.ts +0 -247
- package/src/scripts/witness-serializer.ts +0 -384
- package/src/transaction.ts +0 -430
- package/src/treekey.d.ts +0 -383
- package/src/treekey.js +0 -774
- package/src/treekey.parity.test.ts +0 -459
- package/src/treekey.ts +0 -975
- package/src/tx/TransactionLifecycle.d.ts +0 -55
- package/src/tx/TransactionLifecycle.js +0 -188
- package/src/tx/TransactionLifecycle.ts +0 -280
- package/src/tx/TransactionReceiptStore.d.ts +0 -36
- package/src/tx/TransactionReceiptStore.js +0 -89
- package/src/tx/TransactionReceiptStore.ts +0 -112
- package/src/tx/TransactionService.d.ts +0 -31
- package/src/tx/TransactionService.js +0 -140
- package/src/tx/TransactionService.ts +0 -252
- package/src/tx/index.d.ts +0 -13
- package/src/tx/index.js +0 -20
- package/src/tx/index.ts +0 -44
- package/src/tx/types.d.ts +0 -80
- package/src/tx/types.js +0 -6
- package/src/tx/types.ts +0 -130
- package/src/util.d.ts +0 -1
- package/src/util.js +0 -5
- package/src/util.ts +0 -1
- package/src/utils.d.ts +0 -8
- package/src/utils.js +0 -42
- package/src/utils.ts +0 -38
- package/src/verify.ts +0 -210
- package/src/version.ts +0 -13
- package/src/wots.d.ts +0 -224
- package/src/wots.d.ts.map +0 -1
- package/src/wots.js +0 -565
- package/src/wots.js.map +0 -1
- package/src/wots.ts +0 -651
- package/test-vectors.json +0 -45
package/src/tx/types.d.ts
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module Transaction Types
|
|
3
|
-
* Type definitions for transaction service
|
|
4
|
-
*/
|
|
5
|
-
export interface WotsIndices {
|
|
6
|
-
addressIndex: number;
|
|
7
|
-
l1: number;
|
|
8
|
-
l2: number;
|
|
9
|
-
}
|
|
10
|
-
export interface PrepareRequest {
|
|
11
|
-
to: string;
|
|
12
|
-
amount: string;
|
|
13
|
-
tokenId?: string;
|
|
14
|
-
burn?: string;
|
|
15
|
-
txId?: string;
|
|
16
|
-
}
|
|
17
|
-
export interface PrepareResponse {
|
|
18
|
-
addressIndex: number;
|
|
19
|
-
l1: number;
|
|
20
|
-
l2: number;
|
|
21
|
-
leaseToken: string;
|
|
22
|
-
digestTx: string;
|
|
23
|
-
digestL2: string | null;
|
|
24
|
-
digestL3: string | null;
|
|
25
|
-
txId: string;
|
|
26
|
-
rootPublicKey: string;
|
|
27
|
-
paramSet: string;
|
|
28
|
-
leaseId: string;
|
|
29
|
-
leaseTTL: number;
|
|
30
|
-
}
|
|
31
|
-
export interface SignRequest {
|
|
32
|
-
addressIndex: number;
|
|
33
|
-
l1: number;
|
|
34
|
-
l2: number;
|
|
35
|
-
digestTx: string;
|
|
36
|
-
}
|
|
37
|
-
export interface WitnessBundle {
|
|
38
|
-
addressIndex: number;
|
|
39
|
-
l1: number;
|
|
40
|
-
l2: number;
|
|
41
|
-
signatures: {
|
|
42
|
-
l1Proof: string[];
|
|
43
|
-
l2Proof: string[];
|
|
44
|
-
l3Proof: string[];
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
export interface SignResult {
|
|
48
|
-
witnessBundle: WitnessBundle;
|
|
49
|
-
signedHex: string;
|
|
50
|
-
}
|
|
51
|
-
export interface FinalizeRequest {
|
|
52
|
-
leaseToken: string;
|
|
53
|
-
signedHex: string;
|
|
54
|
-
}
|
|
55
|
-
export interface FinalizeResponse {
|
|
56
|
-
ok: boolean;
|
|
57
|
-
leaseId: string;
|
|
58
|
-
txpowid: string;
|
|
59
|
-
}
|
|
60
|
-
export interface TransactionMetadata {
|
|
61
|
-
to: string;
|
|
62
|
-
amount: string;
|
|
63
|
-
tokenId: string;
|
|
64
|
-
}
|
|
65
|
-
export interface TransactionReceipt {
|
|
66
|
-
txpowid: string;
|
|
67
|
-
timestamp: number;
|
|
68
|
-
to: string;
|
|
69
|
-
amount: string;
|
|
70
|
-
tokenId: string;
|
|
71
|
-
indices: WotsIndices;
|
|
72
|
-
status: 'confirmed' | 'pending' | 'failed';
|
|
73
|
-
txId?: string;
|
|
74
|
-
leaseId?: string;
|
|
75
|
-
}
|
|
76
|
-
export interface TransactionError {
|
|
77
|
-
code: number;
|
|
78
|
-
message: string;
|
|
79
|
-
userMessage: string;
|
|
80
|
-
}
|
package/src/tx/types.js
DELETED
package/src/tx/types.ts
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module Transaction Types
|
|
3
|
-
* Type definitions for transaction service
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
export interface WotsIndices {
|
|
7
|
-
addressIndex: number;
|
|
8
|
-
l1: number;
|
|
9
|
-
l2: number;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface PrepareRequest {
|
|
13
|
-
to: string;
|
|
14
|
-
amount: string;
|
|
15
|
-
tokenId?: string;
|
|
16
|
-
burn?: string;
|
|
17
|
-
txId?: string;
|
|
18
|
-
addressIndex?: number; // Required by v2-spec backend (0-63, must match the coin's address)
|
|
19
|
-
walletMode?: 'AnonTree' | 'RootTree'; // Key derivation scheme — omit for legacy/AnonTree
|
|
20
|
-
perAddressPublicKey?: string; // Hex pubkey of per-address TreeKey root (required for RootTree validation)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface PrepareResponse {
|
|
24
|
-
addressIndex: number;
|
|
25
|
-
l1: number;
|
|
26
|
-
l2: number;
|
|
27
|
-
leaseToken: string;
|
|
28
|
-
digestTx: string;
|
|
29
|
-
digestL2: string | null;
|
|
30
|
-
digestL3: string | null;
|
|
31
|
-
txId: string;
|
|
32
|
-
rootPublicKey: string;
|
|
33
|
-
paramSet: string;
|
|
34
|
-
leaseId: string;
|
|
35
|
-
leaseTTL: number;
|
|
36
|
-
perAddressScript?: string | null; // RETURN SIGNEDBY(<perAddressPublicKey>) — present in RootTree mode
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export interface SignRequest {
|
|
40
|
-
addressIndex: number;
|
|
41
|
-
l1: number;
|
|
42
|
-
l2: number;
|
|
43
|
-
digestTx: string;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Per-proof entry in a hierarchical TreeKey signature chain.
|
|
48
|
-
* Each proof represents one parent→child signing step.
|
|
49
|
-
*/
|
|
50
|
-
export interface SignatureProofHex {
|
|
51
|
-
leafPubkey: string; // 32-byte WOTS public key DIGEST as hex (SHA3-256 of full key)
|
|
52
|
-
signature: string; // 1088-byte WOTS signature as hex
|
|
53
|
-
mmrProof: string; // Serialized MMR proof as hex
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Hierarchical witness bundle produced by per-address TreeKey signing.
|
|
58
|
-
*
|
|
59
|
-
* Index mapping:
|
|
60
|
-
* addressIndex — which HD address (0-63)
|
|
61
|
-
* l1 — L1 index within per-address TreeKey (0-63)
|
|
62
|
-
* l2 — L2 index within per-address TreeKey (0-63)
|
|
63
|
-
*
|
|
64
|
-
* proofs contains 3 entries for depth-3 TreeKeys (Root→L1→L2→DATA),
|
|
65
|
-
* matching Minima's TreeKey.sign() exactly.
|
|
66
|
-
*/
|
|
67
|
-
export interface HierarchicalWitnessBundle {
|
|
68
|
-
addressIndex: number;
|
|
69
|
-
l1: number;
|
|
70
|
-
l2: number;
|
|
71
|
-
rootPublicKey: string;
|
|
72
|
-
proofs: SignatureProofHex[];
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* @deprecated Use HierarchicalWitnessBundle. Kept for backward compatibility.
|
|
77
|
-
*/
|
|
78
|
-
export interface WitnessBundle {
|
|
79
|
-
addressIndex: number;
|
|
80
|
-
l1: number;
|
|
81
|
-
l2: number;
|
|
82
|
-
signatures: {
|
|
83
|
-
l1Proof: string[];
|
|
84
|
-
l2Proof: string[];
|
|
85
|
-
l3Proof: string[];
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export interface SignResult {
|
|
90
|
-
witnessBundle: HierarchicalWitnessBundle;
|
|
91
|
-
signedHex: string;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export interface FinalizeRequest {
|
|
95
|
-
leaseToken: string;
|
|
96
|
-
signedHex?: string; // Hex-encoded signed transaction
|
|
97
|
-
signedBase64?: string; // Base64-encoded signed transaction (preferred, bypasses WAF)
|
|
98
|
-
transactionHex?: string; // Optional: the unsigned transaction body
|
|
99
|
-
importId?: string; // Optional: client-provided import ID for txnimport
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export interface FinalizeResponse {
|
|
103
|
-
ok: boolean;
|
|
104
|
-
leaseId: string;
|
|
105
|
-
txpowid: string;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export interface TransactionMetadata {
|
|
109
|
-
to: string;
|
|
110
|
-
amount: string;
|
|
111
|
-
tokenId: string;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export interface TransactionReceipt {
|
|
115
|
-
txpowid: string;
|
|
116
|
-
timestamp: number;
|
|
117
|
-
to: string;
|
|
118
|
-
amount: string;
|
|
119
|
-
tokenId: string;
|
|
120
|
-
indices: WotsIndices;
|
|
121
|
-
status: 'confirmed' | 'pending' | 'failed';
|
|
122
|
-
txId?: string;
|
|
123
|
-
leaseId?: string;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
export interface TransactionError {
|
|
127
|
-
code: number;
|
|
128
|
-
message: string;
|
|
129
|
-
userMessage: string;
|
|
130
|
-
}
|
package/src/util.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const bytesToHex: (b: Uint8Array) => string;
|
package/src/util.js
DELETED
package/src/util.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const bytesToHex = (b: Uint8Array) => Array.from(b).map(x => x.toString(16).padStart(2, "0")).join("").toUpperCase();
|
package/src/utils.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Utility functions for byte array manipulation
|
|
3
|
-
*/
|
|
4
|
-
export declare function bytesToHex(bytes: Uint8Array): string;
|
|
5
|
-
export declare function hexToBytes(hex: string): Uint8Array;
|
|
6
|
-
export declare function concatBytes(...arrays: Uint8Array[]): Uint8Array;
|
|
7
|
-
export declare function utf8ToBytes(str: string): Uint8Array;
|
|
8
|
-
export declare function bytesToUtf8(bytes: Uint8Array): string;
|
package/src/utils.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Utility functions for byte array manipulation
|
|
4
|
-
*/
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.bytesToHex = bytesToHex;
|
|
7
|
-
exports.hexToBytes = hexToBytes;
|
|
8
|
-
exports.concatBytes = concatBytes;
|
|
9
|
-
exports.utf8ToBytes = utf8ToBytes;
|
|
10
|
-
exports.bytesToUtf8 = bytesToUtf8;
|
|
11
|
-
function bytesToHex(bytes) {
|
|
12
|
-
return Array.from(bytes)
|
|
13
|
-
.map(b => b.toString(16).padStart(2, '0'))
|
|
14
|
-
.join('');
|
|
15
|
-
}
|
|
16
|
-
function hexToBytes(hex) {
|
|
17
|
-
if (hex.startsWith('0x'))
|
|
18
|
-
hex = hex.slice(2);
|
|
19
|
-
if (hex.length % 2 !== 0)
|
|
20
|
-
throw new Error('Invalid hex string');
|
|
21
|
-
const bytes = new Uint8Array(hex.length / 2);
|
|
22
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
23
|
-
bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
24
|
-
}
|
|
25
|
-
return bytes;
|
|
26
|
-
}
|
|
27
|
-
function concatBytes(...arrays) {
|
|
28
|
-
const totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0);
|
|
29
|
-
const result = new Uint8Array(totalLength);
|
|
30
|
-
let offset = 0;
|
|
31
|
-
for (const arr of arrays) {
|
|
32
|
-
result.set(arr, offset);
|
|
33
|
-
offset += arr.length;
|
|
34
|
-
}
|
|
35
|
-
return result;
|
|
36
|
-
}
|
|
37
|
-
function utf8ToBytes(str) {
|
|
38
|
-
return new TextEncoder().encode(str);
|
|
39
|
-
}
|
|
40
|
-
function bytesToUtf8(bytes) {
|
|
41
|
-
return new TextDecoder().decode(bytes);
|
|
42
|
-
}
|
package/src/utils.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Utility functions for byte array manipulation
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
export function bytesToHex(bytes: Uint8Array): string {
|
|
6
|
-
return Array.from(bytes)
|
|
7
|
-
.map(b => b.toString(16).padStart(2, '0'))
|
|
8
|
-
.join('');
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function hexToBytes(hex: string): Uint8Array {
|
|
12
|
-
if (hex.startsWith('0x')) hex = hex.slice(2);
|
|
13
|
-
if (hex.length % 2 !== 0) throw new Error('Invalid hex string');
|
|
14
|
-
const bytes = new Uint8Array(hex.length / 2);
|
|
15
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
16
|
-
bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
17
|
-
}
|
|
18
|
-
return bytes;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function concatBytes(...arrays: Uint8Array[]): Uint8Array {
|
|
22
|
-
const totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0);
|
|
23
|
-
const result = new Uint8Array(totalLength);
|
|
24
|
-
let offset = 0;
|
|
25
|
-
for (const arr of arrays) {
|
|
26
|
-
result.set(arr, offset);
|
|
27
|
-
offset += arr.length;
|
|
28
|
-
}
|
|
29
|
-
return result;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function utf8ToBytes(str: string): Uint8Array {
|
|
33
|
-
return new TextEncoder().encode(str);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export function bytesToUtf8(bytes: Uint8Array): string {
|
|
37
|
-
return new TextDecoder().decode(bytes);
|
|
38
|
-
}
|
package/src/verify.ts
DELETED
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
import { sha3_256 } from '@noble/hashes/sha3.js';
|
|
2
|
-
import { verifyTreeSignature, deserializeTreeSignature } from './treekey.js';
|
|
3
|
-
import { scriptToAddress, addressToRoot } from './derive.js';
|
|
4
|
-
import { scriptFromWotsPk } from './script.js';
|
|
5
|
-
import { getRootPublicKey } from './treekey.js';
|
|
6
|
-
import { wotsVerifyDigest } from './wots.js';
|
|
7
|
-
import { getParamSet } from './params.js';
|
|
8
|
-
import type { TreeSignature, SignatureProof } from './treekey.js';
|
|
9
|
-
import type { Bytes } from './mmr.js';
|
|
10
|
-
|
|
11
|
-
export interface VerificationResult {
|
|
12
|
-
valid: boolean;
|
|
13
|
-
error?: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function normalizeHex(hex: string): string {
|
|
17
|
-
const stripped = hex.startsWith('0x') ? hex.slice(2) : hex;
|
|
18
|
-
return stripped.toLowerCase();
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function hexToBytes(hex: string): Uint8Array {
|
|
22
|
-
const h = normalizeHex(hex);
|
|
23
|
-
if (h.length % 2 !== 0) throw new Error('Invalid hex string: odd length');
|
|
24
|
-
const bytes = new Uint8Array(h.length / 2);
|
|
25
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
26
|
-
bytes[i] = parseInt(h.slice(i * 2, i * 2 + 2), 16);
|
|
27
|
-
}
|
|
28
|
-
return bytes;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function bytesToHex(bytes: Uint8Array): string {
|
|
32
|
-
return Array.from(bytes)
|
|
33
|
-
.map(b => b.toString(16).padStart(2, '0'))
|
|
34
|
-
.join('');
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function deriveAddressFromPublicKey(publicKeyHex: string): string {
|
|
38
|
-
const pkBytes = hexToBytes(publicKeyHex);
|
|
39
|
-
if (pkBytes.length !== 32) {
|
|
40
|
-
throw new Error(
|
|
41
|
-
`Public key must be 32 bytes (64 hex chars), got ${pkBytes.length} bytes`
|
|
42
|
-
);
|
|
43
|
-
}
|
|
44
|
-
const script = scriptFromWotsPk(pkBytes);
|
|
45
|
-
return scriptToAddress(script);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function verifyTreeSignatureDetailed(
|
|
49
|
-
expectedPubkey: Bytes,
|
|
50
|
-
data: Bytes,
|
|
51
|
-
signature: TreeSignature
|
|
52
|
-
): VerificationResult {
|
|
53
|
-
const { proofs } = signature;
|
|
54
|
-
|
|
55
|
-
if (proofs.length === 0) {
|
|
56
|
-
return { valid: false, error: 'Signature contains no proofs' };
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const paramSet = getParamSet();
|
|
60
|
-
|
|
61
|
-
for (let depth = 0; depth < proofs.length; depth++) {
|
|
62
|
-
const proof = proofs[depth];
|
|
63
|
-
|
|
64
|
-
const rootPubkey = getRootPublicKey(proof);
|
|
65
|
-
|
|
66
|
-
if (depth === 0) {
|
|
67
|
-
if (!timingSafeEqual(expectedPubkey, rootPubkey)) {
|
|
68
|
-
return {
|
|
69
|
-
valid: false,
|
|
70
|
-
error: 'Root public key does not match expected public key (wrong key or address)',
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
let signedData: Bytes;
|
|
76
|
-
if (depth === proofs.length - 1) {
|
|
77
|
-
signedData = data;
|
|
78
|
-
} else {
|
|
79
|
-
signedData = getRootPublicKey(proofs[depth + 1]);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (!wotsVerifyDigest(proof.signature, signedData, proof.leafPubkey, paramSet)) {
|
|
83
|
-
return {
|
|
84
|
-
valid: false,
|
|
85
|
-
error: `WOTS signature verification failed at depth ${depth} (digest mismatch or corrupted signature)`,
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return { valid: true };
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export function verifySignature(
|
|
94
|
-
address: string,
|
|
95
|
-
message: string,
|
|
96
|
-
signatureHex: string,
|
|
97
|
-
publicKeyHex: string
|
|
98
|
-
): boolean {
|
|
99
|
-
const result = verifySignatureDetailed(address, message, signatureHex, publicKeyHex);
|
|
100
|
-
return result.valid;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export function verifySignatureDetailed(
|
|
104
|
-
address: string,
|
|
105
|
-
message: string,
|
|
106
|
-
signatureHex: string,
|
|
107
|
-
publicKeyHex: string
|
|
108
|
-
): VerificationResult {
|
|
109
|
-
try {
|
|
110
|
-
const pkBytes = hexToBytes(publicKeyHex);
|
|
111
|
-
if (pkBytes.length !== 32) {
|
|
112
|
-
return {
|
|
113
|
-
valid: false,
|
|
114
|
-
error: `Public key must be 32 bytes (64 hex chars), got ${pkBytes.length} bytes`,
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const derivedAddress = deriveAddressFromPublicKey(publicKeyHex);
|
|
119
|
-
if (derivedAddress.toLowerCase() !== address.toLowerCase()) {
|
|
120
|
-
return {
|
|
121
|
-
valid: false,
|
|
122
|
-
error: `Public key does not match claimed address. Expected ${address}, derived ${derivedAddress}`,
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
const sigBytes = hexToBytes(signatureHex);
|
|
127
|
-
let treeSignature: TreeSignature;
|
|
128
|
-
try {
|
|
129
|
-
treeSignature = deserializeTreeSignature(sigBytes);
|
|
130
|
-
} catch (e: any) {
|
|
131
|
-
return {
|
|
132
|
-
valid: false,
|
|
133
|
-
error: `Malformed signature: ${e.message}`,
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const digest = sha3_256(new TextEncoder().encode(message));
|
|
138
|
-
|
|
139
|
-
return verifyTreeSignatureDetailed(pkBytes, digest, treeSignature);
|
|
140
|
-
} catch (e: any) {
|
|
141
|
-
return {
|
|
142
|
-
valid: false,
|
|
143
|
-
error: `Verification error: ${e.message}`,
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
function timingSafeEqual(a: Uint8Array, b: Uint8Array): boolean {
|
|
149
|
-
if (a.length !== b.length) return false;
|
|
150
|
-
let result = 0;
|
|
151
|
-
for (let i = 0; i < a.length; i++) {
|
|
152
|
-
result |= a[i] ^ b[i];
|
|
153
|
-
}
|
|
154
|
-
return result === 0;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
function cryptoRandomBytes(length: number): Uint8Array {
|
|
158
|
-
const buf = new Uint8Array(length);
|
|
159
|
-
if (typeof globalThis.crypto !== 'undefined' && globalThis.crypto.getRandomValues) {
|
|
160
|
-
// Web Crypto API — available in browsers, Bare/Pear, and Node.js ≥ 19
|
|
161
|
-
globalThis.crypto.getRandomValues(buf);
|
|
162
|
-
} else if (typeof process !== 'undefined' && process.versions?.node) {
|
|
163
|
-
// Node.js < 19 fallback — dynamic import avoids static resolution in Bare/bundlers
|
|
164
|
-
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
165
|
-
const nodeCrypto = Function('return require')()('crypto') as { randomFillSync: (buf: Uint8Array) => void };
|
|
166
|
-
nodeCrypto.randomFillSync(buf);
|
|
167
|
-
} else {
|
|
168
|
-
throw new Error('No cryptographically secure RNG available in this runtime');
|
|
169
|
-
}
|
|
170
|
-
return buf;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
export function createChallenge(domain: string, nonce?: string): string {
|
|
174
|
-
const ts = Date.now();
|
|
175
|
-
const n = nonce || bytesToHex(
|
|
176
|
-
sha3_256(new Uint8Array([...new TextEncoder().encode(`${domain}:${ts}:`), ...cryptoRandomBytes(16)]))
|
|
177
|
-
).slice(0, 16);
|
|
178
|
-
return JSON.stringify({ domain, nonce: n, issuedAt: ts });
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
export function validateChallenge(
|
|
182
|
-
challenge: string,
|
|
183
|
-
options?: { maxAgeMs?: number; expectedDomain?: string }
|
|
184
|
-
): VerificationResult {
|
|
185
|
-
try {
|
|
186
|
-
const parsed = JSON.parse(challenge);
|
|
187
|
-
const { domain, nonce, issuedAt } = parsed;
|
|
188
|
-
|
|
189
|
-
if (!domain || !nonce || !issuedAt) {
|
|
190
|
-
return { valid: false, error: 'Challenge missing required fields (domain, nonce, issuedAt)' };
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
if (options?.expectedDomain && domain !== options.expectedDomain) {
|
|
194
|
-
return { valid: false, error: `Domain mismatch: expected ${options.expectedDomain}, got ${domain}` };
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
const maxAge = options?.maxAgeMs ?? 5 * 60 * 1000;
|
|
198
|
-
const age = Date.now() - issuedAt;
|
|
199
|
-
if (age > maxAge) {
|
|
200
|
-
return { valid: false, error: `Challenge expired: ${Math.round(age / 1000)}s old (max ${Math.round(maxAge / 1000)}s)` };
|
|
201
|
-
}
|
|
202
|
-
if (age < 0) {
|
|
203
|
-
return { valid: false, error: 'Challenge issued in the future' };
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
return { valid: true };
|
|
207
|
-
} catch {
|
|
208
|
-
return { valid: false, error: 'Invalid challenge format (not valid JSON)' };
|
|
209
|
-
}
|
|
210
|
-
}
|
package/src/version.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SDK Core Version Information
|
|
3
|
-
*
|
|
4
|
-
* CORE_BUILD_ID is used to detect bundle duplication issues where
|
|
5
|
-
* the extension might bundle two different copies of sdk-core,
|
|
6
|
-
* computing addresses with one copy and signing with another.
|
|
7
|
-
*
|
|
8
|
-
* If you see different CORE_BUILD_IDs logged from wallet creation
|
|
9
|
-
* vs signing modules, there's a bundling issue.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
export const CORE_BUILD_ID = '2026.02.05-v1';
|
|
13
|
-
export const CORE_VERSION = '1.0.0';
|