@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
package/test-sdk.js ADDED
@@ -0,0 +1,234 @@
1
+ const { BitShardSDK } = require('./dist/index.js');
2
+
3
+ async function testSDK() {
4
+ console.log('Testing BitShard SDK...');
5
+
6
+ try {
7
+ // Create SDK instance
8
+ const sdk = new BitShardSDK();
9
+ console.log('āœ… SDK created successfully');
10
+
11
+ // Create a local wallet
12
+ const wallet = await sdk.createLocalWallet({
13
+ totalParties: 3,
14
+ threshold: 2
15
+ });
16
+ console.log('āœ… Local wallet created');
17
+ console.log(' Public key:', wallet.publicKey);
18
+ console.log(' Keyshares:', wallet.keyshares.length);
19
+
20
+ // Derive addresses
21
+ const addresses = sdk.deriveAddresses(wallet.publicKey);
22
+ console.log('āœ… Addresses derived:');
23
+ console.log(' Ethereum:', addresses.ethereum);
24
+ console.log(' Bitcoin:', addresses.bitcoin);
25
+ console.log(' Arbitrum:', addresses.arbitrum);
26
+
27
+ // Test signing with threshold (2 of 3)
28
+ const message = 'Hello BitShard!';
29
+ const signature = await sdk.personalSignWithWallet(message, wallet, { threshold: 2 });
30
+ console.log('āœ… Message signed:');
31
+ console.log(' Signature:', signature.signature);
32
+
33
+ // Verify the signature
34
+ const { ethers } = require('ethers');
35
+ try {
36
+ const fullSig = signature.signature + signature.v.toString(16).padStart(2, '0');
37
+ const recoveredAddress = ethers.utils.verifyMessage(message, fullSig);
38
+ if (recoveredAddress.toLowerCase() === addresses.ethereum.toLowerCase()) {
39
+ console.log('āœ… Signature verified! Recovered address matches.');
40
+ console.log(' Recovered address:', recoveredAddress);
41
+ } else {
42
+ console.log('āš ļø Recovered address:', recoveredAddress);
43
+ console.log(' Expected address:', addresses.ethereum);
44
+ return; // Exit if signature doesn't match
45
+ }
46
+ } catch (error) {
47
+ console.log('āš ļø Verification error:', error.message);
48
+ return;
49
+ }
50
+
51
+ // Arbitrum Sepolia transaction functionality
52
+ console.log('\n' + '='.repeat(60));
53
+ console.log('šŸ’° ARBITRUM SEPOLIA TRANSACTION TEST');
54
+ console.log('='.repeat(60));
55
+
56
+ // Set up Arbitrum Sepolia provider
57
+ const ARBITRUM_SEPOLIA_RPC = 'https://sepolia-rollup.arbitrum.io/rpc';
58
+ const provider = new ethers.providers.JsonRpcProvider(ARBITRUM_SEPOLIA_RPC);
59
+
60
+ console.log('\nšŸ“” Connected to Arbitrum Sepolia');
61
+ console.log(' RPC:', ARBITRUM_SEPOLIA_RPC);
62
+
63
+ // Display the address to fund
64
+ console.log('\nšŸ’³ Please send some ETH to this address on Arbitrum Sepolia:');
65
+ console.log(' Address:', addresses.ethereum);
66
+ console.log(' (Minimum: 0.001 ETH for test transaction + gas)');
67
+
68
+ // You can get testnet ETH from:
69
+ console.log('\n🚰 Get testnet ETH from:');
70
+ console.log(' 1. Arbitrum Sepolia Faucet: https://faucet.quicknode.com/arbitrum/sepolia');
71
+ console.log(' 2. Bridge from Sepolia: https://bridge.arbitrum.io/');
72
+
73
+ console.log('\nā³ Monitoring balance (checking every 5 seconds)...');
74
+
75
+ // Monitor balance
76
+ let previousBalance = ethers.BigNumber.from(0);
77
+ const checkBalance = async () => {
78
+ try {
79
+ const balance = await provider.getBalance(addresses.ethereum);
80
+ const balanceEth = ethers.utils.formatEther(balance);
81
+
82
+ if (balance.gt(previousBalance) && previousBalance.gt(0)) {
83
+ console.log(`\nšŸ’° Funds received! New balance: ${balanceEth} ETH`);
84
+ }
85
+
86
+ if (balance.gt(0) && previousBalance.eq(0)) {
87
+ console.log(`\nāœ… Funds detected! Balance: ${balanceEth} ETH`);
88
+ previousBalance = balance;
89
+
90
+ // Create and send transaction
91
+ await sendTransaction();
92
+ return true; // Stop monitoring
93
+ }
94
+
95
+ previousBalance = balance;
96
+
97
+ if (balance.eq(0)) {
98
+ process.stdout.write('.');
99
+ }
100
+
101
+ return false; // Continue monitoring
102
+ } catch (error) {
103
+ console.error('\nāŒ Error checking balance:', error.message);
104
+ return false;
105
+ }
106
+ };
107
+
108
+ // Send transaction function
109
+ const sendTransaction = async () => {
110
+ try {
111
+ console.log('\nšŸ“ Preparing transaction...');
112
+
113
+ // Get current gas price and nonce
114
+ const [gasPrice, nonce] = await Promise.all([
115
+ provider.getGasPrice(),
116
+ provider.getTransactionCount(addresses.ethereum)
117
+ ]);
118
+
119
+ // Convert message to hex data
120
+ // You can customize this message or even pass JSON data
121
+ const message = process.env.TX_MESSAGE || 'bitshard.io - MPC wallet transaction';
122
+ const messageHex = '0x' + Buffer.from(message, 'utf8').toString('hex');
123
+
124
+ // Calculate additional gas needed for data (roughly 68 gas per byte)
125
+ const dataGas = Buffer.from(message).length * 68;
126
+
127
+ // Create transaction with data field
128
+ const tx = {
129
+ to: '0xd8424ee7cc2520f7dae828f84f99f53ac0dd6734', // G recipient
130
+ value: ethers.utils.parseEther('0.0001'), // Send 0.0001 ETH
131
+ data: messageHex, // Add custom message as data
132
+ gasLimit: 21000 + dataGas, // Base gas + data gas
133
+ gasPrice: gasPrice.mul(110).div(100), // Add 10% buffer
134
+ nonce: nonce,
135
+ chainId: 421614, // Arbitrum Sepolia chain ID
136
+ type: 0 // Legacy transaction for simplicity
137
+ };
138
+
139
+ console.log('\nšŸ“‹ Transaction details:');
140
+ console.log(' To:', tx.to);
141
+ console.log(' Value:', ethers.utils.formatEther(tx.value), 'ETH');
142
+ console.log(' Data:', message, `(${messageHex})`);
143
+ console.log(' Gas Price:', ethers.utils.formatUnits(tx.gasPrice, 'gwei'), 'gwei');
144
+ console.log(' Gas Limit:', tx.gasLimit);
145
+ console.log(' Nonce:', tx.nonce);
146
+ console.log(' Chain ID:', tx.chainId);
147
+
148
+ // Serialize the transaction for signing
149
+ const serializedTx = ethers.utils.serializeTransaction(tx);
150
+ const txHash = ethers.utils.keccak256(serializedTx);
151
+
152
+ console.log('\nšŸ–Šļø Signing transaction with MPC wallet...');
153
+ console.log(' Transaction hash:', txHash);
154
+
155
+ // Sign the transaction hash (use signTransactionWithWallet for raw hash)
156
+ const txSignature = await sdk.signTransactionWithWallet(
157
+ txHash,
158
+ wallet,
159
+ { threshold: 2 }
160
+ );
161
+
162
+ console.log(' Signature R:', txSignature.r);
163
+ console.log(' Signature S:', txSignature.s);
164
+ console.log(' Signature V (recovery):', txSignature.v);
165
+
166
+ // For Arbitrum Sepolia, we need to adjust v value for EIP-155
167
+ // v = chainId * 2 + 35 + recovery_id (0 or 1)
168
+ // DKLS returns 27 or 28 based on signature (27 = recovery_id 0, 28 = recovery_id 1)
169
+ // The SDK automatically determines the correct v value by testing recovery
170
+ const recoveryId = txSignature.v - 27; // Will be 0 or 1 depending on the signature
171
+ const eip155V = tx.chainId * 2 + 35 + recoveryId;
172
+
173
+ console.log(' Recovery ID:', recoveryId, `(from v=${txSignature.v})`);
174
+ console.log(' EIP-155 V value:', eip155V);
175
+
176
+ // Add signature to transaction
177
+ const signedTx = ethers.utils.serializeTransaction(tx, {
178
+ r: txSignature.r,
179
+ s: txSignature.s,
180
+ v: eip155V
181
+ });
182
+
183
+ console.log('āœ… Transaction signed');
184
+ console.log(' Signed transaction:', signedTx);
185
+ console.log('\nšŸ“” Broadcasting transaction...');
186
+
187
+ // Send the transaction
188
+ const txResponse = await provider.sendTransaction(signedTx);
189
+ console.log('āœ… Transaction sent!');
190
+ console.log(' Transaction hash:', txResponse.hash);
191
+ console.log(' View on explorer: https://sepolia.arbiscan.io/tx/' + txResponse.hash);
192
+
193
+ console.log('\nā³ Waiting for confirmation...');
194
+ const receipt = await txResponse.wait();
195
+
196
+ console.log('āœ… Transaction confirmed!');
197
+ console.log(' Block number:', receipt.blockNumber);
198
+ console.log(' Gas used:', receipt.gasUsed.toString());
199
+
200
+ // Show how to view the input data
201
+ console.log('\nšŸ“ Transaction Data:');
202
+ console.log(' Input data on chain:', tx.data);
203
+ console.log(' Decoded message:', Buffer.from(tx.data.slice(2), 'hex').toString('utf8'));
204
+ console.log(' View on explorer (check "Input Data" section):');
205
+ console.log(' https://sepolia.arbiscan.io/tx/' + txResponse.hash);
206
+
207
+ // Check final balance
208
+ const finalBalance = await provider.getBalance(addresses.ethereum);
209
+ console.log('\nšŸ’° Final balance:', ethers.utils.formatEther(finalBalance), 'ETH');
210
+
211
+ } catch (error) {
212
+ console.error('\nāŒ Transaction error:', error.message);
213
+ if (error.reason) console.error(' Reason:', error.reason);
214
+ if (error.code) console.error(' Code:', error.code);
215
+ }
216
+ };
217
+
218
+ // Start monitoring loop
219
+ const monitorInterval = setInterval(async () => {
220
+ const shouldStop = await checkBalance();
221
+ if (shouldStop) {
222
+ clearInterval(monitorInterval);
223
+ }
224
+ }, 5000);
225
+
226
+ // Initial balance check
227
+ await checkBalance();
228
+
229
+ } catch (error) {
230
+ console.error('āŒ Error:', error.message);
231
+ }
232
+ }
233
+
234
+ testSDK();