Skip to content

Commit 282812b

Browse files
Adds troubleshooting and cleans up titles.
1 parent 4d02a99 commit 282812b

File tree

1 file changed

+166
-88
lines changed

1 file changed

+166
-88
lines changed

content/reference/javascript-sdk.md

Lines changed: 166 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -29,64 +29,73 @@ Before we can use the code, you'll need to create an account.
2929
3030
<!--TODO: explain process to create an account here.-->
3131
32-
## Time to write some code
32+
## Write the script
3333
34-
Let's break down the implementation into smaller parts.
34+
Instead of throwing a giant block of code at your, let's break down the implementation into smaller parts.
3535

3636
### Basic imports
3737

38-
Create a new file called `entropy-demo.mjs` and add the following lines.
38+
1. Create a new file called `entropy-demo.mjs` and add the following lines:
3939

40-
```javascript
41-
import { Keyring } from '@entropyxyz/sdk/keys';
42-
import { wasmGlobalsReady, Entropy } from '@entropyxyz/sdk';
43-
import { Buffer } from 'buffer';
44-
```
40+
```javascript
41+
import { Keyring } from '@entropyxyz/sdk/keys';
42+
import { wasmGlobalsReady, Entropy } from '@entropyxyz/sdk';
43+
import { Buffer } from 'buffer';
44+
```
45+
46+
2. Save the file and move onto the next section.
47+
48+
#### Explaining the code
4549
4650
These imports set up the fundamental building blocks needed to manage your cryptographic keys (`Keyring`), connect to and interact with the Entropy network (`Entropy`), ensure the cryptographic operations are ready (`wasmGlobalsReady`), and handle the necessary data conversions (`Buffer`).
4751
4852
### Core setup
4953
50-
Read through and add this block of code below the existing code in your `entropy-demo.mjs` file:
51-
52-
```javascript
53-
async function runEntropyDemo() {
54-
try {
55-
// Wait for WASM to be ready.
56-
console.log('Initializing WASM...');
57-
await wasmGlobalsReady();
58-
console.log('WASM initialized successfully');
59-
60-
// Replace this with your actual seed from the Entropy platform.
61-
const seed = '0x786ad0e2df456fe43dd1f91ebca22e235bc162e0bb8d53c633e8c85b2af68b7a';
62-
63-
// Initialize the keystore with your seed.
64-
const keyStore = { seed };
65-
console.log('Keystore initialized');
66-
67-
// Create a new keyring instance.
68-
const keyring = new Keyring(keyStore);
69-
console.log('Keyring created successfully');
70-
71-
// Configure the Entropy connection.
72-
const opts = {
73-
endpoint: 'wss://testnet.entropy.xyz',
74-
keyring
75-
};
76-
77-
// Initialize Entropy.
78-
console.log('Connecting to Entropy network...');
79-
const entropy = new Entropy(opts);
80-
await entropy.ready;
81-
console.log('Successfully connected to Entropy network');
82-
83-
return entropy;
84-
} catch (error) {
85-
console.error('Error in setup:', error);
86-
throw error;
54+
1. Read through and add this block of code below the existing code in your `entropy-demo.mjs` file:
55+
56+
```javascript
57+
async function runEntropyDemo() {
58+
try {
59+
// Wait for WASM to be ready.
60+
console.log('Initializing WASM...');
61+
await wasmGlobalsReady();
62+
console.log('WASM initialized successfully');
63+
64+
// Replace this with your actual seed from the Entropy platform.
65+
const seed = '0x786ad0e2df456fe43dd1f91ebca22e235bc162e0bb8d53c633e8c85b2af68b7a';
66+
67+
// Initialize the keystore with your seed.
68+
const keyStore = { seed };
69+
console.log('Keystore initialized');
70+
71+
// Create a new keyring instance.
72+
const keyring = new Keyring(keyStore);
73+
console.log('Keyring created successfully');
74+
75+
// Configure the Entropy connection.
76+
const opts = {
77+
endpoint: 'wss://testnet.entropy.xyz',
78+
keyring
79+
};
80+
81+
// Initialize Entropy.
82+
console.log('Connecting to Entropy network...');
83+
const entropy = new Entropy(opts);
84+
await entropy.ready;
85+
console.log('Successfully connected to Entropy network');
86+
87+
return entropy;
88+
} catch (error) {
89+
console.error('Error in setup:', error);
90+
throw error;
91+
}
8792
}
88-
}
89-
```
93+
```
94+
95+
1. On line 8, enter the seed of your account.
96+
1. Save the file and move onto the next section.
97+
98+
#### Explaining the code
9099
91100
This codeblock is setting up the core infrastructure needed to connect to the Entropy network. Here's what's happening step by step:
92101
@@ -103,49 +112,53 @@ Finally, this function creates an `Entropy` instance with these options and wait
103112
104113
### Creating and verifying signatures
105114
106-
Finally, read through and add this function to the file:
107-
108-
```javascript
109-
async function createAndVerifySignature(entropy) {
110-
try {
111-
// Create a message to sign. Feel free to change this if you want.
112-
const message = 'Hello world: signature from entropy!';
113-
console.log(`Creating signature for message: ${message}`);
114-
115-
const msgObject = {
116-
msg: Buffer.from(message).toString('hex')
117-
};
118-
119-
// Register with the network.
120-
console.log('Registering with network...');
121-
const verifyingKey = await entropy.register();
122-
console.log('Registration successful. Verifying key:', verifyingKey);
123-
124-
// Create signature.
125-
console.log('Creating signature...');
126-
const signatureData = await entropy.signWithAdaptersInOrder({
127-
msg: msgObject,
128-
order: ['deviceKeyProxy']
129-
});
130-
console.log('Signature created:', signatureData);
131-
132-
// Verify the signature.
133-
console.log('Verifying signature...');
134-
const isValid = await entropy.verify(signatureData);
135-
136-
if (!isValid) {
137-
throw new Error('Signature verification failed');
138-
}
139-
140-
console.log('Signature verified successfully!');
141-
return signatureData;
115+
1. Finally, read through and add this function to the file:
142116
143-
} catch (error) {
144-
console.error('Error in signature creation/verification:', error);
145-
throw error;
117+
```javascript
118+
async function createAndVerifySignature(entropy) {
119+
try {
120+
// Create a message to sign. Feel free to change this if you want.
121+
const message = 'Hello world: signature from entropy!';
122+
console.log(`Creating signature for message: ${message}`);
123+
124+
const msgObject = {
125+
msg: Buffer.from(message).toString('hex')
126+
};
127+
128+
// Register with the network.
129+
console.log('Registering with network...');
130+
const verifyingKey = await entropy.register();
131+
console.log('Registration successful. Verifying key:', verifyingKey);
132+
133+
// Create signature.
134+
console.log('Creating signature...');
135+
const signatureData = await entropy.signWithAdaptersInOrder({
136+
msg: msgObject,
137+
order: ['deviceKeyProxy']
138+
});
139+
console.log('Signature created:', signatureData);
140+
141+
// Verify the signature.
142+
console.log('Verifying signature...');
143+
const isValid = await entropy.verify(signatureData);
144+
145+
if (!isValid) {
146+
throw new Error('Signature verification failed');
147+
}
148+
149+
console.log('Signature verified successfully!');
150+
return signatureData;
151+
152+
} catch (error) {
153+
console.error('Error in signature creation/verification:', error);
154+
throw error;
155+
}
146156
}
147-
}
148-
```
157+
```
158+
159+
1. Save the file and move onto the next section.
160+
161+
#### Explaining the code
149162
150163
This code handles the actual signature creation and verification process. Here's what's happening step by step:
151164
@@ -180,3 +193,68 @@ Like before, the entire process is wrapped in a try-catch block to handle any er
180193
```shell
181194
node entropy-demo.js
182195
```
196+
197+
1. You should receive output similar to this:
198+
199+
```shell
200+
Initializing WASM...
201+
WASM initialized successfully
202+
Keystore initialized
203+
Keyring created successfully
204+
Connecting to Entropy network...
205+
Successfully connected to Entropy network
206+
Creating signature for message: Hello world: signature from entropy!
207+
Registering with network...
208+
Registration successful. Verifying key: 0x0352a439ec6eec3f7401bcb2c4d10e60d1e40b06bbbc5ef346e59a3675ed44ff8e
209+
Creating signature...
210+
Signature created: {
211+
signature: '0x2050d22bc54e5ef57b5ca3c81bf873e2bc324d866c30259b0fcc3b99602c454d471cb8db6b40bebd422345ccfc98eafed38a4f74b5e6827ba43b97095dcfabc601',
212+
hashType: 'keccak',
213+
verifyingKey: '0x0352a439ec6eec3f7401bcb2c4d10e60d1e40b06bbbc5ef346e59a3675ed44ff8e',
214+
message: '0x7b226d7367223a22343836353663366336663230373736663732366336343361323037333639363736653631373437353732363532303636373236663664323036353665373437323666373037393231227d'
215+
}
216+
Verifying signature...
217+
Signature verified successfully!
218+
Complete signature data: {
219+
signature: '0x2050d22bc54e5ef57b5ca3c81bf873e2bc324d866c30259b0fcc3b99602c454d471cb8db6b40bebd422345ccfc98eafed38a4f74b5e6827ba43b97095dcfabc601',
220+
hashType: 'keccak',
221+
verifyingKey: '0x0352a439ec6eec3f7401bcb2c4d10e60d1e40b06bbbc5ef346e59a3675ed44ff8e',
222+
message: '0x7b226d7367223a22343836353663366336663230373736663732366336343361323037333639363736653631373437353732363532303636373236663664323036353665373437323666373037393231227d'
223+
}
224+
```
225+
226+
## Troubleshooting
227+
228+
**Invalid Transaction: Inability to pay some fees**
229+
230+
You may encounter the following error when running the script:
231+
232+
```shell
233+
2025-01-21 11:51:27 RPC-CORE: submitAndWatchExtrinsic(extrinsic: Extrinsic): ExtrinsicStatus:: 1010: Invalid Transaction: Inability to pay some fees , e.g. account balance too low
234+
2025-01-21 11:51:27 RPC-CORE: submitAndWatchExtrinsic(extrinsic: Extrinsic): ExtrinsicStatus:: 1010: Invalid Transaction: Inability to pay some fees , e.g. account balance too low
235+
Error in signature creation/verification: Error: 1010: Invalid Transaction: Inability to pay some fees , e.g. account balance too low
236+
at file:///home/johnny/Code/entropy/testing-the-sdk/node_modules/@entropyxyz/sdk/dist/index.js:59:16
237+
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
238+
Main execution error: Error: 1010: Invalid Transaction: Inability to pay some fees , e.g. account balance too low
239+
at file:///home/johnny/Code/entropy/testing-the-sdk/node_modules/@entropyxyz/sdk/dist/index.js:59:16
240+
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
241+
```
242+
243+
The main thing to look out for here is the line `2025-01-21 11:51:27 RPC-CORE: submitAndWatchExtrinsic(extrinsic: Extrinsic): ExtrinsicStatus:: 1010: Invalid Transaction: Inability to pay some fees , e.g. account balance too low`. This error happens because the network has determined that the seed you have used to create your account in the script doesn't have enough funds to contrinue the registering and signing process. Take a look at the [Getting Funds]({{< relref "get-funds" >}}) guide to find out how to deal with this error.
244+
245+
**Undefined is not iterable at verifyAndPick**
246+
247+
You may encounter this error when running the script:
248+
249+
```shell
250+
Creating signature...
251+
Error in signature creation/verification: TypeError: undefined is not iterable (can
252+
not read property Symbol(Symbol.iterator))
253+
at #verifyAndPick (file:///home/johnny/Code/entropy/testing-the-sdk/node_modules/@entropyxyz/sdk/dist/index.js:440:26)
254+
at SignatureRequestManager.sign (file:///home/johnny/Code/entropy/testing-the-sdk/node_modules/@entropyxyz/sdk/dist/index.js:330:48)
255+
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
256+
257+
[...]
258+
```
259+
260+
This is likely because the account you are using within the script has already been registered with the signing program we're using here. Try creating a new account, getting some test funds, and adding the seed of the new account into the script.

0 commit comments

Comments
 (0)