Skip to content

Commit

Permalink
Adds troubleshooting and cleans up titles.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymatthews committed Jan 21, 2025
1 parent 4d02a99 commit 282812b
Showing 1 changed file with 166 additions and 88 deletions.
254 changes: 166 additions & 88 deletions content/reference/javascript-sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,64 +29,73 @@ Before we can use the code, you'll need to create an account.
<!--TODO: explain process to create an account here.-->
## Time to write some code
## Write the script
Let's break down the implementation into smaller parts.
Instead of throwing a giant block of code at your, let's break down the implementation into smaller parts.

### Basic imports

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

```javascript
import { Keyring } from '@entropyxyz/sdk/keys';
import { wasmGlobalsReady, Entropy } from '@entropyxyz/sdk';
import { Buffer } from 'buffer';
```
```javascript
import { Keyring } from '@entropyxyz/sdk/keys';
import { wasmGlobalsReady, Entropy } from '@entropyxyz/sdk';
import { Buffer } from 'buffer';
```
2. Save the file and move onto the next section.
#### Explaining the code
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`).
### Core setup
Read through and add this block of code below the existing code in your `entropy-demo.mjs` file:
```javascript
async function runEntropyDemo() {
try {
// Wait for WASM to be ready.
console.log('Initializing WASM...');
await wasmGlobalsReady();
console.log('WASM initialized successfully');
// Replace this with your actual seed from the Entropy platform.
const seed = '0x786ad0e2df456fe43dd1f91ebca22e235bc162e0bb8d53c633e8c85b2af68b7a';
// Initialize the keystore with your seed.
const keyStore = { seed };
console.log('Keystore initialized');
// Create a new keyring instance.
const keyring = new Keyring(keyStore);
console.log('Keyring created successfully');
// Configure the Entropy connection.
const opts = {
endpoint: 'wss://testnet.entropy.xyz',
keyring
};
// Initialize Entropy.
console.log('Connecting to Entropy network...');
const entropy = new Entropy(opts);
await entropy.ready;
console.log('Successfully connected to Entropy network');
return entropy;
} catch (error) {
console.error('Error in setup:', error);
throw error;
1. Read through and add this block of code below the existing code in your `entropy-demo.mjs` file:
```javascript
async function runEntropyDemo() {
try {
// Wait for WASM to be ready.
console.log('Initializing WASM...');
await wasmGlobalsReady();
console.log('WASM initialized successfully');
// Replace this with your actual seed from the Entropy platform.
const seed = '0x786ad0e2df456fe43dd1f91ebca22e235bc162e0bb8d53c633e8c85b2af68b7a';
// Initialize the keystore with your seed.
const keyStore = { seed };
console.log('Keystore initialized');
// Create a new keyring instance.
const keyring = new Keyring(keyStore);
console.log('Keyring created successfully');
// Configure the Entropy connection.
const opts = {
endpoint: 'wss://testnet.entropy.xyz',
keyring
};
// Initialize Entropy.
console.log('Connecting to Entropy network...');
const entropy = new Entropy(opts);
await entropy.ready;
console.log('Successfully connected to Entropy network');
return entropy;
} catch (error) {
console.error('Error in setup:', error);
throw error;
}
}
}
```
```
1. On line 8, enter the seed of your account.
1. Save the file and move onto the next section.
#### Explaining the code
This codeblock is setting up the core infrastructure needed to connect to the Entropy network. Here's what's happening step by step:
Expand All @@ -103,49 +112,53 @@ Finally, this function creates an `Entropy` instance with these options and wait
### Creating and verifying signatures
Finally, read through and add this function to the file:
```javascript
async function createAndVerifySignature(entropy) {
try {
// Create a message to sign. Feel free to change this if you want.
const message = 'Hello world: signature from entropy!';
console.log(`Creating signature for message: ${message}`);
const msgObject = {
msg: Buffer.from(message).toString('hex')
};
// Register with the network.
console.log('Registering with network...');
const verifyingKey = await entropy.register();
console.log('Registration successful. Verifying key:', verifyingKey);
// Create signature.
console.log('Creating signature...');
const signatureData = await entropy.signWithAdaptersInOrder({
msg: msgObject,
order: ['deviceKeyProxy']
});
console.log('Signature created:', signatureData);
// Verify the signature.
console.log('Verifying signature...');
const isValid = await entropy.verify(signatureData);
if (!isValid) {
throw new Error('Signature verification failed');
}
console.log('Signature verified successfully!');
return signatureData;
1. Finally, read through and add this function to the file:
} catch (error) {
console.error('Error in signature creation/verification:', error);
throw error;
```javascript
async function createAndVerifySignature(entropy) {
try {
// Create a message to sign. Feel free to change this if you want.
const message = 'Hello world: signature from entropy!';
console.log(`Creating signature for message: ${message}`);
const msgObject = {
msg: Buffer.from(message).toString('hex')
};
// Register with the network.
console.log('Registering with network...');
const verifyingKey = await entropy.register();
console.log('Registration successful. Verifying key:', verifyingKey);
// Create signature.
console.log('Creating signature...');
const signatureData = await entropy.signWithAdaptersInOrder({
msg: msgObject,
order: ['deviceKeyProxy']
});
console.log('Signature created:', signatureData);
// Verify the signature.
console.log('Verifying signature...');
const isValid = await entropy.verify(signatureData);
if (!isValid) {
throw new Error('Signature verification failed');
}
console.log('Signature verified successfully!');
return signatureData;
} catch (error) {
console.error('Error in signature creation/verification:', error);
throw error;
}
}
}
```
```
1. Save the file and move onto the next section.
#### Explaining the code
This code handles the actual signature creation and verification process. Here's what's happening step by step:
Expand Down Expand Up @@ -180,3 +193,68 @@ Like before, the entire process is wrapped in a try-catch block to handle any er
```shell
node entropy-demo.js
```
1. You should receive output similar to this:
```shell
Initializing WASM...
WASM initialized successfully
Keystore initialized
Keyring created successfully
Connecting to Entropy network...
Successfully connected to Entropy network
Creating signature for message: Hello world: signature from entropy!
Registering with network...
Registration successful. Verifying key: 0x0352a439ec6eec3f7401bcb2c4d10e60d1e40b06bbbc5ef346e59a3675ed44ff8e
Creating signature...
Signature created: {
signature: '0x2050d22bc54e5ef57b5ca3c81bf873e2bc324d866c30259b0fcc3b99602c454d471cb8db6b40bebd422345ccfc98eafed38a4f74b5e6827ba43b97095dcfabc601',
hashType: 'keccak',
verifyingKey: '0x0352a439ec6eec3f7401bcb2c4d10e60d1e40b06bbbc5ef346e59a3675ed44ff8e',
message: '0x7b226d7367223a22343836353663366336663230373736663732366336343361323037333639363736653631373437353732363532303636373236663664323036353665373437323666373037393231227d'
}
Verifying signature...
Signature verified successfully!
Complete signature data: {
signature: '0x2050d22bc54e5ef57b5ca3c81bf873e2bc324d866c30259b0fcc3b99602c454d471cb8db6b40bebd422345ccfc98eafed38a4f74b5e6827ba43b97095dcfabc601',
hashType: 'keccak',
verifyingKey: '0x0352a439ec6eec3f7401bcb2c4d10e60d1e40b06bbbc5ef346e59a3675ed44ff8e',
message: '0x7b226d7367223a22343836353663366336663230373736663732366336343361323037333639363736653631373437353732363532303636373236663664323036353665373437323666373037393231227d'
}
```
## Troubleshooting
**Invalid Transaction: Inability to pay some fees**
You may encounter the following error when running the script:
```shell
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
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
Error in signature creation/verification: Error: 1010: Invalid Transaction: Inability to pay some fees , e.g. account balance too low
at file:///home/johnny/Code/entropy/testing-the-sdk/node_modules/@entropyxyz/sdk/dist/index.js:59:16
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
Main execution error: Error: 1010: Invalid Transaction: Inability to pay some fees , e.g. account balance too low
at file:///home/johnny/Code/entropy/testing-the-sdk/node_modules/@entropyxyz/sdk/dist/index.js:59:16
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
```
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.
**Undefined is not iterable at verifyAndPick**
You may encounter this error when running the script:
```shell
Creating signature...
Error in signature creation/verification: TypeError: undefined is not iterable (can
not read property Symbol(Symbol.iterator))
at #verifyAndPick (file:///home/johnny/Code/entropy/testing-the-sdk/node_modules/@entropyxyz/sdk/dist/index.js:440:26)
at SignatureRequestManager.sign (file:///home/johnny/Code/entropy/testing-the-sdk/node_modules/@entropyxyz/sdk/dist/index.js:330:48)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
[...]
```
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 comments on commit 282812b

Please sign in to comment.