@nockchain/sdk 0.1.4-nightly
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 +88 -0
- package/dist/constants.d.ts +22 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +21 -0
- package/dist/constants.js.map +1 -0
- package/dist/errors.d.ts +42 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +66 -0
- package/dist/errors.js.map +1 -0
- package/dist/hooks/index.d.ts +2 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +2 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/use-iris.d.ts +14 -0
- package/dist/hooks/use-iris.d.ts.map +1 -0
- package/dist/hooks/use-iris.js +72 -0
- package/dist/hooks/use-iris.js.map +1 -0
- package/dist/hooks/use-rose.d.ts +14 -0
- package/dist/hooks/use-rose.d.ts.map +1 -0
- package/dist/hooks/use-rose.js +75 -0
- package/dist/hooks/use-rose.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/provider.d.ts +193 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +350 -0
- package/dist/provider.js.map +1 -0
- package/dist/transaction.d.ts +81 -0
- package/dist/transaction.d.ts.map +1 -0
- package/dist/transaction.js +144 -0
- package/dist/transaction.js.map +1 -0
- package/dist/types.d.ts +92 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/dist/wasm.d.ts +13 -0
- package/dist/wasm.d.ts.map +1 -0
- package/dist/wasm.js +13 -0
- package/dist/wasm.js.map +1 -0
- package/package.json +68 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NockchainProvider - Main SDK class for interacting with Iris wallet
|
|
3
|
+
*/
|
|
4
|
+
import type { Transaction, NockchainEvent, EventListener } from './types.js';
|
|
5
|
+
import { TransactionBuilder } from './transaction.js';
|
|
6
|
+
/**
|
|
7
|
+
* NockchainProvider class - Main interface for dApps to interact with Iris wallet
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const nockchain = new NockchainProvider();
|
|
12
|
+
*
|
|
13
|
+
* // Connect wallet
|
|
14
|
+
* const accounts = await nockchain.requestAccounts();
|
|
15
|
+
*
|
|
16
|
+
* // Build and send transaction
|
|
17
|
+
* const tx = nockchain.transaction()
|
|
18
|
+
* .to('recipient_address')
|
|
19
|
+
* .amount(1_000_000)
|
|
20
|
+
* .build();
|
|
21
|
+
*
|
|
22
|
+
* const txId = await nockchain.sendTransaction(tx);
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare class NockchainProvider {
|
|
26
|
+
private injected;
|
|
27
|
+
private eventListeners;
|
|
28
|
+
private _accounts;
|
|
29
|
+
private _chainId;
|
|
30
|
+
private _messageHandler?;
|
|
31
|
+
/**
|
|
32
|
+
* Create a new NockchainProvider instance
|
|
33
|
+
* @throws {WalletNotInstalledError} If the Iris extension is not installed
|
|
34
|
+
*/
|
|
35
|
+
constructor();
|
|
36
|
+
/**
|
|
37
|
+
* Connect to the wallet and request access
|
|
38
|
+
* This will prompt the user to approve the connection
|
|
39
|
+
* @returns Promise resolving to wallet info with PKH and gRPC endpoint
|
|
40
|
+
* @throws {UserRejectedError} If the user rejects the request
|
|
41
|
+
* @throws {RpcError} If the RPC call fails
|
|
42
|
+
*/
|
|
43
|
+
connect(): Promise<{
|
|
44
|
+
pkh: string;
|
|
45
|
+
grpcEndpoint: string;
|
|
46
|
+
}>;
|
|
47
|
+
/**
|
|
48
|
+
* Get the currently connected accounts (if any)
|
|
49
|
+
* @returns Array of connected account addresses (PKH)
|
|
50
|
+
*/
|
|
51
|
+
get accounts(): string[];
|
|
52
|
+
/**
|
|
53
|
+
* Get the current chain ID
|
|
54
|
+
* @returns The current chain ID or null if not connected
|
|
55
|
+
*/
|
|
56
|
+
get chainId(): string | null;
|
|
57
|
+
/**
|
|
58
|
+
* Check if the wallet is connected
|
|
59
|
+
* @returns true if wallet is connected
|
|
60
|
+
*/
|
|
61
|
+
get isConnected(): boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Send a transaction
|
|
64
|
+
* @param transaction - The transaction object to send
|
|
65
|
+
* @returns Promise resolving to the transaction ID
|
|
66
|
+
* @throws {NoAccountError} If no account is connected
|
|
67
|
+
* @throws {UserRejectedError} If the user rejects the transaction
|
|
68
|
+
* @throws {RpcError} If the RPC call fails
|
|
69
|
+
*/
|
|
70
|
+
sendTransaction(transaction: Transaction): Promise<string>;
|
|
71
|
+
/**
|
|
72
|
+
* Sign an arbitrary message with the current account
|
|
73
|
+
* @param message - The message to sign
|
|
74
|
+
* @returns Promise resolving to the signature and public key hex (for verification)
|
|
75
|
+
* @throws {NoAccountError} If no account is connected
|
|
76
|
+
* @throws {UserRejectedError} If the user rejects the signing request
|
|
77
|
+
* @throws {RpcError} If the RPC call fails
|
|
78
|
+
*/
|
|
79
|
+
signMessage(message: string): Promise<{
|
|
80
|
+
signature: string;
|
|
81
|
+
publicKeyHex: string;
|
|
82
|
+
}>;
|
|
83
|
+
/**
|
|
84
|
+
* Sign a raw transaction
|
|
85
|
+
* Accepts either wasm objects (with toProtobuf() method) or protobuf JS objects
|
|
86
|
+
* @param params - The transaction parameters (rawTx, notes, spendConditions)
|
|
87
|
+
* @returns Promise resolving to the signed raw transaction as protobuf Uint8Array
|
|
88
|
+
* @throws {NoAccountError} If no account is connected
|
|
89
|
+
* @throws {UserRejectedError} If the user rejects the signing request
|
|
90
|
+
* @throws {RpcError} If the RPC call fails
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* // Option 1: Pass wasm objects directly (auto-converts to protobuf)
|
|
95
|
+
* const rawTx = builder.build();
|
|
96
|
+
* const txNotes = builder.allNotes();
|
|
97
|
+
*
|
|
98
|
+
* const signedTx = await provider.signRawTx({
|
|
99
|
+
* rawTx: rawTx, // wasm RawTx object
|
|
100
|
+
* notes: txNotes.notes, // array of wasm Note objects
|
|
101
|
+
* spendConditions: txNotes.spendConditions // array of wasm SpendCondition objects
|
|
102
|
+
* });
|
|
103
|
+
*
|
|
104
|
+
* // Option 2: Pass protobuf JS objects directly
|
|
105
|
+
* const signedTx = await provider.signRawTx({
|
|
106
|
+
* rawTx: rawTxProtobufObject, // protobuf JS object
|
|
107
|
+
* notes: noteProtobufObjects, // array of protobuf JS objects
|
|
108
|
+
* spendConditions: spendCondProtobufObjects // array of protobuf JS objects
|
|
109
|
+
* });
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
signRawTx(params: {
|
|
113
|
+
rawTx: any;
|
|
114
|
+
notes: any[];
|
|
115
|
+
spendConditions: any[];
|
|
116
|
+
}): Promise<Uint8Array>;
|
|
117
|
+
/**
|
|
118
|
+
* Create a new transaction builder
|
|
119
|
+
* @returns A new TransactionBuilder instance
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```typescript
|
|
123
|
+
* const tx = provider.transaction()
|
|
124
|
+
* .to('recipient_address')
|
|
125
|
+
* .amount(1_000_000)
|
|
126
|
+
* .fee(50_000)
|
|
127
|
+
* .build();
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
transaction(): TransactionBuilder;
|
|
131
|
+
/**
|
|
132
|
+
* Add an event listener for wallet events
|
|
133
|
+
* @param event - The event to listen for
|
|
134
|
+
* @param listener - The callback function to invoke when the event occurs
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* provider.on('accountsChanged', (accounts) => {
|
|
139
|
+
* console.log('Accounts changed:', accounts);
|
|
140
|
+
* });
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
on<T = unknown>(event: NockchainEvent, listener: EventListener<T>): void;
|
|
144
|
+
/**
|
|
145
|
+
* Remove an event listener
|
|
146
|
+
* @param event - The event to stop listening for
|
|
147
|
+
* @param listener - The callback function to remove
|
|
148
|
+
*/
|
|
149
|
+
off<T = unknown>(event: NockchainEvent, listener: EventListener<T>): void;
|
|
150
|
+
/**
|
|
151
|
+
* Remove all event listeners for a specific event or all events
|
|
152
|
+
* @param event - Optional event to remove listeners for (removes all if not specified)
|
|
153
|
+
*/
|
|
154
|
+
removeAllListeners(event?: NockchainEvent): void;
|
|
155
|
+
/**
|
|
156
|
+
* Make a raw RPC request to the wallet extension (EIP-1193 compatible)
|
|
157
|
+
* @param args - The RPC request arguments
|
|
158
|
+
* @returns Promise resolving to the result
|
|
159
|
+
* @throws {UserRejectedError} If the user rejects the request
|
|
160
|
+
* @throws {RpcError} If the RPC call fails
|
|
161
|
+
*/
|
|
162
|
+
request<T = unknown>(args: {
|
|
163
|
+
method: string;
|
|
164
|
+
params?: unknown[];
|
|
165
|
+
}): Promise<T>;
|
|
166
|
+
/**
|
|
167
|
+
* Check if an error represents user rejection
|
|
168
|
+
* Uses EIP-1193 standard error code 4001
|
|
169
|
+
*/
|
|
170
|
+
private isUserRejected;
|
|
171
|
+
/**
|
|
172
|
+
* Set up event listeners for wallet events
|
|
173
|
+
* This listens for events from the extension and forwards them to registered listeners
|
|
174
|
+
*/
|
|
175
|
+
private setupEventListeners;
|
|
176
|
+
/**
|
|
177
|
+
* Clean up event listeners and resources
|
|
178
|
+
* Call this when the provider is no longer needed (e.g., on component unmount)
|
|
179
|
+
*/
|
|
180
|
+
dispose(): void;
|
|
181
|
+
/**
|
|
182
|
+
* Emit an event to all registered listeners
|
|
183
|
+
* @param event - The event to emit
|
|
184
|
+
* @param data - The data to pass to listeners
|
|
185
|
+
*/
|
|
186
|
+
private emit;
|
|
187
|
+
/**
|
|
188
|
+
* Check if the Iris extension is installed and authentic
|
|
189
|
+
* @returns true if the extension is installed
|
|
190
|
+
*/
|
|
191
|
+
static isInstalled(): boolean;
|
|
192
|
+
}
|
|
193
|
+
//#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;IA4BvB;;;;;;;;;;;;OAYG;IACH,WAAW,IAAI,kBAAkB;IAIjC;;;;;;;;;;;OAWG;IACH,EAAE,CAAC,CAAC,GAAG,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,QAAQ,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,IAAI;IAOxE;;;;OAIG;IACH,GAAG,CAAC,CAAC,GAAG,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,QAAQ,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,IAAI;IAOzE;;;OAGG;IACH,kBAAkB,CAAC,KAAK,CAAC,EAAE,cAAc,GAAG,IAAI;IAQhD;;;;;;OAMG;IACU,OAAO,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,OAAO,EAAE,CAAA;KAAE,GAAG,OAAO,CAAC,CAAC,CAAC;IAsB3F;;;OAGG;IACH,OAAO,CAAC,cAAc;IAgBtB;;;OAGG;IACH,OAAO,CAAC,mBAAmB;IAqC3B;;;OAGG;IACI,OAAO,IAAI,IAAI;IAQtB;;;;OAIG;IACH,OAAO,CAAC,IAAI;IAaZ;;;OAGG;IACH,MAAM,CAAC,WAAW,IAAI,OAAO;CAI9B"}
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NockchainProvider - Main SDK class for interacting with Iris wallet
|
|
3
|
+
*/
|
|
4
|
+
import { TransactionBuilder } from './transaction.js';
|
|
5
|
+
import { WalletNotInstalledError, UserRejectedError, RpcError, NoAccountError } from './errors.js';
|
|
6
|
+
import { PROVIDER_METHODS } from './constants.js';
|
|
7
|
+
/**
|
|
8
|
+
* NockchainProvider class - Main interface for dApps to interact with Iris wallet
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const nockchain = new NockchainProvider();
|
|
13
|
+
*
|
|
14
|
+
* // Connect wallet
|
|
15
|
+
* const accounts = await nockchain.requestAccounts();
|
|
16
|
+
*
|
|
17
|
+
* // Build and send transaction
|
|
18
|
+
* const tx = nockchain.transaction()
|
|
19
|
+
* .to('recipient_address')
|
|
20
|
+
* .amount(1_000_000)
|
|
21
|
+
* .build();
|
|
22
|
+
*
|
|
23
|
+
* const txId = await nockchain.sendTransaction(tx);
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export class NockchainProvider {
|
|
27
|
+
/**
|
|
28
|
+
* Create a new NockchainProvider instance
|
|
29
|
+
* @throws {WalletNotInstalledError} If the Iris extension is not installed
|
|
30
|
+
*/
|
|
31
|
+
constructor() {
|
|
32
|
+
this._accounts = [];
|
|
33
|
+
this._chainId = null;
|
|
34
|
+
if (typeof window === 'undefined') {
|
|
35
|
+
throw new Error('NockchainProvider can only be used in a browser environment');
|
|
36
|
+
}
|
|
37
|
+
// Verify Iris extension is installed and authentic
|
|
38
|
+
if (!NockchainProvider.isInstalled()) {
|
|
39
|
+
throw new WalletNotInstalledError();
|
|
40
|
+
}
|
|
41
|
+
const injected = window.nockchain;
|
|
42
|
+
// TODO: remove this duplicate check
|
|
43
|
+
if (!injected) {
|
|
44
|
+
throw new WalletNotInstalledError();
|
|
45
|
+
}
|
|
46
|
+
this.injected = injected;
|
|
47
|
+
this.eventListeners = new Map();
|
|
48
|
+
// Initialize event listeners for wallet events
|
|
49
|
+
this.setupEventListeners();
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Connect to the wallet and request access
|
|
53
|
+
* This will prompt the user to approve the connection
|
|
54
|
+
* @returns Promise resolving to wallet info with PKH and gRPC endpoint
|
|
55
|
+
* @throws {UserRejectedError} If the user rejects the request
|
|
56
|
+
* @throws {RpcError} If the RPC call fails
|
|
57
|
+
*/
|
|
58
|
+
async connect() {
|
|
59
|
+
const info = await this.request({
|
|
60
|
+
method: PROVIDER_METHODS.CONNECT,
|
|
61
|
+
});
|
|
62
|
+
// Store the PKH as the connected account
|
|
63
|
+
this._accounts = [info.pkh];
|
|
64
|
+
return info;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Get the currently connected accounts (if any)
|
|
68
|
+
* @returns Array of connected account addresses (PKH)
|
|
69
|
+
*/
|
|
70
|
+
get accounts() {
|
|
71
|
+
return [...this._accounts];
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Get the current chain ID
|
|
75
|
+
* @returns The current chain ID or null if not connected
|
|
76
|
+
*/
|
|
77
|
+
get chainId() {
|
|
78
|
+
return this._chainId;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Check if the wallet is connected
|
|
82
|
+
* @returns true if wallet is connected
|
|
83
|
+
*/
|
|
84
|
+
get isConnected() {
|
|
85
|
+
return this._accounts.length > 0;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Send a transaction
|
|
89
|
+
* @param transaction - The transaction object to send
|
|
90
|
+
* @returns Promise resolving to the transaction ID
|
|
91
|
+
* @throws {NoAccountError} If no account is connected
|
|
92
|
+
* @throws {UserRejectedError} If the user rejects the transaction
|
|
93
|
+
* @throws {RpcError} If the RPC call fails
|
|
94
|
+
*/
|
|
95
|
+
async sendTransaction(transaction) {
|
|
96
|
+
if (!this.isConnected) {
|
|
97
|
+
throw new NoAccountError();
|
|
98
|
+
}
|
|
99
|
+
return this.request({
|
|
100
|
+
method: PROVIDER_METHODS.SEND_TRANSACTION,
|
|
101
|
+
params: [transaction],
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Sign an arbitrary message with the current account
|
|
106
|
+
* @param message - The message to sign
|
|
107
|
+
* @returns Promise resolving to the signature and public key hex (for verification)
|
|
108
|
+
* @throws {NoAccountError} If no account is connected
|
|
109
|
+
* @throws {UserRejectedError} If the user rejects the signing request
|
|
110
|
+
* @throws {RpcError} If the RPC call fails
|
|
111
|
+
*/
|
|
112
|
+
async signMessage(message) {
|
|
113
|
+
if (!this.isConnected) {
|
|
114
|
+
throw new NoAccountError();
|
|
115
|
+
}
|
|
116
|
+
return this.request({
|
|
117
|
+
method: PROVIDER_METHODS.SIGN_MESSAGE,
|
|
118
|
+
params: [message],
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Sign a raw transaction
|
|
123
|
+
* Accepts either wasm objects (with toProtobuf() method) or protobuf JS objects
|
|
124
|
+
* @param params - The transaction parameters (rawTx, notes, spendConditions)
|
|
125
|
+
* @returns Promise resolving to the signed raw transaction as protobuf Uint8Array
|
|
126
|
+
* @throws {NoAccountError} If no account is connected
|
|
127
|
+
* @throws {UserRejectedError} If the user rejects the signing request
|
|
128
|
+
* @throws {RpcError} If the RPC call fails
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```typescript
|
|
132
|
+
* // Option 1: Pass wasm objects directly (auto-converts to protobuf)
|
|
133
|
+
* const rawTx = builder.build();
|
|
134
|
+
* const txNotes = builder.allNotes();
|
|
135
|
+
*
|
|
136
|
+
* const signedTx = await provider.signRawTx({
|
|
137
|
+
* rawTx: rawTx, // wasm RawTx object
|
|
138
|
+
* notes: txNotes.notes, // array of wasm Note objects
|
|
139
|
+
* spendConditions: txNotes.spendConditions // array of wasm SpendCondition objects
|
|
140
|
+
* });
|
|
141
|
+
*
|
|
142
|
+
* // Option 2: Pass protobuf JS objects directly
|
|
143
|
+
* const signedTx = await provider.signRawTx({
|
|
144
|
+
* rawTx: rawTxProtobufObject, // protobuf JS object
|
|
145
|
+
* notes: noteProtobufObjects, // array of protobuf JS objects
|
|
146
|
+
* spendConditions: spendCondProtobufObjects // array of protobuf JS objects
|
|
147
|
+
* });
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
async signRawTx(params) {
|
|
151
|
+
if (!this.isConnected) {
|
|
152
|
+
throw new NoAccountError();
|
|
153
|
+
}
|
|
154
|
+
// Helper to convert to protobuf if it's a wasm object
|
|
155
|
+
const toProtobuf = (obj) => {
|
|
156
|
+
// If object has toProtobuf method, it's a wasm object - convert it
|
|
157
|
+
if (obj && typeof obj.toProtobuf === 'function') {
|
|
158
|
+
return obj.toProtobuf();
|
|
159
|
+
}
|
|
160
|
+
// Otherwise assume it's already a protobuf JS object
|
|
161
|
+
return obj;
|
|
162
|
+
};
|
|
163
|
+
// Convert wasm objects to protobuf (if needed)
|
|
164
|
+
const protobufParams = {
|
|
165
|
+
rawTx: toProtobuf(params.rawTx),
|
|
166
|
+
notes: params.notes.map(toProtobuf),
|
|
167
|
+
spendConditions: params.spendConditions.map(toProtobuf),
|
|
168
|
+
};
|
|
169
|
+
return this.request({
|
|
170
|
+
method: PROVIDER_METHODS.SIGN_RAW_TX,
|
|
171
|
+
params: [protobufParams],
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Create a new transaction builder
|
|
176
|
+
* @returns A new TransactionBuilder instance
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```typescript
|
|
180
|
+
* const tx = provider.transaction()
|
|
181
|
+
* .to('recipient_address')
|
|
182
|
+
* .amount(1_000_000)
|
|
183
|
+
* .fee(50_000)
|
|
184
|
+
* .build();
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
transaction() {
|
|
188
|
+
return new TransactionBuilder();
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Add an event listener for wallet events
|
|
192
|
+
* @param event - The event to listen for
|
|
193
|
+
* @param listener - The callback function to invoke when the event occurs
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* ```typescript
|
|
197
|
+
* provider.on('accountsChanged', (accounts) => {
|
|
198
|
+
* console.log('Accounts changed:', accounts);
|
|
199
|
+
* });
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
on(event, listener) {
|
|
203
|
+
if (!this.eventListeners.has(event)) {
|
|
204
|
+
this.eventListeners.set(event, new Set());
|
|
205
|
+
}
|
|
206
|
+
this.eventListeners.get(event).add(listener);
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Remove an event listener
|
|
210
|
+
* @param event - The event to stop listening for
|
|
211
|
+
* @param listener - The callback function to remove
|
|
212
|
+
*/
|
|
213
|
+
off(event, listener) {
|
|
214
|
+
const listeners = this.eventListeners.get(event);
|
|
215
|
+
if (listeners) {
|
|
216
|
+
listeners.delete(listener);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Remove all event listeners for a specific event or all events
|
|
221
|
+
* @param event - Optional event to remove listeners for (removes all if not specified)
|
|
222
|
+
*/
|
|
223
|
+
removeAllListeners(event) {
|
|
224
|
+
if (event) {
|
|
225
|
+
this.eventListeners.delete(event);
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
this.eventListeners.clear();
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Make a raw RPC request to the wallet extension (EIP-1193 compatible)
|
|
233
|
+
* @param args - The RPC request arguments
|
|
234
|
+
* @returns Promise resolving to the result
|
|
235
|
+
* @throws {UserRejectedError} If the user rejects the request
|
|
236
|
+
* @throws {RpcError} If the RPC call fails
|
|
237
|
+
*/
|
|
238
|
+
async request(args) {
|
|
239
|
+
try {
|
|
240
|
+
const result = await this.injected.request(args);
|
|
241
|
+
return result;
|
|
242
|
+
}
|
|
243
|
+
catch (error) {
|
|
244
|
+
// Handle RPC errors and map known error codes
|
|
245
|
+
if (error && typeof error === 'object' && 'code' in error && 'message' in error) {
|
|
246
|
+
const { code, message, data } = error;
|
|
247
|
+
const rpcError = new RpcError(code, message, data);
|
|
248
|
+
// Map EIP-1193 error codes to typed errors
|
|
249
|
+
if (this.isUserRejected(rpcError)) {
|
|
250
|
+
throw new UserRejectedError(message);
|
|
251
|
+
}
|
|
252
|
+
throw rpcError;
|
|
253
|
+
}
|
|
254
|
+
// Re-throw other errors as-is
|
|
255
|
+
throw error;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Check if an error represents user rejection
|
|
260
|
+
* Uses EIP-1193 standard error code 4001
|
|
261
|
+
*/
|
|
262
|
+
isUserRejected(error) {
|
|
263
|
+
// EIP-1193 standard: 4001 = User Rejected Request
|
|
264
|
+
const USER_REJECTED_CODES = new Set([4001]);
|
|
265
|
+
if (error instanceof RpcError) {
|
|
266
|
+
return USER_REJECTED_CODES.has(error.code);
|
|
267
|
+
}
|
|
268
|
+
// Fallback: check message for common rejection phrases
|
|
269
|
+
if (error instanceof Error) {
|
|
270
|
+
return /reject|denied|cancel/i.test(error.message);
|
|
271
|
+
}
|
|
272
|
+
return false;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Set up event listeners for wallet events
|
|
276
|
+
* This listens for events from the extension and forwards them to registered listeners
|
|
277
|
+
*/
|
|
278
|
+
setupEventListeners() {
|
|
279
|
+
if (typeof window === 'undefined')
|
|
280
|
+
return;
|
|
281
|
+
this._messageHandler = (event) => {
|
|
282
|
+
// Only accept messages from the same window
|
|
283
|
+
if (event.source !== window)
|
|
284
|
+
return;
|
|
285
|
+
const payload = event.data;
|
|
286
|
+
// SECURITY: Verify the message is from Iris extension
|
|
287
|
+
// This prevents malicious scripts from forging wallet events
|
|
288
|
+
if (!payload || payload.__iris !== true)
|
|
289
|
+
return;
|
|
290
|
+
// Check if this is a valid wallet event
|
|
291
|
+
if (typeof payload.type !== 'string' || !payload.type.startsWith('nockchain_'))
|
|
292
|
+
return;
|
|
293
|
+
const eventType = payload.type.replace('nockchain_', '');
|
|
294
|
+
const eventData = payload.data;
|
|
295
|
+
// Update internal state based on event type
|
|
296
|
+
if (eventType === 'accountsChanged' && Array.isArray(eventData)) {
|
|
297
|
+
this._accounts = eventData;
|
|
298
|
+
}
|
|
299
|
+
else if (eventType === 'chainChanged' && typeof eventData === 'string') {
|
|
300
|
+
this._chainId = eventData;
|
|
301
|
+
}
|
|
302
|
+
else if (eventType === 'disconnect') {
|
|
303
|
+
// Clear state on disconnect
|
|
304
|
+
this._accounts = [];
|
|
305
|
+
this._chainId = null;
|
|
306
|
+
}
|
|
307
|
+
// Emit to registered listeners
|
|
308
|
+
this.emit(eventType, eventData);
|
|
309
|
+
};
|
|
310
|
+
window.addEventListener('message', this._messageHandler);
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Clean up event listeners and resources
|
|
314
|
+
* Call this when the provider is no longer needed (e.g., on component unmount)
|
|
315
|
+
*/
|
|
316
|
+
dispose() {
|
|
317
|
+
if (this._messageHandler && typeof window !== 'undefined') {
|
|
318
|
+
window.removeEventListener('message', this._messageHandler);
|
|
319
|
+
this._messageHandler = undefined;
|
|
320
|
+
}
|
|
321
|
+
this.removeAllListeners();
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Emit an event to all registered listeners
|
|
325
|
+
* @param event - The event to emit
|
|
326
|
+
* @param data - The data to pass to listeners
|
|
327
|
+
*/
|
|
328
|
+
emit(event, data) {
|
|
329
|
+
const listeners = this.eventListeners.get(event);
|
|
330
|
+
if (listeners) {
|
|
331
|
+
listeners.forEach(listener => {
|
|
332
|
+
try {
|
|
333
|
+
listener(data);
|
|
334
|
+
}
|
|
335
|
+
catch (error) {
|
|
336
|
+
console.error(`Error in ${event} listener:`, error);
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Check if the Iris extension is installed and authentic
|
|
343
|
+
* @returns true if the extension is installed
|
|
344
|
+
*/
|
|
345
|
+
static isInstalled() {
|
|
346
|
+
// TODO: Support other providers
|
|
347
|
+
return typeof window !== 'undefined' && window.nockchain?.provider === 'iris';
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
//# sourceMappingURL=provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAA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AGG;IACI,OAAO;QACZ,IAAI,IAAI,CAAC,eAAe,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;YAC1D,MAAM,CAAC,mBAAmB,CAAC,SAAS,EAAE,IAAI,CAAC,eAAe,CAAC,CAAC;YAC5D,IAAI,CAAC,eAAe,GAAG,SAAS,CAAC;QACnC,CAAC;QACD,IAAI,CAAC,kBAAkB,EAAE,CAAC;IAC5B,CAAC;IAED;;;;OAIG;IACK,IAAI,CAAc,KAAqB,EAAE,IAAO;QACtD,MAAM,SAAS,GAAG,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACjD,IAAI,SAAS,EAAE,CAAC;YACd,SAAS,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;gBAC3B,IAAI,CAAC;oBACH,QAAQ,CAAC,IAAI,CAAC,CAAC;gBACjB,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,OAAO,CAAC,KAAK,CAAC,YAAY,KAAK,YAAY,EAAE,KAAK,CAAC,CAAC;gBACtD,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,WAAW;QAChB,gCAAgC;QAChC,OAAO,OAAO,MAAM,KAAK,WAAW,IAAI,MAAM,CAAC,SAAS,EAAE,QAAQ,KAAK,MAAM,CAAC;IAChF,CAAC;CACF"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transaction builder with fluent API for constructing Nockchain transactions
|
|
3
|
+
*/
|
|
4
|
+
import type { Transaction } from './types.js';
|
|
5
|
+
/**
|
|
6
|
+
* Conversion rate: 1 NOCK = 65,536 nicks (2^16)
|
|
7
|
+
*/
|
|
8
|
+
export declare const NOCK_TO_NICKS = 65536;
|
|
9
|
+
/**
|
|
10
|
+
* Default transaction fee in nicks (32,768 nicks = 0.5 NOCK)
|
|
11
|
+
*/
|
|
12
|
+
export declare const DEFAULT_FEE = 32768;
|
|
13
|
+
/**
|
|
14
|
+
* Minimum amount in nicks (must be positive)
|
|
15
|
+
*/
|
|
16
|
+
export declare const MIN_AMOUNT = 1;
|
|
17
|
+
/**
|
|
18
|
+
* TransactionBuilder class implementing the builder pattern for type-safe transaction construction
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* const tx = new TransactionBuilder()
|
|
23
|
+
* .to('nock1recipient_address')
|
|
24
|
+
* .amount(1_000_000)
|
|
25
|
+
* .fee(50_000)
|
|
26
|
+
* .build();
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare class TransactionBuilder {
|
|
30
|
+
private _to?;
|
|
31
|
+
private _amount?;
|
|
32
|
+
private _fee?;
|
|
33
|
+
/**
|
|
34
|
+
* Set the recipient address for the transaction
|
|
35
|
+
* @param address - Base58-encoded Nockchain V1 PKH address (40 bytes, ~54-55 chars)
|
|
36
|
+
* @returns A new TransactionBuilder instance with the address set
|
|
37
|
+
* @throws {InvalidAddressError} If the address format is invalid
|
|
38
|
+
*/
|
|
39
|
+
to(address: string): TransactionBuilder;
|
|
40
|
+
/**
|
|
41
|
+
* Set the amount to send in nicks (1 NOCK = 65,536 nicks)
|
|
42
|
+
* @param nicks - Amount in nicks (must be a positive integer)
|
|
43
|
+
* @returns A new TransactionBuilder instance with the amount set
|
|
44
|
+
* @throws {InvalidTransactionError} If the amount is invalid
|
|
45
|
+
*/
|
|
46
|
+
amount(nicks: number): TransactionBuilder;
|
|
47
|
+
/**
|
|
48
|
+
* Set the transaction fee in nicks (optional, defaults to 32,768 nicks)
|
|
49
|
+
* @param nicks - Fee amount in nicks (must be a positive integer)
|
|
50
|
+
* @returns A new TransactionBuilder instance with the fee set
|
|
51
|
+
* @throws {InvalidTransactionError} If the fee is invalid
|
|
52
|
+
*/
|
|
53
|
+
fee(nicks: number): TransactionBuilder;
|
|
54
|
+
/**
|
|
55
|
+
* Build and validate the transaction
|
|
56
|
+
* @returns The constructed Transaction object
|
|
57
|
+
* @throws {InvalidTransactionError} If required fields are missing
|
|
58
|
+
*/
|
|
59
|
+
build(): Transaction;
|
|
60
|
+
/**
|
|
61
|
+
* Validate a Nockchain V1 PKH address format
|
|
62
|
+
* V1 PKH addresses are TIP5 hash (40 bytes) of public key, base58-encoded
|
|
63
|
+
*
|
|
64
|
+
* Validates by decoding the base58 string and checking for exactly 40 bytes
|
|
65
|
+
* rather than relying on character count which can vary
|
|
66
|
+
*
|
|
67
|
+
* @param address - The address to validate
|
|
68
|
+
* @returns true if valid, false otherwise
|
|
69
|
+
*/
|
|
70
|
+
private isValidAddress;
|
|
71
|
+
/**
|
|
72
|
+
* Create a transaction builder from an existing transaction object
|
|
73
|
+
* Useful for modifying existing transactions
|
|
74
|
+
* @param tx - The transaction to create a builder from
|
|
75
|
+
* @returns A new TransactionBuilder instance with values from the transaction
|
|
76
|
+
* @throws {InvalidAddressError} If the transaction address is invalid
|
|
77
|
+
* @throws {InvalidTransactionError} If the transaction amount or fee is invalid
|
|
78
|
+
*/
|
|
79
|
+
static fromTransaction(tx: Transaction): TransactionBuilder;
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=transaction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transaction.d.ts","sourceRoot":"","sources":["../src/transaction.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAG9C;;GAEG;AACH,eAAO,MAAM,aAAa,QAAS,CAAC;AAEpC;;GAEG;AACH,eAAO,MAAM,WAAW,QAAS,CAAC;AAElC;;GAEG;AACH,eAAO,MAAM,UAAU,IAAI,CAAC;AAE5B;;;;;;;;;;;GAWG;AACH,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,GAAG,CAAC,CAAS;IACrB,OAAO,CAAC,OAAO,CAAC,CAAS;IACzB,OAAO,CAAC,IAAI,CAAC,CAAS;IAEtB;;;;;OAKG;IACH,EAAE,CAAC,OAAO,EAAE,MAAM,GAAG,kBAAkB;IAYvC;;;;;OAKG;IACH,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,kBAAkB;IAgBzC;;;;;OAKG;IACH,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,kBAAkB;IAetC;;;;OAIG;IACH,KAAK,IAAI,WAAW;IAepB;;;;;;;;;OASG;IACH,OAAO,CAAC,cAAc;IAatB;;;;;;;OAOG;IACH,MAAM,CAAC,eAAe,CAAC,EAAE,EAAE,WAAW,GAAG,kBAAkB;CAU5D"}
|