@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.
@@ -0,0 +1,144 @@
1
+ /**
2
+ * Transaction builder with fluent API for constructing Nockchain transactions
3
+ */
4
+ import { base58 } from '@scure/base';
5
+ import { InvalidAddressError, InvalidTransactionError } from './errors.js';
6
+ /**
7
+ * Conversion rate: 1 NOCK = 65,536 nicks (2^16)
8
+ */
9
+ export const NOCK_TO_NICKS = 65536;
10
+ /**
11
+ * Default transaction fee in nicks (32,768 nicks = 0.5 NOCK)
12
+ */
13
+ export const DEFAULT_FEE = 32768;
14
+ /**
15
+ * Minimum amount in nicks (must be positive)
16
+ */
17
+ export const MIN_AMOUNT = 1;
18
+ /**
19
+ * TransactionBuilder class implementing the builder pattern for type-safe transaction construction
20
+ *
21
+ * @example
22
+ * ```typescript
23
+ * const tx = new TransactionBuilder()
24
+ * .to('nock1recipient_address')
25
+ * .amount(1_000_000)
26
+ * .fee(50_000)
27
+ * .build();
28
+ * ```
29
+ */
30
+ export class TransactionBuilder {
31
+ /**
32
+ * Set the recipient address for the transaction
33
+ * @param address - Base58-encoded Nockchain V1 PKH address (40 bytes, ~54-55 chars)
34
+ * @returns A new TransactionBuilder instance with the address set
35
+ * @throws {InvalidAddressError} If the address format is invalid
36
+ */
37
+ to(address) {
38
+ if (!this.isValidAddress(address)) {
39
+ throw new InvalidAddressError(address);
40
+ }
41
+ const builder = new TransactionBuilder();
42
+ builder._to = address;
43
+ builder._amount = this._amount;
44
+ builder._fee = this._fee;
45
+ return builder;
46
+ }
47
+ /**
48
+ * Set the amount to send in nicks (1 NOCK = 65,536 nicks)
49
+ * @param nicks - Amount in nicks (must be a positive integer)
50
+ * @returns A new TransactionBuilder instance with the amount set
51
+ * @throws {InvalidTransactionError} If the amount is invalid
52
+ */
53
+ amount(nicks) {
54
+ if (!Number.isInteger(nicks)) {
55
+ throw new InvalidTransactionError('Amount must be an integer');
56
+ }
57
+ if (nicks < MIN_AMOUNT) {
58
+ throw new InvalidTransactionError(`Amount must be at least ${MIN_AMOUNT} nick`);
59
+ }
60
+ // Note: MIN_AMOUNT = 1, so the above check already covers negative amounts
61
+ const builder = new TransactionBuilder();
62
+ builder._to = this._to;
63
+ builder._amount = nicks;
64
+ builder._fee = this._fee;
65
+ return builder;
66
+ }
67
+ /**
68
+ * Set the transaction fee in nicks (optional, defaults to 32,768 nicks)
69
+ * @param nicks - Fee amount in nicks (must be a positive integer)
70
+ * @returns A new TransactionBuilder instance with the fee set
71
+ * @throws {InvalidTransactionError} If the fee is invalid
72
+ */
73
+ fee(nicks) {
74
+ if (!Number.isInteger(nicks)) {
75
+ throw new InvalidTransactionError('Fee must be an integer');
76
+ }
77
+ if (nicks < 0) {
78
+ throw new InvalidTransactionError('Fee must be non-negative');
79
+ }
80
+ const builder = new TransactionBuilder();
81
+ builder._to = this._to;
82
+ builder._amount = this._amount;
83
+ builder._fee = nicks;
84
+ return builder;
85
+ }
86
+ /**
87
+ * Build and validate the transaction
88
+ * @returns The constructed Transaction object
89
+ * @throws {InvalidTransactionError} If required fields are missing
90
+ */
91
+ build() {
92
+ if (!this._to) {
93
+ throw new InvalidTransactionError('Missing required field: to (recipient address)');
94
+ }
95
+ if (this._amount === undefined || this._amount === null) {
96
+ throw new InvalidTransactionError('Missing required field: amount');
97
+ }
98
+ return {
99
+ to: this._to,
100
+ amount: this._amount,
101
+ fee: this._fee ?? DEFAULT_FEE,
102
+ };
103
+ }
104
+ /**
105
+ * Validate a Nockchain V1 PKH address format
106
+ * V1 PKH addresses are TIP5 hash (40 bytes) of public key, base58-encoded
107
+ *
108
+ * Validates by decoding the base58 string and checking for exactly 40 bytes
109
+ * rather than relying on character count which can vary
110
+ *
111
+ * @param address - The address to validate
112
+ * @returns true if valid, false otherwise
113
+ */
114
+ isValidAddress(address) {
115
+ try {
116
+ const trimmed = (address || '').trim();
117
+ if (trimmed.length === 0)
118
+ return false;
119
+ const bytes = base58.decode(trimmed);
120
+ return bytes.length === 40;
121
+ }
122
+ catch {
123
+ // Invalid base58 encoding
124
+ return false;
125
+ }
126
+ }
127
+ /**
128
+ * Create a transaction builder from an existing transaction object
129
+ * Useful for modifying existing transactions
130
+ * @param tx - The transaction to create a builder from
131
+ * @returns A new TransactionBuilder instance with values from the transaction
132
+ * @throws {InvalidAddressError} If the transaction address is invalid
133
+ * @throws {InvalidTransactionError} If the transaction amount or fee is invalid
134
+ */
135
+ static fromTransaction(tx) {
136
+ // Use setters to ensure validation is applied
137
+ let builder = new TransactionBuilder().to(tx.to).amount(tx.amount);
138
+ if (typeof tx.fee === 'number') {
139
+ builder = builder.fee(tx.fee);
140
+ }
141
+ return builder;
142
+ }
143
+ }
144
+ //# sourceMappingURL=transaction.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transaction.js","sourceRoot":"","sources":["../src/transaction.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EAAE,mBAAmB,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AAE3E;;GAEG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,KAAM,CAAC;AAEpC;;GAEG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,KAAM,CAAC;AAElC;;GAEG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,CAAC;AAE5B;;;;;;;;;;;GAWG;AACH,MAAM,OAAO,kBAAkB;IAK7B;;;;;OAKG;IACH,EAAE,CAAC,OAAe;QAChB,IAAI,CAAC,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,EAAE,CAAC;YAClC,MAAM,IAAI,mBAAmB,CAAC,OAAO,CAAC,CAAC;QACzC,CAAC;QAED,MAAM,OAAO,GAAG,IAAI,kBAAkB,EAAE,CAAC;QACzC,OAAO,CAAC,GAAG,GAAG,OAAO,CAAC;QACtB,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC,O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*/
4
+ /**
5
+ * Transaction object representing a Nockchain transaction
6
+ */
7
+ export interface Transaction {
8
+ /** Recipient address (base58-encoded public key hash / PKH) */
9
+ to: string;
10
+ /** Amount to send in nicks (1 NOCK = 65,536 nicks) */
11
+ amount: number;
12
+ /** Transaction fee in nicks (optional, defaults to 32,768 = 0.5 NOCK) */
13
+ fee?: number;
14
+ }
15
+ /**
16
+ * RPC request object for communicating with the extension
17
+ */
18
+ export interface RpcRequest {
19
+ /** The RPC method to call */
20
+ method: string;
21
+ /** Optional parameters for the method */
22
+ params?: unknown[];
23
+ /** Optional timeout for the request */
24
+ timeout?: number;
25
+ }
26
+ /**
27
+ * RPC response object from the extension
28
+ */
29
+ export interface RpcResponse<T = unknown> {
30
+ /** The result of the RPC call */
31
+ result?: T;
32
+ /** Error information if the call failed */
33
+ error?: {
34
+ code: number;
35
+ message: string;
36
+ data?: unknown;
37
+ };
38
+ }
39
+ /**
40
+ * Event types that the provider can emit
41
+ */
42
+ export type NockchainEvent = 'accountsChanged' | 'chainChanged' | 'connect' | 'disconnect';
43
+ /**
44
+ * Event listener callback function
45
+ */
46
+ export type EventListener<T = unknown> = (data: T) => void;
47
+ /**
48
+ * Interface for the injected window.nockchain object
49
+ */
50
+ export interface InjectedNockchain {
51
+ /**
52
+ * Make an RPC request to the wallet extension
53
+ * @param request - The RPC request object
54
+ * @returns Promise resolving to the result
55
+ */
56
+ request<T = unknown>(request: RpcRequest): Promise<T>;
57
+ /**
58
+ * Provider name (e.g., 'iris')
59
+ */
60
+ provider?: string;
61
+ /**
62
+ * Provider version
63
+ */
64
+ version?: string;
65
+ }
66
+ /**
67
+ * Extended Window interface with nockchain property
68
+ */
69
+ declare global {
70
+ interface Window {
71
+ nockchain?: InjectedNockchain;
72
+ }
73
+ }
74
+ /**
75
+ * Chain information
76
+ */
77
+ export interface ChainInfo {
78
+ /** Chain ID (e.g., 'mainnet', 'testnet') */
79
+ chainId: string;
80
+ /** Network name */
81
+ name: string;
82
+ }
83
+ /**
84
+ * Account information
85
+ */
86
+ export interface AccountInfo {
87
+ /** Account address */
88
+ address: string;
89
+ /** Account balance in nicks (optional, may not be available) */
90
+ balance?: number;
91
+ }
92
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,+DAA+D;IAC/D,EAAE,EAAE,MAAM,CAAC;IACX,sDAAsD;IACtD,MAAM,EAAE,MAAM,CAAC;IACf,yEAAyE;IACzE,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,6BAA6B;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,yCAAyC;IACzC,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC;IACnB,uCAAuC;IACvC,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,WAAW,CAAC,CAAC,GAAG,OAAO;IACtC,iCAAiC;IACjC,MAAM,CAAC,EAAE,CAAC,CAAC;IACX,2CAA2C;IAC3C,KAAK,CAAC,EAAE;QACN,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE,MAAM,CAAC;QAChB,IAAI,CAAC,EAAE,OAAO,CAAC;KAChB,CAAC;CACH;AAED;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,iBAAiB,GAAG,cAAc,GAAG,SAAS,GAAG,YAAY,CAAC;AAE3F;;GAEG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,GAAG,OAAO,IAAI,CAAC,IAAI,EAAE,CAAC,KAAK,IAAI,CAAC;AAE3D;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC;;;;OAIG;IACH,OAAO,CAAC,CAAC,GAAG,OAAO,EAAE,OAAO,EAAE,UAAU,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;IAEtD;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;GAEG;AACH,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,SAAS,CAAC,EAAE,iBAAiB,CAAC;KAC/B;CACF;AAED;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,4CAA4C;IAC5C,OAAO,EAAE,MAAM,CAAC;IAChB,mBAAmB;IACnB,IAAI,EAAE,MAAM,CAAC;CACd;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,sBAAsB;IACtB,OAAO,EAAE,MAAM,CAAC;IAChB,gEAAgE;IAChE,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB"}
package/dist/types.js ADDED
@@ -0,0 +1,5 @@
1
+ /**
2
+ * TypeScript type definitions for Iris SDK
3
+ */
4
+ export {};
5
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG"}
package/dist/wasm.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * `@nockchain/sdk/wasm`
3
+ *
4
+ * Convenience re-export of the Rose WASM package so consumers can import:
5
+ * - `@nockchain/sdk/wasm`
6
+ * instead of:
7
+ * - `@nockchain/rose-wasm/rose_wasm.js`
8
+ *
9
+ * This mirrors the "SDK owns WASM surface" pattern used elsewhere in the repo.
10
+ */
11
+ export * from '@nockchain/rose-wasm/rose_wasm.js';
12
+ export { default } from '@nockchain/rose-wasm/rose_wasm.js';
13
+ //# sourceMappingURL=wasm.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wasm.d.ts","sourceRoot":"","sources":["../src/wasm.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,cAAc,mCAAmC,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,MAAM,mCAAmC,CAAC"}
package/dist/wasm.js ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * `@nockchain/sdk/wasm`
3
+ *
4
+ * Convenience re-export of the Rose WASM package so consumers can import:
5
+ * - `@nockchain/sdk/wasm`
6
+ * instead of:
7
+ * - `@nockchain/rose-wasm/rose_wasm.js`
8
+ *
9
+ * This mirrors the "SDK owns WASM surface" pattern used elsewhere in the repo.
10
+ */
11
+ export * from '@nockchain/rose-wasm/rose_wasm.js';
12
+ export { default } from '@nockchain/rose-wasm/rose_wasm.js';
13
+ //# sourceMappingURL=wasm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wasm.js","sourceRoot":"","sources":["../src/wasm.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,cAAc,mCAAmC,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,MAAM,mCAAmC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "@nockchain/sdk",
3
+ "version": "0.1.4-nightly",
4
+ "description": "TypeScript SDK for interacting with Nockchain wallet extensions",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ },
13
+ "./wasm": {
14
+ "types": "./dist/wasm.d.ts",
15
+ "import": "./dist/wasm.js"
16
+ },
17
+ "./hooks": {
18
+ "types": "./dist/hooks/index.d.ts",
19
+ "import": "./dist/hooks/index.js"
20
+ },
21
+ "./hooks/use-rose": {
22
+ "types": "./dist/hooks/use-rose.d.ts",
23
+ "import": "./dist/hooks/use-rose.js"
24
+ }
25
+ },
26
+ "files": [
27
+ "dist",
28
+ "README.md"
29
+ ],
30
+ "keywords": [
31
+ "nockchain",
32
+ "wallet",
33
+ "web3",
34
+ "blockchain",
35
+ "schnorr"
36
+ ],
37
+ "author": "Nockchain.net LLC",
38
+ "license": "MIT",
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "git+https://github.com/nocktoshi/rose.git",
42
+ "directory": "sdk"
43
+ },
44
+ "scripts": {
45
+ "build": "tsc",
46
+ "build-example": "npm run build && tsc --project examples/tsconfig.json && vite build --config vite.config.examples.ts",
47
+ "dev": "tsc --watch",
48
+ "dev-example": "vite --config vite.config.examples.ts",
49
+ "format": "prettier --write .",
50
+ "format:check": "prettier --check .",
51
+ "typecheck": "tsc --noEmit",
52
+ "prepublishOnly": "npm run build"
53
+ },
54
+ "dependencies": {
55
+ "@nockchain/rose-wasm": "^0.1.4-nightly.13.1.1bd3e0f",
56
+ "@scure/base": "2.0.0"
57
+ },
58
+ "devDependencies": {
59
+ "@types/react": "19.2.7",
60
+ "prettier": "3.7.4",
61
+ "react": "19.2.3",
62
+ "typescript": "5.9.3",
63
+ "vite": "7.3.0"
64
+ },
65
+ "peerDependencies": {
66
+ "react": ">=18"
67
+ }
68
+ }