@bitshard.io/bitshard-sdk 0.0.1

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 (117) hide show
  1. package/dist/BitShardSDK.d.ts +156 -0
  2. package/dist/BitShardSDK.d.ts.map +1 -0
  3. package/dist/BitShardSDK.js +350 -0
  4. package/dist/BitShardSDK.js.map +1 -0
  5. package/dist/chains/bitcoin/BitcoinChain.d.ts +6 -0
  6. package/dist/chains/bitcoin/BitcoinChain.d.ts.map +1 -0
  7. package/dist/chains/bitcoin/BitcoinChain.js +10 -0
  8. package/dist/chains/bitcoin/BitcoinChain.js.map +1 -0
  9. package/dist/chains/config.d.ts +5 -0
  10. package/dist/chains/config.d.ts.map +1 -0
  11. package/dist/chains/config.js +7 -0
  12. package/dist/chains/config.js.map +1 -0
  13. package/dist/chains/evm/EVMChain.d.ts +6 -0
  14. package/dist/chains/evm/EVMChain.d.ts.map +1 -0
  15. package/dist/chains/evm/EVMChain.js +10 -0
  16. package/dist/chains/evm/EVMChain.js.map +1 -0
  17. package/dist/core/DKLSParty.d.ts +132 -0
  18. package/dist/core/DKLSParty.d.ts.map +1 -0
  19. package/dist/core/DKLSParty.js +267 -0
  20. package/dist/core/DKLSParty.js.map +1 -0
  21. package/dist/core/DKLSService.d.ts +83 -0
  22. package/dist/core/DKLSService.d.ts.map +1 -0
  23. package/dist/core/DKLSService.js +325 -0
  24. package/dist/core/DKLSService.js.map +1 -0
  25. package/dist/core/ThresholdConfig.d.ts +76 -0
  26. package/dist/core/ThresholdConfig.d.ts.map +1 -0
  27. package/dist/core/ThresholdConfig.js +127 -0
  28. package/dist/core/ThresholdConfig.js.map +1 -0
  29. package/dist/core/types.d.ts +238 -0
  30. package/dist/core/types.d.ts.map +1 -0
  31. package/dist/core/types.js +3 -0
  32. package/dist/core/types.js.map +1 -0
  33. package/dist/crypto/addresses.d.ts +82 -0
  34. package/dist/crypto/addresses.d.ts.map +1 -0
  35. package/dist/crypto/addresses.js +242 -0
  36. package/dist/crypto/addresses.js.map +1 -0
  37. package/dist/crypto/elliptic.d.ts +19 -0
  38. package/dist/crypto/elliptic.d.ts.map +1 -0
  39. package/dist/crypto/elliptic.js +114 -0
  40. package/dist/crypto/elliptic.js.map +1 -0
  41. package/dist/crypto/encoding.d.ts +111 -0
  42. package/dist/crypto/encoding.d.ts.map +1 -0
  43. package/dist/crypto/encoding.js +224 -0
  44. package/dist/crypto/encoding.js.map +1 -0
  45. package/dist/index.d.ts +23 -0
  46. package/dist/index.d.ts.map +1 -0
  47. package/dist/index.js +58 -0
  48. package/dist/index.js.map +1 -0
  49. package/dist/protocols/keygen.d.ts +6 -0
  50. package/dist/protocols/keygen.d.ts.map +1 -0
  51. package/dist/protocols/keygen.js +12 -0
  52. package/dist/protocols/keygen.js.map +1 -0
  53. package/dist/protocols/presignature.d.ts +68 -0
  54. package/dist/protocols/presignature.d.ts.map +1 -0
  55. package/dist/protocols/presignature.js +147 -0
  56. package/dist/protocols/presignature.js.map +1 -0
  57. package/dist/protocols/refresh.d.ts +5 -0
  58. package/dist/protocols/refresh.d.ts.map +1 -0
  59. package/dist/protocols/refresh.js +10 -0
  60. package/dist/protocols/refresh.js.map +1 -0
  61. package/dist/protocols/signing.d.ts +7 -0
  62. package/dist/protocols/signing.d.ts.map +1 -0
  63. package/dist/protocols/signing.js +12 -0
  64. package/dist/protocols/signing.js.map +1 -0
  65. package/dist/rpc/RPCProvider.d.ts +6 -0
  66. package/dist/rpc/RPCProvider.d.ts.map +1 -0
  67. package/dist/rpc/RPCProvider.js +6 -0
  68. package/dist/rpc/RPCProvider.js.map +1 -0
  69. package/dist/rpc/methods.d.ts +5 -0
  70. package/dist/rpc/methods.d.ts.map +1 -0
  71. package/dist/rpc/methods.js +10 -0
  72. package/dist/rpc/methods.js.map +1 -0
  73. package/dist/websocket/coordinator.d.ts +6 -0
  74. package/dist/websocket/coordinator.d.ts.map +1 -0
  75. package/dist/websocket/coordinator.js +10 -0
  76. package/dist/websocket/coordinator.js.map +1 -0
  77. package/dist/websocket/messages.d.ts +9 -0
  78. package/dist/websocket/messages.d.ts.map +1 -0
  79. package/dist/websocket/messages.js +7 -0
  80. package/dist/websocket/messages.js.map +1 -0
  81. package/dist/websocket/session.d.ts +5 -0
  82. package/dist/websocket/session.d.ts.map +1 -0
  83. package/dist/websocket/session.js +7 -0
  84. package/dist/websocket/session.js.map +1 -0
  85. package/dist/wire/format.d.ts +8 -0
  86. package/dist/wire/format.d.ts.map +1 -0
  87. package/dist/wire/format.js +11 -0
  88. package/dist/wire/format.js.map +1 -0
  89. package/dist/wire/validation.d.ts +6 -0
  90. package/dist/wire/validation.d.ts.map +1 -0
  91. package/dist/wire/validation.js +13 -0
  92. package/dist/wire/validation.js.map +1 -0
  93. package/package.json +67 -0
  94. package/src/BitShardSDK.ts +428 -0
  95. package/src/chains/bitcoin/BitcoinChain.ts +7 -0
  96. package/src/chains/config.ts +7 -0
  97. package/src/chains/evm/EVMChain.ts +7 -0
  98. package/src/core/DKLSParty.ts +317 -0
  99. package/src/core/DKLSService.ts +426 -0
  100. package/src/core/ThresholdConfig.ts +159 -0
  101. package/src/core/types.ts +253 -0
  102. package/src/crypto/addresses.ts +282 -0
  103. package/src/crypto/elliptic.ts +133 -0
  104. package/src/crypto/encoding.ts +227 -0
  105. package/src/index.ts +40 -0
  106. package/src/protocols/keygen.ts +8 -0
  107. package/src/protocols/presignature.ts +196 -0
  108. package/src/protocols/refresh.ts +7 -0
  109. package/src/protocols/signing.ts +9 -0
  110. package/src/rpc/RPCProvider.ts +7 -0
  111. package/src/rpc/methods.ts +7 -0
  112. package/src/websocket/coordinator.ts +7 -0
  113. package/src/websocket/messages.ts +11 -0
  114. package/src/websocket/session.ts +7 -0
  115. package/src/wire/format.ts +10 -0
  116. package/src/wire/validation.ts +14 -0
  117. package/test-sdk.js +234 -0
@@ -0,0 +1,428 @@
1
+ import { Keyshare } from '@silencelaboratories/dkls-wasm-ll-node';
2
+ import { DKLSService } from './core/DKLSService';
3
+ import { DKLSParty } from './core/DKLSParty';
4
+ import type {
5
+ SDKConfig,
6
+ LocalWallet,
7
+ PartyConfig,
8
+ SignatureResult,
9
+ BlockchainAddresses,
10
+ ChainConfig,
11
+ BitcoinConfig
12
+ } from './core/types';
13
+ import { deriveAddresses } from './crypto/addresses';
14
+
15
+ /**
16
+ * Main SDK class for BitShard MPC operations
17
+ */
18
+ export class BitShardSDK {
19
+ private readonly dklsService: DKLSService;
20
+ private readonly chains: Map<string, ChainConfig>;
21
+ private bitcoinConfig?: BitcoinConfig;
22
+
23
+ /**
24
+ * Create a new BitShardSDK instance
25
+ * @param config SDK configuration options
26
+ */
27
+ constructor(config: SDKConfig = {}) {
28
+ this.dklsService = new DKLSService();
29
+ this.chains = new Map();
30
+
31
+ // Initialize default chains
32
+ this.initializeDefaultChains();
33
+
34
+ // Add custom chains from config
35
+ if (config.chains) {
36
+ Object.entries(config.chains).forEach(([name, chainConfig]) => {
37
+ this.chains.set(name, chainConfig);
38
+ });
39
+ }
40
+
41
+ // Set Bitcoin config
42
+ if (config.bitcoin) {
43
+ this.bitcoinConfig = config.bitcoin;
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Create a local wallet for testing (all parties in one process)
49
+ * @param config Wallet configuration
50
+ * @returns Local wallet with all keyshares
51
+ */
52
+ async createLocalWallet(config: {
53
+ totalParties?: number;
54
+ threshold?: number;
55
+ partyIds?: number[];
56
+ } = {}): Promise<LocalWallet> {
57
+ const totalParties = config.totalParties ?? 3;
58
+ const threshold = config.threshold ?? 2;
59
+ const partyIds = config.partyIds ?? Array.from({ length: totalParties }, (_, i) => i);
60
+
61
+ // Generate DKG locally
62
+ const dkgResult = await this.dklsService.generateDKG(totalParties, threshold, partyIds);
63
+
64
+ // Reconstruct keyshares from base64
65
+ const keyshares = dkgResult.keyshares.map(pk =>
66
+ Keyshare.fromBytes(new Uint8Array(Buffer.from(pk.share, 'base64')))
67
+ );
68
+
69
+ return {
70
+ publicKey: dkgResult.publicKey,
71
+ keyshares,
72
+ config: {
73
+ totalParties,
74
+ threshold,
75
+ partyIds
76
+ }
77
+ };
78
+ }
79
+
80
+ /**
81
+ * Create a party for distributed setup
82
+ * @param config Party configuration
83
+ * @returns DKLS party instance
84
+ */
85
+ createParty(config: PartyConfig): DKLSParty {
86
+ return new DKLSParty(config);
87
+ }
88
+
89
+ /**
90
+ * Convert DKLS Message to wire format
91
+ * @param msg DKLS Message
92
+ * @returns Wire format message
93
+ */
94
+ toWireMessage(msg: any): any {
95
+ return DKLSService.toWireMessage(msg);
96
+ }
97
+
98
+ /**
99
+ * Convert wire format to DKLS Message
100
+ * @param wire Wire format message
101
+ * @returns DKLS Message
102
+ */
103
+ fromWireMessage(wire: any): any {
104
+ return DKLSService.fromWireMessage(wire);
105
+ }
106
+
107
+ /**
108
+ * Derive addresses from public key
109
+ * @param publicKeyHex Public key in hex format
110
+ * @returns Blockchain addresses
111
+ */
112
+ deriveAddresses(publicKeyHex: string): BlockchainAddresses {
113
+ const addresses = deriveAddresses(publicKeyHex);
114
+ return {
115
+ ethereum: addresses.ethereum,
116
+ bitcoin: addresses.bitcoin,
117
+ cosmos: addresses.cosmos,
118
+ arbitrum: addresses.arbitrum,
119
+ polygon: addresses.polygon,
120
+ bnb: addresses.bnb,
121
+ avalanche: addresses.avalanche
122
+ };
123
+ }
124
+
125
+ /**
126
+ * Sign a personal message (EIP-191)
127
+ * @param message Message to sign
128
+ * @param keyshares Keyshares for signing
129
+ * @param options Signing options including publicKey for v calculation
130
+ * @returns Signature result
131
+ */
132
+ async personalSign(
133
+ message: string,
134
+ keyshares: Keyshare[],
135
+ options: { threshold?: number; publicKey?: string } = {}
136
+ ): Promise<SignatureResult> {
137
+ // Prepare message with EIP-191 prefix
138
+ const messageBytes = Buffer.from(message, 'utf8');
139
+ const prefix = `\x19Ethereum Signed Message:\n${messageBytes.length}`;
140
+ const prefixedMessage = Buffer.concat([
141
+ Buffer.from(prefix, 'utf8'),
142
+ messageBytes
143
+ ]);
144
+
145
+ // Hash the prefixed message with Keccak256 (Ethereum standard)
146
+ const { keccak256 } = require('viem');
147
+ const messageHash = Buffer.from(keccak256(prefixedMessage).slice(2), 'hex');
148
+
149
+ // Sign with DKLS (pass public key if available for v calculation)
150
+ const threshold = options.threshold ?? keyshares.length;
151
+ const publicKey = options.publicKey;
152
+
153
+ return this.dklsService.signMessage(messageHash, keyshares, threshold, publicKey);
154
+ }
155
+
156
+ /**
157
+ * Sign a personal message using a wallet (convenience method)
158
+ * @param message Message to sign
159
+ * @param wallet Local wallet containing keyshares and public key
160
+ * @param options Additional signing options
161
+ * @returns Signature result
162
+ */
163
+ async personalSignWithWallet(
164
+ message: string,
165
+ wallet: LocalWallet,
166
+ options: { threshold?: number } = {}
167
+ ): Promise<SignatureResult> {
168
+ return this.personalSign(message, wallet.keyshares, {
169
+ threshold: options.threshold ?? wallet.config.threshold,
170
+ publicKey: wallet.publicKey
171
+ });
172
+ }
173
+
174
+ /**
175
+ * Sign a raw hash (for transactions)
176
+ * @param hash Hash to sign (32 bytes)
177
+ * @param keyshares Keyshares for signing
178
+ * @param options Signing options including publicKey for v calculation
179
+ * @returns Signature result
180
+ */
181
+ async signHash(
182
+ hash: string | Uint8Array,
183
+ keyshares: Keyshare[],
184
+ options: { threshold?: number; publicKey?: string } = {}
185
+ ): Promise<SignatureResult> {
186
+ // Convert hash to Uint8Array if needed
187
+ const hashBytes = typeof hash === 'string'
188
+ ? Buffer.from(hash.replace('0x', ''), 'hex')
189
+ : hash;
190
+
191
+ if (hashBytes.length !== 32) {
192
+ throw new Error(`Invalid hash length: expected 32 bytes, got ${hashBytes.length}`);
193
+ }
194
+
195
+ // Sign with DKLS
196
+ const threshold = options.threshold ?? keyshares.length;
197
+ const publicKey = options.publicKey;
198
+
199
+ return this.dklsService.signMessage(hashBytes, keyshares, threshold, publicKey);
200
+ }
201
+
202
+ /**
203
+ * Sign a transaction hash using a wallet
204
+ * @param hash Transaction hash to sign
205
+ * @param wallet Local wallet containing keyshares and public key
206
+ * @param options Additional signing options
207
+ * @returns Signature result
208
+ */
209
+ async signTransactionWithWallet(
210
+ hash: string | Uint8Array,
211
+ wallet: LocalWallet,
212
+ options: { threshold?: number } = {}
213
+ ): Promise<SignatureResult> {
214
+ return this.signHash(hash, wallet.keyshares, {
215
+ threshold: options.threshold ?? wallet.config.threshold,
216
+ publicKey: wallet.publicKey
217
+ });
218
+ }
219
+
220
+ /**
221
+ * Sign typed data (EIP-712)
222
+ * @param typedData Typed data to sign
223
+ * @param keyshares Keyshares for signing
224
+ * @param options Signing options
225
+ * @returns Signature result
226
+ */
227
+ async signTypedDataV4(
228
+ typedData: any,
229
+ keyshares: Keyshare[],
230
+ options: { threshold?: number } = {}
231
+ ): Promise<SignatureResult> {
232
+ // TODO: Implement proper EIP-712 hashing
233
+ // For now, use a placeholder
234
+ const messageHash = Buffer.from(JSON.stringify(typedData), 'utf8');
235
+ const hash = require('crypto').createHash('sha256').update(messageHash).digest();
236
+
237
+ const threshold = options.threshold ?? keyshares.length;
238
+ return this.dklsService.signMessage(hash, keyshares, threshold);
239
+ }
240
+
241
+ /**
242
+ * Send raw transaction (sign and prepare for broadcast)
243
+ * @param tx Transaction data
244
+ * @param keyshares Keyshares for signing
245
+ * @param options Signing options
246
+ * @returns Signed transaction hash
247
+ */
248
+ async sendRawTransaction(
249
+ tx: any,
250
+ keyshares: Keyshare[],
251
+ options: { threshold?: number } = {}
252
+ ): Promise<string> {
253
+ // TODO: Implement proper transaction signing
254
+ // This is a placeholder
255
+ const txHash = Buffer.from(JSON.stringify(tx), 'utf8');
256
+ const hash = require('crypto').createHash('sha256').update(txHash).digest();
257
+
258
+ const threshold = options.threshold ?? keyshares.length;
259
+ const signature = await this.dklsService.signMessage(hash, keyshares, threshold);
260
+
261
+ return signature.signature;
262
+ }
263
+
264
+ /**
265
+ * Configure a custom chain
266
+ * @param name Chain name
267
+ * @param config Chain configuration
268
+ */
269
+ configureChain(name: string, config: ChainConfig): void {
270
+ this.chains.set(name, config);
271
+ }
272
+
273
+ /**
274
+ * Use a preset chain configuration
275
+ * @param name Preset name
276
+ */
277
+ usePresetChain(name: string): void {
278
+ const preset = this.getPresetChain(name);
279
+ if (preset) {
280
+ this.chains.set(name, preset);
281
+ } else {
282
+ throw new Error(`Unknown preset chain: ${name}`);
283
+ }
284
+ }
285
+
286
+ /**
287
+ * Configure Bitcoin network
288
+ * @param config Bitcoin configuration
289
+ */
290
+ configureBitcoin(config: BitcoinConfig): void {
291
+ this.bitcoinConfig = config;
292
+ }
293
+
294
+ /**
295
+ * Get chain configuration
296
+ * @param name Chain name
297
+ * @returns Chain configuration or undefined
298
+ */
299
+ getChainConfig(name: string): ChainConfig | undefined {
300
+ return this.chains.get(name);
301
+ }
302
+
303
+ /**
304
+ * Get Bitcoin configuration
305
+ * @returns Bitcoin configuration or undefined
306
+ */
307
+ getBitcoinConfig(): BitcoinConfig | undefined {
308
+ return this.bitcoinConfig;
309
+ }
310
+
311
+ /**
312
+ * Initialize default chain configurations
313
+ */
314
+ private initializeDefaultChains(): void {
315
+ // Ethereum
316
+ this.chains.set('ethereum', {
317
+ chainId: 1,
318
+ name: 'Ethereum Mainnet',
319
+ rpcUrl: 'https://eth.llamarpc.com',
320
+ explorer: 'https://etherscan.io',
321
+ nativeCurrency: {
322
+ name: 'Ether',
323
+ symbol: 'ETH',
324
+ decimals: 18
325
+ }
326
+ });
327
+
328
+ // Arbitrum One
329
+ this.chains.set('arbitrum-one', {
330
+ chainId: 42161,
331
+ name: 'Arbitrum One',
332
+ rpcUrl: 'https://arb1.arbitrum.io/rpc',
333
+ explorer: 'https://arbiscan.io',
334
+ nativeCurrency: {
335
+ name: 'Ether',
336
+ symbol: 'ETH',
337
+ decimals: 18
338
+ },
339
+ gasConfig: {
340
+ maxFeePerGas: '0.1',
341
+ maxPriorityFeePerGas: '0.01'
342
+ }
343
+ });
344
+
345
+ // Arbitrum Nova
346
+ this.chains.set('arbitrum-nova', {
347
+ chainId: 42170,
348
+ name: 'Arbitrum Nova',
349
+ rpcUrl: 'https://nova.arbitrum.io/rpc',
350
+ explorer: 'https://nova.arbiscan.io',
351
+ nativeCurrency: {
352
+ name: 'Ether',
353
+ symbol: 'ETH',
354
+ decimals: 18
355
+ }
356
+ });
357
+
358
+ // Arbitrum Sepolia
359
+ this.chains.set('arbitrum-sepolia', {
360
+ chainId: 421614,
361
+ name: 'Arbitrum Sepolia',
362
+ rpcUrl: 'https://sepolia-rollup.arbitrum.io/rpc',
363
+ explorer: 'https://sepolia.arbiscan.io',
364
+ nativeCurrency: {
365
+ name: 'Ether',
366
+ symbol: 'ETH',
367
+ decimals: 18
368
+ }
369
+ });
370
+
371
+ // Polygon
372
+ this.chains.set('polygon', {
373
+ chainId: 137,
374
+ name: 'Polygon',
375
+ rpcUrl: 'https://polygon-rpc.com',
376
+ explorer: 'https://polygonscan.com',
377
+ nativeCurrency: {
378
+ name: 'MATIC',
379
+ symbol: 'MATIC',
380
+ decimals: 18
381
+ }
382
+ });
383
+
384
+ // BNB Smart Chain
385
+ this.chains.set('bnb', {
386
+ chainId: 56,
387
+ name: 'BNB Smart Chain',
388
+ rpcUrl: 'https://bsc-dataseed.binance.org',
389
+ explorer: 'https://bscscan.com',
390
+ nativeCurrency: {
391
+ name: 'BNB',
392
+ symbol: 'BNB',
393
+ decimals: 18
394
+ }
395
+ });
396
+
397
+ // Avalanche C-Chain
398
+ this.chains.set('avalanche', {
399
+ chainId: 43114,
400
+ name: 'Avalanche C-Chain',
401
+ rpcUrl: 'https://api.avax.network/ext/bc/C/rpc',
402
+ explorer: 'https://snowtrace.io',
403
+ nativeCurrency: {
404
+ name: 'AVAX',
405
+ symbol: 'AVAX',
406
+ decimals: 18
407
+ }
408
+ });
409
+ }
410
+
411
+ /**
412
+ * Get preset chain configuration
413
+ * @param name Preset name
414
+ * @returns Chain configuration or undefined
415
+ */
416
+ private getPresetChain(name: string): ChainConfig | undefined {
417
+ // Re-use initialized chains
418
+ return this.chains.get(name);
419
+ }
420
+
421
+ /**
422
+ * Get DKLS service instance (for advanced usage)
423
+ * @returns DKLSService instance
424
+ */
425
+ getDKLSService(): DKLSService {
426
+ return this.dklsService;
427
+ }
428
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Bitcoin chain base class
3
+ */
4
+
5
+ export class BitcoinChain {
6
+ // TODO: Implement Bitcoin chain handling
7
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Chain configuration registry
3
+ */
4
+
5
+ export type { ChainConfig, BitcoinConfig } from '../core/types';
6
+
7
+ // TODO: Implement chain registry
@@ -0,0 +1,7 @@
1
+ /**
2
+ * EVM chain base class
3
+ */
4
+
5
+ export class EVMChain {
6
+ // TODO: Implement EVM chain handling
7
+ }