@bloxchain/sdk 1.0.0-alpha.6 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +411 -376
  3. package/abi/AccountBlox.abi.json +1895 -1833
  4. package/abi/BaseStateMachine.abi.json +773 -822
  5. package/abi/CopyBlox.abi.json +1609 -0
  6. package/abi/EngineBlox.abi.json +562 -552
  7. package/abi/GuardController.abi.json +1597 -1609
  8. package/abi/GuardControllerDefinitions.abi.json +257 -120
  9. package/abi/IDefinition.abi.json +57 -47
  10. package/abi/RoleBlox.abi.json +2993 -2982
  11. package/abi/RuntimeRBAC.abi.json +841 -842
  12. package/abi/RuntimeRBACDefinitions.abi.json +265 -99
  13. package/abi/SecureBlox.abi.json +2752 -2752
  14. package/abi/SecureOwnable.abi.json +1365 -1349
  15. package/abi/SecureOwnableDefinitions.abi.json +174 -164
  16. package/dist/abi/AccountBlox.abi.json +3978 -0
  17. package/dist/abi/BareBlox.abi.json +1378 -0
  18. package/dist/abi/BaseStateMachine.abi.json +1281 -1330
  19. package/dist/abi/CopyBlox.abi.json +1609 -0
  20. package/dist/abi/EngineBlox.abi.json +862 -0
  21. package/dist/abi/GuardController.abi.json +2325 -2337
  22. package/dist/abi/GuardControllerDefinitions.abi.json +416 -0
  23. package/dist/abi/IDefinition.abi.json +122 -112
  24. package/dist/abi/RoleBlox.abi.json +2994 -0
  25. package/dist/abi/RuntimeRBAC.abi.json +1629 -1630
  26. package/dist/abi/RuntimeRBACDefinitions.abi.json +386 -0
  27. package/dist/abi/SecureBlox.abi.json +2753 -0
  28. package/dist/abi/SecureOwnable.abi.json +2735 -2719
  29. package/dist/abi/SecureOwnableDefinitions.abi.json +421 -0
  30. package/{abi → dist/abi}/SimpleRWA20.abi.json +52 -74
  31. package/{abi → dist/abi}/SimpleRWA20Definitions.abi.json +5 -0
  32. package/{abi → dist/abi}/SimpleVault.abi.json +54 -76
  33. package/{abi → dist/abi}/SimpleVaultDefinitions.abi.json +5 -0
  34. package/dist/abi.d.ts +11 -0
  35. package/dist/abi.d.ts.map +1 -0
  36. package/dist/abi.js +10 -0
  37. package/dist/abi.js.map +1 -0
  38. package/dist/contracts/core/BaseStateMachine.d.ts +7 -8
  39. package/dist/contracts/core/BaseStateMachine.d.ts.map +1 -1
  40. package/dist/contracts/core/BaseStateMachine.js +77 -42
  41. package/dist/contracts/core/BaseStateMachine.js.map +1 -1
  42. package/dist/contracts/core/GuardController.d.ts +21 -14
  43. package/dist/contracts/core/GuardController.d.ts.map +1 -1
  44. package/dist/contracts/core/GuardController.js +18 -20
  45. package/dist/contracts/core/GuardController.js.map +1 -1
  46. package/dist/contracts/core/RuntimeRBAC.d.ts +5 -18
  47. package/dist/contracts/core/RuntimeRBAC.d.ts.map +1 -1
  48. package/dist/contracts/core/RuntimeRBAC.js +4 -21
  49. package/dist/contracts/core/RuntimeRBAC.js.map +1 -1
  50. package/dist/contracts/core/SecureOwnable.d.ts +14 -16
  51. package/dist/contracts/core/SecureOwnable.d.ts.map +1 -1
  52. package/dist/contracts/core/SecureOwnable.js +21 -24
  53. package/dist/contracts/core/SecureOwnable.js.map +1 -1
  54. package/dist/index.d.ts +32 -29
  55. package/dist/index.d.ts.map +1 -1
  56. package/dist/index.js +28 -26
  57. package/dist/index.js.map +1 -1
  58. package/dist/interfaces/base.index.d.ts +10 -2
  59. package/dist/interfaces/base.index.d.ts.map +1 -1
  60. package/dist/interfaces/base.state.machine.index.d.ts +4 -6
  61. package/dist/interfaces/base.state.machine.index.d.ts.map +1 -1
  62. package/dist/interfaces/core.access.index.d.ts +4 -9
  63. package/dist/interfaces/core.access.index.d.ts.map +1 -1
  64. package/dist/interfaces/core.execution.index.d.ts +9 -12
  65. package/dist/interfaces/core.execution.index.d.ts.map +1 -1
  66. package/dist/interfaces/core.security.index.d.ts +4 -7
  67. package/dist/interfaces/core.security.index.d.ts.map +1 -1
  68. package/dist/interfaces/definition.index.d.ts +1 -1
  69. package/dist/interfaces/definition.index.d.ts.map +1 -1
  70. package/dist/interfaces/lib.index.d.ts +2 -2
  71. package/dist/interfaces/lib.index.d.ts.map +1 -1
  72. package/dist/lib/Definition.d.ts +2 -2
  73. package/dist/lib/Definition.d.ts.map +1 -1
  74. package/dist/lib/Definition.js +9 -2
  75. package/dist/lib/Definition.js.map +1 -1
  76. package/dist/lib/EngineBlox.d.ts +23 -48
  77. package/dist/lib/EngineBlox.d.ts.map +1 -1
  78. package/dist/lib/EngineBlox.js +61 -136
  79. package/dist/lib/EngineBlox.js.map +1 -1
  80. package/dist/lib/definitions/GuardControllerDefinitions.d.ts +30 -13
  81. package/dist/lib/definitions/GuardControllerDefinitions.d.ts.map +1 -1
  82. package/dist/lib/definitions/GuardControllerDefinitions.js +58 -27
  83. package/dist/lib/definitions/GuardControllerDefinitions.js.map +1 -1
  84. package/dist/lib/definitions/RuntimeRBACDefinitions.d.ts +44 -13
  85. package/dist/lib/definitions/RuntimeRBACDefinitions.d.ts.map +1 -1
  86. package/dist/lib/definitions/RuntimeRBACDefinitions.js +68 -32
  87. package/dist/lib/definitions/RuntimeRBACDefinitions.js.map +1 -1
  88. package/dist/lib/definitions/SecureOwnableDefinitions.d.ts +12 -7
  89. package/dist/lib/definitions/SecureOwnableDefinitions.d.ts.map +1 -1
  90. package/dist/lib/definitions/SecureOwnableDefinitions.js +25 -9
  91. package/dist/lib/definitions/SecureOwnableDefinitions.js.map +1 -1
  92. package/dist/lib/definitions/index.d.ts +7 -5
  93. package/dist/lib/definitions/index.d.ts.map +1 -1
  94. package/dist/lib/definitions/index.js +6 -5
  95. package/dist/lib/definitions/index.js.map +1 -1
  96. package/dist/types/base.state.machine.index.d.ts +4 -20
  97. package/dist/types/base.state.machine.index.d.ts.map +1 -1
  98. package/dist/types/base.state.machine.index.js +20 -40
  99. package/dist/types/base.state.machine.index.js.map +1 -1
  100. package/dist/types/core.access.index.d.ts +0 -16
  101. package/dist/types/core.access.index.d.ts.map +1 -1
  102. package/dist/types/core.access.index.js +2 -13
  103. package/dist/types/core.access.index.js.map +1 -1
  104. package/dist/types/core.execution.index.d.ts +9 -10
  105. package/dist/types/core.execution.index.d.ts.map +1 -1
  106. package/dist/types/core.execution.index.js +16 -13
  107. package/dist/types/core.execution.index.js.map +1 -1
  108. package/dist/types/core.security.index.d.ts +4 -6
  109. package/dist/types/core.security.index.d.ts.map +1 -1
  110. package/dist/types/core.security.index.js +23 -27
  111. package/dist/types/core.security.index.js.map +1 -1
  112. package/dist/types/definition.index.d.ts +8 -1
  113. package/dist/types/definition.index.d.ts.map +1 -1
  114. package/dist/types/definition.index.js.map +1 -1
  115. package/dist/types/lib.index.d.ts +7 -28
  116. package/dist/types/lib.index.d.ts.map +1 -1
  117. package/dist/types/lib.index.js +11 -34
  118. package/dist/types/lib.index.js.map +1 -1
  119. package/dist/types/meta-transaction.signatures.d.ts +10 -0
  120. package/dist/types/meta-transaction.signatures.d.ts.map +1 -0
  121. package/dist/types/meta-transaction.signatures.js +12 -0
  122. package/dist/types/meta-transaction.signatures.js.map +1 -0
  123. package/dist/types/meta-tx-signatures.d.ts +9 -0
  124. package/dist/types/meta-tx-signatures.d.ts.map +1 -0
  125. package/dist/types/meta-tx-signatures.js +11 -0
  126. package/dist/types/meta-tx-signatures.js.map +1 -0
  127. package/dist/utils/bitmap.d.ts +1 -1
  128. package/dist/utils/bitmap.d.ts.map +1 -1
  129. package/dist/utils/contract-errors.d.ts +125 -12
  130. package/dist/utils/contract-errors.d.ts.map +1 -1
  131. package/dist/utils/contract-errors.js +367 -104
  132. package/dist/utils/contract-errors.js.map +1 -1
  133. package/dist/utils/erc20/erc20Token.d.ts +1 -1
  134. package/dist/utils/erc20/erc20Token.d.ts.map +1 -1
  135. package/dist/utils/erc20/erc20Token.js +5 -1
  136. package/dist/utils/erc20/erc20Token.js.map +1 -1
  137. package/dist/utils/interface-ids.d.ts +5 -31
  138. package/dist/utils/interface-ids.d.ts.map +1 -1
  139. package/dist/utils/interface-ids.js +27 -88
  140. package/dist/utils/interface-ids.js.map +1 -1
  141. package/dist/utils/metaTx/metaTransaction.d.ts +110 -9
  142. package/dist/utils/metaTx/metaTransaction.d.ts.map +1 -1
  143. package/dist/utils/metaTx/metaTransaction.js +146 -26
  144. package/dist/utils/metaTx/metaTransaction.js.map +1 -1
  145. package/dist/utils/validations.d.ts +1 -1
  146. package/dist/utils/validations.d.ts.map +1 -1
  147. package/dist/utils/validations.js +12 -12
  148. package/dist/utils/validations.js.map +1 -1
  149. package/dist/utils/viem-error-handler.d.ts +1 -1
  150. package/dist/utils/viem-error-handler.d.ts.map +1 -1
  151. package/dist/utils/viem-error-handler.js +1 -1
  152. package/dist/utils/viem-error-handler.js.map +1 -1
  153. package/package.json +77 -71
  154. package/dist/contracts/BaseStateMachine.d.ts +0 -92
  155. package/dist/contracts/BaseStateMachine.d.ts.map +0 -1
  156. package/dist/contracts/BaseStateMachine.js +0 -263
  157. package/dist/contracts/BaseStateMachine.js.map +0 -1
  158. package/dist/contracts/GuardController.d.ts +0 -120
  159. package/dist/contracts/GuardController.d.ts.map +0 -1
  160. package/dist/contracts/GuardController.js +0 -150
  161. package/dist/contracts/GuardController.js.map +0 -1
  162. package/dist/contracts/RuntimeRBAC.d.ts +0 -40
  163. package/dist/contracts/RuntimeRBAC.d.ts.map +0 -1
  164. package/dist/contracts/RuntimeRBAC.js +0 -50
  165. package/dist/contracts/RuntimeRBAC.js.map +0 -1
  166. package/dist/contracts/SecureOwnable.d.ts +0 -41
  167. package/dist/contracts/SecureOwnable.d.ts.map +0 -1
  168. package/dist/contracts/SecureOwnable.js +0 -81
  169. package/dist/contracts/SecureOwnable.js.map +0 -1
package/README.md CHANGED
@@ -1,376 +1,411 @@
1
- # Bloxchain Protocol TypeScript SDK
2
-
3
- [![npm version](https://img.shields.io/npm/v/@bloxchain/sdk.svg)](https://www.npmjs.com/package/@bloxchain/sdk)
4
- [![License: MPL-2.0](https://img.shields.io/badge/License-MPL--2.0-blue.svg)](https://opensource.org/licenses/MPL-2.0)
5
- [![TypeScript](https://img.shields.io/badge/TypeScript-5.x-blue.svg)](https://www.typescriptlang.org/)
6
- [![Node](https://img.shields.io/badge/Node-%3E%3D18.0.0-green.svg)](https://nodejs.org/)
7
-
8
- A comprehensive TypeScript SDK for interacting with the Bloxchain Protocol smart contracts, providing type-safe interfaces for secure multi-phase operations, dynamic role-based access control, and state abstraction.
9
-
10
- > **⚠️ EXPERIMENTAL SOFTWARE WARNING**
11
- > This package contains experimental smart contract code and SDK support. While the framework is feature-complete and tested, it is not yet audited for production use. Use at your own risk and do not deploy with real assets without proper security review.
12
-
13
- ## Requirements
14
-
15
- - **Node.js**: >= 18.0.0
16
- - **TypeScript**: 5.x (recommended)
17
- - **Peer dependency**: `viem` ^2.0.0 (required for all SDK usage)
18
- - **Optional peer**: `@bloxchain/contracts` ^1.0.0-alpha (for Solidity/ABI alignment when building apps that use both)
19
-
20
- ## 🏗️ **Unique Architecture**
21
-
22
- Bloxchain Protocol implements a **state machine architecture** with `SecureOperationState` as the core engine, providing:
23
-
24
- - **Centralized State Management**: All security operations flow through a unified state machine
25
- - **Multi-Phase Transaction Processing**: Time-locked operations with request/approval workflows
26
- - **Dynamic Role-Based Access Control**: Flexible, hierarchical permission system
27
- - **Meta-Transaction Support**: Gasless transactions and delegated execution
28
- - **Event-Driven Architecture**: Comprehensive audit trails and external monitoring
29
-
30
- ## Features
31
-
32
- - **SecureOwnable**: Multi-phase ownership management with time-locked operations
33
- - **RuntimeRBAC**: Runtime role-based access control system with batch configuration
34
- - **Definitions**: Dynamic interaction with any definition library implementing IDefinition
35
- - **Guardian**: State abstraction with secure operations
36
- - **Type Safety**: Full TypeScript support with comprehensive type definitions
37
- - **Viem Integration**: Built on top of Viem for modern Ethereum development
38
-
39
- ## 📚 Documentation
40
-
41
- Comprehensive documentation is available in the [`docs/`](./docs/) directory:
42
-
43
- ### **🏗️ Architecture & Design**
44
- - **[Protocol Architecture](./docs/bloxchain-architecture.md)** - Bloxchain protocol overview and design principles
45
- - **[State Machine Engine](./docs/state-machine-engine.md)** - SecureOperationState engine and state management
46
- - **[Architecture Patterns](./docs/architecture-patterns.md)** - Design patterns and best practices
47
-
48
- ### **🚀 Getting Started**
49
- - **[Getting Started](./docs/getting-started.md)** - Quick setup and basic usage
50
- - **[API Reference](./docs/api-reference.md)** - Complete API documentation
51
- - **[SecureOwnable Guide](./docs/secure-ownable.md)** - Ownership management
52
- - **[RuntimeRBAC Guide](./docs/runtime-rbac.md)** - Role-based access control
53
-
54
- ### **🔍 Development Tools**
55
- - **[Best Practices](./docs/best-practices.md)** - Development guidelines
56
- - **[Examples](./docs/examples-basic.md)** - Practical code samples
57
- - **[Types & Interfaces](./docs/types-interfaces.md)** - Type definitions
58
-
59
- **📖 [View All Documentation](./docs/README.md)**
60
-
61
- ## Installation
62
-
63
- ```bash
64
- # Install the SDK and its required peer dependency
65
- npm install @bloxchain/sdk viem
66
-
67
- # Optional: install contracts package when building apps that use both SDK and Solidity
68
- npm install @bloxchain/contracts
69
- ```
70
-
71
- To install from the repository (e.g. for development):
72
-
73
- ```bash
74
- npm install https://github.com/PracticalParticle/Bloxchain-Protocol.git#main --save
75
- ```
76
-
77
- ## Quick Start
78
-
79
- ```typescript
80
- import {
81
- SecureOwnable,
82
- RuntimeRBAC,
83
- GuardController,
84
- Definitions,
85
- type Address,
86
- type PublicClient,
87
- type WalletClient,
88
- type Chain
89
- } from '@bloxchain/sdk';
90
-
91
- // Initialize clients (using your preferred provider)
92
- const publicClient: PublicClient = createPublicClient({...});
93
- const walletClient: WalletClient = createWalletClient({...});
94
- const chain: Chain = mainnet; // or your target chain
95
-
96
- // Initialize SDK classes
97
- const secureOwnable = new SecureOwnable(
98
- publicClient,
99
- walletClient,
100
- contractAddress,
101
- chain
102
- );
103
-
104
- const runtimeRBAC = new RuntimeRBAC(
105
- publicClient,
106
- walletClient,
107
- contractAddress,
108
- chain
109
- );
110
-
111
- const definitions = new Definitions(
112
- publicClient,
113
- walletClient,
114
- definitionsAddress,
115
- chain
116
- );
117
- ```
118
-
119
- ## SecureOwnable Usage
120
-
121
- ### Ownership Management
122
-
123
- ```typescript
124
- // Request ownership transfer
125
- const txResult = await secureOwnable.transferOwnershipRequest({
126
- from: ownerAddress
127
- });
128
-
129
- // Approve after time lock period
130
- const approvalResult = await secureOwnable.transferOwnershipDelayedApproval(
131
- txId,
132
- { from: ownerAddress }
133
- );
134
-
135
- // Cancel ownership transfer
136
- const cancelResult = await secureOwnable.transferOwnershipCancellation(
137
- txId,
138
- { from: ownerAddress }
139
- );
140
- ```
141
-
142
- ### Meta Transactions
143
-
144
- ```typescript
145
- // Create meta transaction parameters
146
- const metaTxParams = await secureOwnable.createMetaTxParams(
147
- handlerContract,
148
- handlerSelector,
149
- deadline,
150
- maxGasPrice,
151
- signer
152
- );
153
-
154
- // Generate unsigned meta transaction
155
- const metaTx = await secureOwnable.generateUnsignedMetaTransactionForNew(
156
- requester,
157
- target,
158
- value,
159
- gasLimit,
160
- operationType,
161
- executionType,
162
- executionOptions,
163
- metaTxParams
164
- );
165
- ```
166
-
167
- ## RuntimeRBAC Usage
168
-
169
- RuntimeRBAC uses batch-based configuration for all role management operations. See the [RuntimeRBAC Guide](./docs/runtime-rbac.md) for complete examples.
170
-
171
- ### Query Functions
172
-
173
- ```typescript
174
- // Get role information
175
- const role = await runtimeRBAC.getRole(roleHash);
176
- console.log(role.roleName, role.maxWallets, role.isProtected);
177
-
178
- // Check if wallet has role
179
- const hasRole = await runtimeRBAC.hasRole(roleHash, walletAddress);
180
-
181
- // Get wallets in role
182
- const wallets = await runtimeRBAC.getWalletsInRole(roleHash);
183
-
184
- // Get roles for a wallet
185
- const walletRoles = await runtimeRBAC.getWalletRoles(walletAddress);
186
-
187
- // Get supported roles
188
- const supportedRoles = await runtimeRBAC.getSupportedRoles();
189
-
190
- // Get active role permissions
191
- const permissions = await runtimeRBAC.getActiveRolePermissions(roleHash);
192
- ```
193
-
194
- ### Batch Configuration
195
-
196
- All role management (create role, add wallet, add permissions, etc.) is done via batch operations. See the [RuntimeRBAC Guide](./docs/runtime-rbac.md) for detailed batch configuration examples.
197
-
198
- ## Definitions Usage
199
-
200
- The `Definitions` class provides dynamic interaction with any definition library that implements the `IDefinition` interface. This allows you to query operation types, function schemas, role permissions, and workflow definitions from any compatible contract.
201
-
202
- ### Basic Usage
203
-
204
- ```typescript
205
- // Initialize Definitions
206
- const definitions = new Definitions(
207
- publicClient,
208
- walletClient,
209
- definitionsAddress,
210
- chain
211
- );
212
-
213
- // Get all operation types
214
- const operationTypes = await definitions.getOperationTypes();
215
- console.log('Available operations:', operationTypes);
216
-
217
- // Get all function schemas
218
- const functionSchemas = await definitions.getFunctionSchemas();
219
- console.log('Function schemas:', functionSchemas);
220
-
221
- // Get role permissions
222
- const rolePermissions = await definitions.getRolePermissions();
223
- console.log('Role permissions:', rolePermissions);
224
- ```
225
-
226
- ### Workflow Management
227
-
228
- ```typescript
229
- // Get all operation workflows
230
- const workflows = await definitions.getOperationWorkflows();
231
- console.log('Available workflows:', workflows);
232
-
233
- // Get workflow for specific operation
234
- const operationType = '0x1234...'; // operation type hash
235
- const workflow = await definitions.getWorkflowForOperation(operationType);
236
- console.log('Workflow for operation:', workflow);
237
-
238
- // Get all workflow paths
239
- const paths = await definitions.getWorkflowPaths();
240
- console.log('Available paths:', paths);
241
- ```
242
-
243
- ### Utility Functions
244
-
245
- ```typescript
246
- // Find operation type by name
247
- const operationType = await definitions.getOperationTypeByName('TRANSFER_OWNERSHIP');
248
- console.log('Operation type hash:', operationType);
249
-
250
- // Get function schema by selector
251
- const functionSelector = '0xabcd...';
252
- const schema = await definitions.getFunctionSchemaBySelector(functionSelector);
253
- console.log('Function schema:', schema);
254
-
255
- // Check role permission for function
256
- const roleHash = '0xefgh...';
257
- const hasPermission = await definitions.hasRolePermission(roleHash, functionSelector);
258
- console.log('Has permission:', hasPermission);
259
-
260
- // Get all roles that can execute a function
261
- const allowedRoles = await definitions.getRolesForFunction(functionSelector);
262
- console.log('Allowed roles:', allowedRoles);
263
- ```
264
-
265
- ### Configuration Management
266
-
267
- ```typescript
268
- // Get current configuration
269
- const config = definitions.getConfig();
270
- console.log('Current config:', config);
271
-
272
- // Update configuration
273
- definitions.updateConfig({
274
- chainId: 137, // Polygon
275
- rpcUrl: 'https://polygon-rpc.com'
276
- });
277
- ```
278
-
279
- ## Types and Constants
280
-
281
- ### Transaction Actions
282
-
283
- ```typescript
284
- import { TxAction } from '@bloxchain/sdk';
285
-
286
- // Available transaction actions
287
- TxAction.EXECUTE_TIME_DELAY_REQUEST
288
- TxAction.EXECUTE_TIME_DELAY_APPROVE
289
- TxAction.EXECUTE_TIME_DELAY_CANCEL
290
- TxAction.SIGN_META_REQUEST_AND_APPROVE
291
- TxAction.SIGN_META_APPROVE
292
- TxAction.SIGN_META_CANCEL
293
- TxAction.EXECUTE_META_REQUEST_AND_APPROVE
294
- TxAction.EXECUTE_META_APPROVE
295
- TxAction.EXECUTE_META_CANCEL
296
- ```
297
-
298
- ### Execution Types
299
-
300
- ```typescript
301
- import { ExecutionType } from '@bloxchain/sdk';
302
-
303
- ExecutionType.NONE
304
- ExecutionType.STANDARD
305
- ExecutionType.RAW
306
- ```
307
-
308
- ### Transaction Status
309
-
310
- ```typescript
311
- import { TxStatus } from '@bloxchain/sdk';
312
-
313
- TxStatus.UNDEFINED
314
- TxStatus.PENDING
315
- TxStatus.CANCELLED
316
- TxStatus.COMPLETED
317
- TxStatus.FAILED
318
- TxStatus.REJECTED
319
- ```
320
-
321
- ## Error Handling
322
-
323
- All SDK methods throw errors for failed operations. Always wrap SDK calls in try-catch blocks:
324
-
325
- ```typescript
326
- try {
327
- const result = await secureOwnable.transferOwnershipRequest({
328
- from: ownerAddress
329
- });
330
- console.log('Transaction successful:', result.hash);
331
- } catch (error) {
332
- console.error('Transaction failed:', error);
333
- }
334
- ```
335
-
336
- ## Security Considerations
337
-
338
- - Always validate addresses and parameters before making transactions
339
- - Use proper time-lock periods for critical operations
340
- - Implement proper access control using RuntimeRBAC
341
- - Monitor transaction status and handle failures appropriately
342
- - Keep private keys secure and never expose them in client-side code
343
-
344
- ## Versioning and stability
345
-
346
- This package follows [Semantic Versioning](https://semver.org/). Current versions are **alpha** (`1.0.0-alpha.x`). Pre-1.0 releases may introduce breaking changes; we recommend pinning the exact version until the protocol is audited and stable.
347
-
348
- ## Security
349
-
350
- - **Vulnerability reporting**: Do not open public GitHub issues for security vulnerabilities. See the [Security Policy](https://github.com/PracticalParticle/Bloxchain-Protocol/blob/main/SECURITY.md) for reporting instructions (e.g. security@particlecs.com).
351
- - **Audit status**: The underlying protocol is not yet audited. Do not use with mainnet assets without an independent security review.
352
-
353
- ## Support and links
354
-
355
- - **Documentation**: [SDK docs](./docs/) in this package; [Bloxchain Protocol README](https://github.com/PracticalParticle/Bloxchain-Protocol#readme) for protocol details
356
- - **Issues and feature requests**: [GitHub Issues](https://github.com/PracticalParticle/Bloxchain-Protocol/issues)
357
- - **Homepage**: [bloxchain.app](https://bloxchain.app/)
358
- - **Author**: [Particle Crypto Security](https://particlecs.com/)
359
-
360
- ## Repository
361
-
362
- Part of [Bloxchain Protocol](https://github.com/PracticalParticle/Bloxchain-Protocol). For protocol architecture and contract documentation, see the main repository README.
363
-
364
- ## Contributing
365
-
366
- When contributing to the SDK:
367
-
368
- 1. Follow TypeScript best practices
369
- 2. Add comprehensive type definitions
370
- 3. Include JSDoc comments for all public methods
371
- 4. Test all new functionality thoroughly
372
- 5. Update this README with new features
373
-
374
- ## License
375
-
376
- MPL-2.0 (Mozilla Public License 2.0). This SDK is part of the Bloxchain Protocol. See the [LICENSE](https://github.com/PracticalParticle/Bloxchain-Protocol/blob/main/LICENSE) file in the main repository.
1
+ # Bloxchain Protocol TypeScript SDK
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@bloxchain/sdk.svg)](https://www.npmjs.com/package/@bloxchain/sdk)
4
+ [![License: MPL-2.0](https://img.shields.io/badge/License-MPL--2.0-blue.svg)](https://opensource.org/licenses/MPL-2.0)
5
+ [![TypeScript](https://img.shields.io/badge/TypeScript-6.x-blue.svg)](https://www.typescriptlang.org/)
6
+ [![Node](https://img.shields.io/badge/Node-%3E%3D18.0.0-green.svg)](https://nodejs.org/)
7
+
8
+ A comprehensive TypeScript SDK for interacting with the Bloxchain Protocol smart contracts, providing type-safe interfaces for secure multi-phase operations, dynamic role-based access control, and state abstraction.
9
+
10
+ The SDK mirrors the Nethermind-audited core protocol (`contracts/core/`). Pre-mainnet today; mainnet coming soon. Pin an exact package version and review release notes before upgrading.
11
+
12
+ ## Requirements
13
+
14
+ - **Node.js**: >= 18.0.0
15
+ - **TypeScript**: 6.x (recommended)
16
+ - **Peer dependency**: `viem` ^2.49.4 (required; align with SDK dependency, currently `2.50.x`)
17
+ - **Optional peer**: `@bloxchain/contracts` ^1.0.0 (for Solidity/ABI alignment when building apps that use both)
18
+
19
+ ## 🏗️ **Unique Architecture**
20
+
21
+ Bloxchain Protocol implements a **state machine architecture** with `SecureOperationState` as the core engine, providing:
22
+
23
+ - **Centralized State Management**: All security operations flow through a unified state machine
24
+ - **Multi-Phase Transaction Processing**: Time-locked operations with request/approval workflows
25
+ - **Dynamic Role-Based Access Control**: Flexible, hierarchical permission system
26
+ - **Meta-Transaction Support**: Gasless transactions and delegated execution
27
+ - **Event-Driven Architecture**: Comprehensive audit trails and external monitoring
28
+
29
+ ## Features
30
+
31
+ - **SecureOwnable**: Multi-phase ownership management with time-locked operations
32
+ - **RuntimeRBAC**: Runtime role-based access control system with batch configuration
33
+ - **Definitions**: Dynamic interaction with any definition library implementing IDefinition
34
+ - **Guardian**: State abstraction with secure operations
35
+ - **Type Safety**: Full TypeScript support with comprehensive type definitions
36
+ - **Viem Integration**: Built on top of Viem for modern Ethereum development
37
+
38
+ ## 📚 Documentation
39
+
40
+ Comprehensive documentation is available in the repository root [`docs/`](../../docs/) folder:
41
+
42
+ ### **🏗️ Architecture & Design**
43
+ - **[Protocol Architecture](../../docs/bloxchain-architecture.md)** - Bloxchain protocol overview and design principles
44
+ - **[State Machine Engine](../../docs/state-machine-engine.md)** - SecureOperationState engine and state management
45
+ - **[Architecture Patterns](../../docs/architecture-patterns.md)** - Design patterns and best practices
46
+
47
+ ### **🚀 Getting Started**
48
+ - **[Getting Started](../../docs/getting-started.md)** - Quick setup and basic usage
49
+ - **[API Reference](../../docs/api-reference.md)** - Complete API documentation
50
+ - **[SecureOwnable Guide](../../docs/secure-ownable.md)** - Ownership management
51
+ - **[RuntimeRBAC Guide](../../docs/runtime-rbac.md)** - Role-based access control
52
+
53
+ ### **🔍 Development Tools**
54
+ - **[Best Practices](../../docs/best-practices.md)** - Development guidelines
55
+ - **[Examples](../../docs/examples-basic.md)** - Practical code samples
56
+ - **[Types & Interfaces](../../docs/types-interfaces.md)** - Type definitions
57
+
58
+ **📖 [View All Documentation](../../docs/README.md)**
59
+
60
+ ## Installation
61
+
62
+ ```bash
63
+ # Install the SDK and its required peer dependency
64
+ npm install @bloxchain/sdk viem
65
+
66
+ # Optional: install contracts package when building apps that use both SDK and Solidity
67
+ npm install @bloxchain/contracts
68
+ ```
69
+
70
+ To install from the repository (e.g. for development):
71
+
72
+ ```bash
73
+ npm install https://github.com/PracticalParticle/Bloxchain-Protocol.git#main --save
74
+ ```
75
+
76
+ ### Node.js ESM (`"type": "module"`)
77
+
78
+ The published package is **native ESM**, built with TypeScript **`module` / `moduleResolution`: `NodeNext`**. All relative imports in `sdk/typescript` source use explicit **`.js`** extensions (and `with { type: 'json' }` for ABIs) so `tsc` output loads under Node without a bundler or post-build patches.
79
+
80
+ **Requires Node.js >= 18.20.5** (JSON import attributes). Node 18.0–18.19 will fail at runtime when loading `dist/`.
81
+
82
+ When adding or moving SDK files, follow the same import style (e.g. `from './SecureOwnable.js'`, not `from './SecureOwnable'`).
83
+
84
+ From the protocol repo, run `npm run release:prepare` before publish (includes SDK build, Node ESM import of `dist/`, and selector alignment vs Solidity). Then `npm run publish:contracts` or `npm run publish:sdk`.
85
+
86
+ ### Public exports (stable integrator surface)
87
+
88
+ | Export | Purpose |
89
+ |--------|---------|
90
+ | `SECURITY_FUNCTION_SELECTORS` | SecureOwnable function selectors (`FUNCTION_SELECTORS` in Solidity definitions) |
91
+ | `RUNTIME_RBAC_FUNCTION_SELECTORS` / `GUARD_CONTROLLER_FUNCTION_SELECTORS` | Batch, timelock, payment, and execute selectors (see `types/meta-tx-signatures.ts`) |
92
+ | `ENGINE_BLOX_META_TRANSACTION_PARAM` / `ENGINE_BLOX_META_TX_PARAMS` / `metaTxHandlerSignature` | Canonical MetaTransaction tuple strings and selector builders (aligned with `EngineBlox.sol`) |
93
+ | `INTERFACE_IDS` / `ComponentDetection` / `supportsInterface` | ERC-165 checks aligned with current `I*` interfaces (strict — no fallback selector) |
94
+ | `EngineBlox`, `Definitions` | Pure helpers and definition-library reads |
95
+ | `roleConfigBatchExecutionParams`, `guardConfigBatchExecutionParams`, encoders | Batch calldata builders |
96
+ | `updateRecoveryExecutionParams`, `updateTimeLockExecutionParams` | SecureOwnable execution params via deployed `SecureOwnableDefinitions` |
97
+ | `extractErrorInfo`, `enhanceViemError` | Revert decoding and Viem error enrichment |
98
+ | `@bloxchain/sdk/abi` | `engineBloxAbi`, `engineBloxErrorAbi` |
99
+
100
+ ## Quick Start
101
+
102
+ ```typescript
103
+ import {
104
+ SecureOwnable,
105
+ RuntimeRBAC,
106
+ GuardController,
107
+ Definitions,
108
+ type Address,
109
+ type PublicClient,
110
+ type WalletClient,
111
+ type Chain
112
+ } from '@bloxchain/sdk';
113
+
114
+ // Initialize clients (using your preferred provider)
115
+ const publicClient: PublicClient = createPublicClient({...});
116
+ const walletClient: WalletClient = createWalletClient({...});
117
+ const chain: Chain = mainnet; // or your target chain
118
+
119
+ // Initialize SDK classes
120
+ const secureOwnable = new SecureOwnable(
121
+ publicClient,
122
+ walletClient,
123
+ contractAddress,
124
+ chain
125
+ );
126
+
127
+ const runtimeRBAC = new RuntimeRBAC(
128
+ publicClient,
129
+ walletClient,
130
+ contractAddress,
131
+ chain
132
+ );
133
+
134
+ const definitions = new Definitions(
135
+ publicClient,
136
+ walletClient,
137
+ definitionsAddress,
138
+ chain
139
+ );
140
+ ```
141
+
142
+ ## SecureOwnable Usage
143
+
144
+ ### Ownership Management
145
+
146
+ ```typescript
147
+ // Request ownership transfer
148
+ const txResult = await secureOwnable.transferOwnershipRequest({
149
+ from: ownerAddress
150
+ });
151
+
152
+ // Approve after time lock period
153
+ const approvalResult = await secureOwnable.transferOwnershipDelayedApproval(
154
+ txId,
155
+ { from: ownerAddress }
156
+ );
157
+
158
+ // Cancel ownership transfer
159
+ const cancelResult = await secureOwnable.transferOwnershipCancellation(
160
+ txId,
161
+ { from: ownerAddress }
162
+ );
163
+ ```
164
+
165
+ ### Meta Transactions
166
+
167
+ ```typescript
168
+ // Create meta transaction parameters
169
+ const metaTxParams = await secureOwnable.createMetaTxParams(
170
+ handlerContract,
171
+ handlerSelector,
172
+ deadline,
173
+ maxGasPrice,
174
+ signer
175
+ );
176
+
177
+ // Generate unsigned meta transaction
178
+ const metaTx = await secureOwnable.generateUnsignedMetaTransactionForNew(
179
+ requester,
180
+ target,
181
+ value,
182
+ gasLimit,
183
+ operationType,
184
+ executionType,
185
+ executionOptions,
186
+ metaTxParams
187
+ );
188
+ ```
189
+
190
+ ## RuntimeRBAC Usage
191
+
192
+ RuntimeRBAC uses batch-based configuration for all role management operations. See the [RuntimeRBAC Guide](../../docs/runtime-rbac.md) for complete examples.
193
+
194
+ ### Query Functions
195
+
196
+ ```typescript
197
+ // Get role information
198
+ const role = await runtimeRBAC.getRole(roleHash);
199
+ console.log(role.roleName, role.maxWallets, role.isProtected);
200
+
201
+ // Check if wallet has role
202
+ const hasRole = await runtimeRBAC.hasRole(roleHash, walletAddress);
203
+
204
+ // Get authorized wallets in role
205
+ const wallets = await runtimeRBAC.getAuthorizedWallets(roleHash);
206
+
207
+ // Get roles for a wallet
208
+ const walletRoles = await runtimeRBAC.getWalletRoles(walletAddress);
209
+
210
+ // Get supported roles
211
+ const supportedRoles = await runtimeRBAC.getSupportedRoles();
212
+
213
+ // Get active role permissions
214
+ const permissions = await runtimeRBAC.getActiveRolePermissions(roleHash);
215
+ ```
216
+
217
+ ### Batch Configuration
218
+
219
+ All role management (create role, add wallet, add permissions, etc.) is done via batch operations. See the [RuntimeRBAC Guide](../../docs/runtime-rbac.md) for detailed batch configuration examples.
220
+
221
+ ## Definitions Usage
222
+
223
+ The `Definitions` class provides dynamic interaction with any definition library that implements the `IDefinition` interface. This allows you to query operation types, function schemas, role permissions, and workflow definitions from any compatible contract.
224
+
225
+ ### Basic Usage
226
+
227
+ ```typescript
228
+ // Initialize Definitions
229
+ const definitions = new Definitions(
230
+ publicClient,
231
+ walletClient,
232
+ definitionsAddress,
233
+ chain
234
+ );
235
+
236
+ // Get all operation types
237
+ const operationTypes = await definitions.getOperationTypes();
238
+ console.log('Available operations:', operationTypes);
239
+
240
+ // Get all function schemas
241
+ const functionSchemas = await definitions.getFunctionSchemas();
242
+ console.log('Function schemas:', functionSchemas);
243
+
244
+ // Get role permissions
245
+ const rolePermissions = await definitions.getRolePermissions();
246
+ console.log('Role permissions:', rolePermissions);
247
+ ```
248
+
249
+ ### Workflow Management
250
+
251
+ ```typescript
252
+ // Get all operation workflows
253
+ const workflows = await definitions.getOperationWorkflows();
254
+ console.log('Available workflows:', workflows);
255
+
256
+ // Get workflow for specific operation
257
+ const operationType = '0x1234...'; // operation type hash
258
+ const workflow = await definitions.getWorkflowForOperation(operationType);
259
+ console.log('Workflow for operation:', workflow);
260
+
261
+ // Get all workflow paths
262
+ const paths = await definitions.getWorkflowPaths();
263
+ console.log('Available paths:', paths);
264
+ ```
265
+
266
+ ### Utility Functions
267
+
268
+ ```typescript
269
+ // Find operation type by name
270
+ const operationType = await definitions.getOperationTypeByName('TRANSFER_OWNERSHIP');
271
+ console.log('Operation type hash:', operationType);
272
+
273
+ // Get function schema by selector
274
+ const functionSelector = '0xabcd...';
275
+ const schema = await definitions.getFunctionSchemaBySelector(functionSelector);
276
+ console.log('Function schema:', schema);
277
+
278
+ // Check role permission for function
279
+ const roleHash = '0xefgh...';
280
+ const hasPermission = await definitions.hasRolePermission(roleHash, functionSelector);
281
+ console.log('Has permission:', hasPermission);
282
+
283
+ // Get all roles that can execute a function
284
+ const allowedRoles = await definitions.getRolesForFunction(functionSelector);
285
+ console.log('Allowed roles:', allowedRoles);
286
+ ```
287
+
288
+ ### Configuration Management
289
+
290
+ ```typescript
291
+ // Get current configuration
292
+ const config = definitions.getConfig();
293
+ console.log('Current config:', config);
294
+
295
+ // Update configuration
296
+ definitions.updateConfig({
297
+ chainId: 137, // Polygon
298
+ rpcUrl: 'https://polygon-rpc.com'
299
+ });
300
+ ```
301
+
302
+ ## Types and Constants
303
+
304
+ ### Transaction Actions
305
+
306
+ ```typescript
307
+ import { TxAction } from '@bloxchain/sdk';
308
+
309
+ // Available transaction actions
310
+ TxAction.EXECUTE_TIME_DELAY_REQUEST
311
+ TxAction.EXECUTE_TIME_DELAY_APPROVE
312
+ TxAction.EXECUTE_TIME_DELAY_CANCEL
313
+ TxAction.SIGN_META_REQUEST_AND_APPROVE
314
+ TxAction.SIGN_META_APPROVE
315
+ TxAction.SIGN_META_CANCEL
316
+ TxAction.EXECUTE_META_REQUEST_AND_APPROVE
317
+ TxAction.EXECUTE_META_APPROVE
318
+ TxAction.EXECUTE_META_CANCEL
319
+ ```
320
+
321
+ ### Execution Types
322
+
323
+ ```typescript
324
+ import { ExecutionType } from '@bloxchain/sdk';
325
+
326
+ ExecutionType.NONE
327
+ ExecutionType.STANDARD
328
+ ExecutionType.RAW
329
+ ```
330
+
331
+ ### Transaction Status
332
+
333
+ ```typescript
334
+ import { TxStatus } from '@bloxchain/sdk';
335
+
336
+ TxStatus.UNDEFINED
337
+ TxStatus.PENDING
338
+ TxStatus.EXECUTING
339
+ TxStatus.PROCESSING_PAYMENT
340
+ TxStatus.CANCELLED
341
+ TxStatus.COMPLETED
342
+ TxStatus.FAILED
343
+ ```
344
+
345
+ ### TxRecord (state machine transactions)
346
+
347
+ The canonical TypeScript shape matches on-chain `EngineBlox.TxRecord`. The completion hash field is **`resultHash`** (bytes32; zero while pending):
348
+
349
+ ```typescript
350
+ import type { TxRecord } from '@bloxchain/sdk';
351
+
352
+ const tx: TxRecord = await secureOwnable.getTransaction(txId);
353
+ console.log(tx.resultHash);
354
+ ```
355
+
356
+ ## Error Handling
357
+
358
+ All SDK methods throw errors for failed operations. Always wrap SDK calls in try-catch blocks:
359
+
360
+ ```typescript
361
+ try {
362
+ const result = await secureOwnable.transferOwnershipRequest({
363
+ from: ownerAddress
364
+ });
365
+ console.log('Transaction successful:', result.hash);
366
+ } catch (error) {
367
+ console.error('Transaction failed:', error);
368
+ }
369
+ ```
370
+
371
+ ## Security Considerations
372
+
373
+ - Always validate addresses and parameters before making transactions
374
+ - Use proper time-lock periods for critical operations
375
+ - Implement proper access control using RuntimeRBAC
376
+ - Monitor transaction status and handle failures appropriately
377
+ - Keep private keys secure and never expose them in client-side code
378
+
379
+ ## Versioning and stability
380
+
381
+ This package follows [Semantic Versioning](https://semver.org/). Stable releases publish to npm **`latest`**; see [CHANGELOG.md](./CHANGELOG.md). Pin the exact version in production and review release notes before upgrading.
382
+
383
+ ## Security
384
+
385
+ - **Vulnerability reporting**: Do not open public GitHub issues for security vulnerabilities. See the [Security Policy](https://github.com/PracticalParticle/Bloxchain-Protocol/blob/main/SECURITY.md) for reporting instructions (e.g. security@particlecs.com).
386
+ - **Audit status**: The SDK mirrors the audited **core** protocol (`contracts/core/`), not a separate Solidity audit. Nethermind report: [audits/nethermind](https://github.com/PracticalParticle/Bloxchain-Protocol/tree/main/audits/nethermind) ([PDF](https://github.com/PracticalParticle/Bloxchain-Protocol/blob/main/audits/nethermind/Nethermind-Bloxchain-Core-NM_0828.pdf)). Pre-mainnet; mainnet coming soon. Pin package versions and follow your own operational review before production use.
387
+
388
+ ## Support and links
389
+
390
+ - **Documentation**: [SDK and protocol guides](../../docs/) in the repository `docs/` folder; [Bloxchain Protocol README](https://github.com/PracticalParticle/Bloxchain-Protocol#readme) for protocol details
391
+ - **Issues and feature requests**: [GitHub Issues](https://github.com/PracticalParticle/Bloxchain-Protocol/issues)
392
+ - **Homepage**: [bloxchain.app](https://bloxchain.app/)
393
+ - **Author**: [Particle Crypto Security](https://particlecs.com/)
394
+
395
+ ## Repository
396
+
397
+ Part of [Bloxchain Protocol](https://github.com/PracticalParticle/Bloxchain-Protocol). For protocol architecture and contract documentation, see the main repository README.
398
+
399
+ ## Contributing
400
+
401
+ When contributing to the SDK:
402
+
403
+ 1. Follow TypeScript best practices
404
+ 2. Add comprehensive type definitions
405
+ 3. Include JSDoc comments for all public methods
406
+ 4. Test all new functionality thoroughly
407
+ 5. Update this README with new features
408
+
409
+ ## License
410
+
411
+ MPL-2.0 (Mozilla Public License 2.0). This SDK is part of the Bloxchain Protocol. See the [LICENSE](https://github.com/PracticalParticle/Bloxchain-Protocol/blob/main/LICENSE) file in the main repository.