Skip to content

Commit 4d02a99

Browse files
Cleans up formatting a lil bit.
1 parent 1d40267 commit 4d02a99

File tree

1 file changed

+27
-138
lines changed

1 file changed

+27
-138
lines changed

content/reference/javascript-sdk.md

Lines changed: 27 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,16 @@ lead: ""
55

66
## Prerequisites
77

8-
Before starting this tutorial, make sure you have:
9-
10-
1. Node.js
11-
1. An Entropy account (we'll cover how to create one).
8+
Before starting this tutorial, make sure you have Node v20.0.0 or above installed
129

1310
## Setting up your project
1411

15-
1. First, create a new directory and initialize a Node.js project:
12+
1. First, create a new directory and initialize a Node project:
1613

1714
```shell
1815
mkdir entropy-sdk-demo
1916
cd entropy-sdk-demo
2017
npm init -y
21-
cd entropy-sdk-demo
2218
```
2319

2420
1. Install the required dependencies:
@@ -29,7 +25,7 @@ Before starting this tutorial, make sure you have:
2925

3026
## Creating your Entropy account
3127

32-
Before we can use the code, you'll need to:
28+
Before we can use the code, you'll need to create an account.
3329
3430
<!--TODO: explain process to create an account here.-->
3531
@@ -39,48 +35,46 @@ Let's break down the implementation into smaller parts.
3935

4036
### Basic imports
4137

42-
Create a new file called `entropy-demo.js` and add the following lines.
38+
Create a new file called `entropy-demo.mjs` and add the following lines.
4339

4440
```javascript
4541
import { Keyring } from '@entropyxyz/sdk/keys';
4642
import { wasmGlobalsReady, Entropy } from '@entropyxyz/sdk';
47-
48-
// Add this at the top of your file to handle ES modules
4943
import { Buffer } from 'buffer';
5044
```
5145
5246
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`).
5347
5448
### Core setup
5549
56-
Read through and add this block of code:
50+
Read through and add this block of code below the existing code in your `entropy-demo.mjs` file:
5751
5852
```javascript
5953
async function runEntropyDemo() {
6054
try {
61-
// Wait for WASM to be ready
55+
// Wait for WASM to be ready.
6256
console.log('Initializing WASM...');
6357
await wasmGlobalsReady();
6458
console.log('WASM initialized successfully');
6559
66-
// Replace this with your actual seed from the Entropy platform
60+
// Replace this with your actual seed from the Entropy platform.
6761
const seed = '0x786ad0e2df456fe43dd1f91ebca22e235bc162e0bb8d53c633e8c85b2af68b7a';
6862
69-
// Initialize the keystore with your seed
63+
// Initialize the keystore with your seed.
7064
const keyStore = { seed };
7165
console.log('Keystore initialized');
7266
73-
// Create a new keyring instance
67+
// Create a new keyring instance.
7468
const keyring = new Keyring(keyStore);
7569
console.log('Keyring created successfully');
7670
77-
// Configure the Entropy connection
71+
// Configure the Entropy connection.
7872
const opts = {
79-
endpoint: 'wss://testnet.entropy.xyz', // Use testnet endpoint
73+
endpoint: 'wss://testnet.entropy.xyz',
8074
keyring
8175
};
8276
83-
// Initialize Entropy
77+
// Initialize Entropy.
8478
console.log('Connecting to Entropy network...');
8579
const entropy = new Entropy(opts);
8680
await entropy.ready;
@@ -114,28 +108,28 @@ Finally, read through and add this function to the file:
114108
```javascript
115109
async function createAndVerifySignature(entropy) {
116110
try {
117-
// Create a message to sign
111+
// Create a message to sign. Feel free to change this if you want.
118112
const message = 'Hello world: signature from entropy!';
119113
console.log(`Creating signature for message: ${message}`);
120114
121115
const msgObject = {
122116
msg: Buffer.from(message).toString('hex')
123117
};
124118
125-
// Register with the network
119+
// Register with the network.
126120
console.log('Registering with network...');
127121
const verifyingKey = await entropy.register();
128122
console.log('Registration successful. Verifying key:', verifyingKey);
129123
130-
// Create signature
124+
// Create signature.
131125
console.log('Creating signature...');
132126
const signatureData = await entropy.signWithAdaptersInOrder({
133127
msg: msgObject,
134128
order: ['deviceKeyProxy']
135129
});
136130
console.log('Signature created:', signatureData);
137131
138-
// Verify the signature
132+
// Verify the signature.
139133
console.log('Verifying signature...');
140134
const isValid = await entropy.verify(signatureData);
141135
@@ -170,124 +164,19 @@ This code handles the actual signature creation and verification process. Here's
170164
171165
Like before, the entire process is wrapped in a try-catch block to handle any errors that might occur during signing or verification.
172166
173-
### Putting It All Together
174-
175-
Here's the complete script that combines all the parts:
176-
177-
```javascript
178-
import { Keyring } from '@entropyxyz/sdk/keys';
179-
import { wasmGlobalsReady, Entropy } from '@entropyxyz/sdk';
180-
import { Buffer } from 'buffer';
181-
182-
async function runEntropyDemo() {
183-
try {
184-
// Wait for WASM to be ready
185-
console.log('Initializing WASM...');
186-
await wasmGlobalsReady();
187-
console.log('WASM initialized successfully');
188-
189-
// Replace this with your actual seed from the Entropy platform
190-
const seed = '0x786ad0e2df456fe43dd1f91ebca22e235bc162e0bb8d53c633e8c85b2af68b7a';
191-
192-
// Initialize the keystore with your seed
193-
const keyStore = { seed };
194-
console.log('Keystore initialized');
195-
196-
// Create a new keyring instance
197-
const keyring = new Keyring(keyStore);
198-
console.log('Keyring created successfully');
199-
200-
// Configure the Entropy connection
201-
const opts = {
202-
endpoint: 'wss://testnet.entropy.xyz', // Use testnet endpoint
203-
keyring
204-
};
205-
206-
// Initialize Entropy
207-
console.log('Connecting to Entropy network...');
208-
const entropy = new Entropy(opts);
209-
await entropy.ready;
210-
console.log('Successfully connected to Entropy network');
211-
212-
return entropy;
213-
} catch (error) {
214-
console.error('Error in setup:', error);
215-
throw error;
216-
}
217-
}
218-
219-
async function createAndVerifySignature(entropy) {
220-
try {
221-
// Create a message to sign
222-
const message = 'Hello world: signature from entropy!';
223-
console.log(`Creating signature for message: ${message}`);
224-
225-
const msgObject = {
226-
msg: Buffer.from(message).toString('hex')
227-
};
228-
229-
// Register with the network
230-
console.log('Registering with network...');
231-
const verifyingKey = await entropy.register();
232-
console.log('Registration successful. Verifying key:', verifyingKey);
233-
234-
// Create signature
235-
console.log('Creating signature...');
236-
const signatureData = await entropy.signWithAdaptersInOrder({
237-
msg: msgObject,
238-
order: ['deviceKeyProxy']
239-
});
240-
console.log('Signature created:', signatureData);
241-
242-
// Verify the signature
243-
console.log('Verifying signature...');
244-
const isValid = await entropy.verify(signatureData);
245-
246-
if (!isValid) {
247-
throw new Error('Signature verification failed');
248-
}
249-
250-
console.log('Signature verified successfully!');
251-
return signatureData;
167+
## Running the code
252168
253-
} catch (error) {
254-
console.error('Error in signature creation/verification:', error);
255-
throw error;
256-
} finally {
257-
// Clean up by closing the connection
258-
await entropy.close();
259-
}
260-
}
169+
1. Save all the code.
170+
1. Update your `package.json` to include:
261171
262-
// Everything gets ran from here.
263-
async function main() {
264-
try {
265-
const entropy = await runEntropyDemo();
266-
const signatureData = await createAndVerifySignature(entropy);
267-
console.log('Complete signature data:', signatureData);
268-
} catch (error) {
269-
console.error('Main execution error:', error);
172+
```json
173+
{
174+
"type": "module"
270175
}
271-
}
272-
273-
// Run the demo
274-
main();
275-
```
276-
277-
## Running the Tutorial
278-
279-
1. Save all the code above in a file named `entropy-demo.js`.
280-
2. Update your `package.json` to include:
281-
282-
```json
283-
{
284-
"type": "module"
285-
}
286-
```
176+
```
287177
288-
3. Replace the seed value with your actual seed from the Entropy platform.
289-
4. Run the script:
178+
1. Run the script:
290179
291-
```bash
292-
node entropy-demo.js
293-
```
180+
```shell
181+
node entropy-demo.js
182+
```

0 commit comments

Comments
 (0)