Hello World
Bounty Instructions
Thanks for running our Hello World! After you’ve followed the steps below, click here to claim your WEV NFT to unlock early access to our future exclusive bounties, hackathon fellowships, events, token launch, and DAO.
Context on What's Happening
In this exercise, we're going to walk you through the basics of connecting to a Weavechain node, writing data, and reading it.
- In the first step, you'll create a public / private keypair that represents your account. Normally you'd save this to be able to 'login again' with the same account, but for this exercise we don't need to keep it.
- The first step also includes a step to write "Hello World!" to the node, with many fields nulled out to be filled automatically by the server.
- The second step creates a second user, connects to the node, and then reads the last record written by the previous user.
- The third step is purely informational, describing the definition of the remote table.
Javascript
Import the API library
import { WeaveAPI, WeaveHelper } from "@weavechain/weave-js-api"
1. Create a new session, then write a message
(hover the code block and click ⏵ to run it)
// Generate a key pair
const [ pub, pvk ] = WeaveHelper.generateKeys();
console.log("Public key: ", pub);
console.log("Private key: ", pvk);
// Connect to a public node (which was configured to allow writes from anyone)
const node = "https://public.weavechain.com:443/92f30f0b6be2732cb817c19839b0940c";
const organization = "weavedemo";
const encrypted = node.startsWith("http://");
// Create a new session
const cfg = WeaveHelper.getConfig(node, pub, pvk, encrypted)
const nodeApi = new WeaveAPI().create(cfg);
await nodeApi.init();
// Write a new record in a pre-defined table
// hosted by the public node in an SQLite database
// Note: the table is public for this example, but it could have full RBAC
const scope = "shared";
const table = "hello";
const session = await nodeApi.login(organization, pub, scope || "*");
// The target table can optionally have some special columns that are auto-filled by the backend
const record = [
null, //id, filled server side
null, //timestamp, filled server side
null, //writer public key, filled server side
null, //signature, filled server side
null, //writer IP, filled server side. Erasure is applied to obfuscate it on read
"*", //row level read access control (could be roles, accounts, blockchain wallets, NFTs etc.)
"Hello World!"
];
const records = new WeaveHelper.Records(table, [ record ]);
await nodeApi.write(session, scope, records, WeaveHelper.Options.WRITE_DEFAULT);
2. Create a new session, read the last inserted record
const [ pub, pvk ] = WeaveHelper.generateKeys();
console.log("Public key: ", pub);
console.log("Private key: ", pvk);
// Connect
const node = "https://public.weavechain.com:443/92f30f0b6be2732cb817c19839b0940c";
const organization = "weavedemo";
const encrypted = node.startsWith("http://");
// Create a new session
const cfg = WeaveHelper.getConfig(node, pub, pvk, encrypted)
const nodeApi = new WeaveAPI().create(cfg);
await nodeApi.init();
const scope = "shared";
const table = "hello";
const session = await nodeApi.login(organization, pub, scope || "*");
// Read the last record
const limit = 1;
const filter = new WeaveHelper.Filter(null, {"id": "DESC"}, limit, null, null, null);
const result = await nodeApi.read(session, scope, table, filter, WeaveHelper.Options.READ_DEFAULT_NO_CHAIN);
console.log(result);
3. Remote table definition
{
"columns": {
"id": { "type": "LONG", "isIndexed": True, "isUnique": True, "isNullable": False },
"ts": { "type": "LONG" },
"pubkey": { "type": "STRING" },
"sig": { "type": "STRING" },
"ip": { "type": "STRING", "readTransform": "ERASURE" },
"roles": { "type": "STRING" },
"message": { "type": "STRING" }
},
"idColumnIndex": 0,
"timestampColumnIndex": 1,
"ownerColumnIndex": 2,
"signatureColumnIndex": 3,
"sourceIpColumnIndex": 4,
"allowedRolesColumnIndex": 5,
"isLocal": False,
"applyReadTransformations": True
}
- idColumnIndex: specifies the column to contain an autogenerated ID
- timestampColumnIndex: fills the column automatically with the network time
- ownerColumnIndex: fills the column automatically with the public key of the writer
- signatureColumnIndex: fills the column with details about the record at write time (hash and signature)
- sourceIpColumnIndex: fills the IP column automatically with the source writer
- allowedRolesColumnIndex: Optional feature: row-level read access (controlled by the writer). "*" allows anyone to read it
- isLocal": set to False allows reading by remote clients
- applyReadTransformations: set to True enables data transformations at read