@web3games-js/api
v0.1.3
Published
A JavaScript library that provides functionality to connect Web3Games-Chain Component APIs.
Downloads
3
Maintainers
Readme
Description
The Web3Games-JS API provides a set of utilities, libraries and tools that enable JavaScript applications to interact with smart contracts or pallets running in the Web3Games network via queries to a Web3Games node.
Installation
npm install @web3games-js/api
or
yarn add @web3games-js/api
Getting started
Start an API connection to a running node on localhost:
import { Web3GamesApi } from '@web3games-js/api';
const Web3GamesApi = await Web3GamesApi.create();
You can also connect to a different node:
const Web3GamesApi = await Web3GamesApi.create({ providerAddress: 'wss://devnet.web3games.org/' });
Getting node info:
const chain = await Web3GamesApi.chain();
const nodeName = await Web3GamesApi.nodeName();
const nodeVersion = await Web3GamesApi.nodeVersion();
const genesis = Web3GamesApi.genesisHash.toHex();
Payloads and metadata
Encode / decode payloads
It's necessary to send only bytes to interact with programs on blockchain.
For that purpose we use the scale-codec
implementation from @polkadot-js
You can use static CreateType.create
method to encode and decode data
import { CreateType } from '@web3games-js/api';
// If "TypeName" alredy registred
const result = CreateType.create('TypeName', somePayload);
// Otherwise need to add metadata containing TypeName and all required types
const result = CreateType.create('TypeName', somePayload, metadata);
Result of this functions is data of type Codec
and it has the next methods
result.toHex(); // - returns a hex represetation of the value
result.toHuman(); // - returns human friendly object representation of the value
result.toString(); // - returns a string represetation of the value
result.toU8a(); // - encodes the value as a Unit8Array
result.toJSON(); // - converts the value to JSON
Events
Subscribe to all events
const unsub = await Web3GamesApi.query.system.events((events) => {
console.log(events.toHuman());
});
// Unsubscribe
unsub();
Blocks
Get block data
const data = await Web3GamesApi.blocks.get(blockNumberOrBlockHash);
console.log(data.toHuman());
Get block timestamp
const ts = await Web3GamesApi.blocks.getBlockTimestamp(blockNumberOrBlockHash);
console.log(ts.toNumber());
Get blockHash by block number
const hash = await Web3GamesApi.blocks.getBlockHash(blockNumber);
console.log(hash.toHex());
Get block number by blockhash
const hash = await Web3GamesApi.blocks.getBlockNumber(blockHash);
console.log(hash.toNumber());
Get all block's events
const events = await Web3GamesApi.blocks.getEvents(blockHash);
events.forEach((event) => {
console.log(event.toHuman());
});
Get all block's extrinsics
const extrinsics = await Web3GamesApi.blocks.getExtrinsics(blockHash);
extrinsics.forEach((extrinsic) => {
console.log(extrinsic.toHuman());
});
Keyring
Create keyring
To create keyring you can use static methods of Web3GamesKeyring
class.
- Creating a new keyring
import { Web3GamesKeyring } from '@web3games-js/api';
const { keyring, json } = await Web3GamesKeyring.create('keyringName', 'passphrase');
- Getting a keyring from JSON
const jsonKeyring = fs.readFileSync('path/to/keyring.json').toString();
const keyring = Web3GamesKeyring.fromJson(jsonKeyring, 'passphrase');
- Getting JSON for keyring
const json = Web3GamesKeyring.toJson(keyring, 'passphrase');
- Getting a keyring from seed
const seed = '0x496f9222372eca011351630ad276c7d44768a593cecea73685299e06acef8c0a';
const keyring = await Web3GamesKeyring.fromSeed(seed, 'name');
- Getting a keyring from mnemonic
const mnemonic = 'slim potato consider exchange shiver bitter drop carpet helmet unfair cotton eagle';
const keyring = Web3GamesKeyring.fromMnemonic(mnemonic, 'name');
- Generate mnemonic and seed
const { mnemonic, seed } = Web3GamesKeyring.generateMnemonic();
// Getting a seed from mnemonic
const { seed } = Web3GamesKeyring.generateSeed(mnemonic);
Sign data
- Create signature
import { Web3GamesKeyring } from '@web3games-js/api';
const message = 'your message';
const signature = Web3GamesKeyring.sign(keyring, message);
- Validate signature
import { signatureIsValid } from '@web3games-js/api';
const publicKey = keyring.address;
const verified = signatureIsValid(publicKey, signature, message);
Convert public keys into ss58 format and back
Use encodeAddress
and decodeAddress
functions to convert the public key into ss58 format and back.
- Convert to raw format
import { decodeAddress } from '@web3games-js/api';
console.log(decodeAddress('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'))
- Convert to ss58 format
import { encodeAddress } from '@web3games-js/api';
console.log(encodeAddress('0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d'))