dxos / registry-client

Registry Client
GNU Affero General Public License v3.0
1 stars 1 forks source link

registry-client

Usage

import { Account, Registry } from '@dxos/registry-client';
const endpoint = 'http://localhost:9473/api';
const registry = new Registry(endpoint);

Generate Private & Public Keys

let mnemonic;

if (!mnemonic) {
  mnemonic = Account.generateMnemonic();
}

const key = Account.generateFromMnemonic(mnemonic);

Query Registry

Get accounts:

const addresses = ['cosmos1sgdt4t6eq6thsewcpe2v9cu6c9ru837w7pj9lm'];
const result = await registry.getAccounts(addresses);

Get records by ids:

let ids = ['QmfKZkWQdWFtUnsJt4Uakp3jSzXKHcafY5sMnyPodR9Ks2'];
let result = await registry.getRecordsByIds(ids);

Query records by attributes:

let attributes = { type: 'wrn:bot' };
let result = await registry.queryRecords(attributes);

Get bonds by ids:

let ids = ['8e340dd7cf6fc91c27eeefce9cca1406c262e93fd6f3a4f3b1e99b01161fcef3'];
let result = await registry.getBondsByIds(ids);

Query bonds (by owner):

let owner = 'cosmos1zk8etz23phxgtse8re6tggsr3nrfk2vtsesegy';
const result = await registry.queryBonds({ owner });

Write to Registry

Publish record:

// Private key & bond ID.
let payloadKey = '31c90b358117ea94bb45f1e6bbef7dc5bb20b6cb39f71790dd510a2190fe222b';
let bondId = '8e340dd7cf6fc91c27eeefce9cca1406c262e93fd6f3a4f3b1e99b01161fcef3';

let record = {
  "type": "dxn:protocol",
  "name": "dxos.org/chess",
  "version": "1.5.2"
};

let result = await registry.setRecord(payloadKey, record, txKey, bondId);

Send coins:

let privateKey = 'b1e4e95dd3e3294f15869b56697b5e3bdcaa24d9d0af1be9ee57d5a59457843a';
let toAddress = 'cosmos1w5q7xy9sk8hqvlklftdfdkc3kgsd90cxlkwvty';

await registry.sendCoins([{ denom: 'wire', amount: '100' }], toAddress, privateKey);

Create bond:

let privateKey = 'b1e4e95dd3e3294f15869b56697b5e3bdcaa24d9d0af1be9ee57d5a59457843a';
let denom = 'uwire';
let amount = '10000';

const result = await registry.createBond([{ denom, amount }], privateKey);

Refill bond:

let privateKey = 'b1e4e95dd3e3294f15869b56697b5e3bdcaa24d9d0af1be9ee57d5a59457843a';
let bondId = '8e340dd7cf6fc91c27eeefce9cca1406c262e93fd6f3a4f3b1e99b01161fcef3';
let denom = 'uwire';
let amount = '500';

const result = await registry.refillBond(bondId, [{ denom, amount }], privateKey);

Withdraw funds from bond:

let privateKey = 'b1e4e95dd3e3294f15869b56697b5e3bdcaa24d9d0af1be9ee57d5a59457843a';
let bondId = '8e340dd7cf6fc91c27eeefce9cca1406c262e93fd6f3a4f3b1e99b01161fcef3';
let denom = 'uwire';
let amount = '500';

const result = await registry.withdrawBond(bondId, [{ denom, amount }], privateKey);

Cancel bond:

let privateKey = 'b1e4e95dd3e3294f15869b56697b5e3bdcaa24d9d0af1be9ee57d5a59457843a';
let bondId = '8e340dd7cf6fc91c27eeefce9cca1406c262e93fd6f3a4f3b1e99b01161fcef3';

const result = await registry.cancelBond(bondId, privateKey);

Associate record with bond:

let privateKey = 'b1e4e95dd3e3294f15869b56697b5e3bdcaa24d9d0af1be9ee57d5a59457843a';
let recordId = 'QmfYFV686CmEuDfd12NkoY94AoVYTKqw7ptaovLgiRSxL2';
let bondId = '8e340dd7cf6fc91c27eeefce9cca1406c262e93fd6f3a4f3b1e99b01161fcef3';

const result = await registry.associateBond(recordId, bondId, privateKey);

Dissociate record from bond:

let privateKey = 'b1e4e95dd3e3294f15869b56697b5e3bdcaa24d9d0af1be9ee57d5a59457843a';
let recordId = 'QmfYFV686CmEuDfd12NkoY94AoVYTKqw7ptaovLgiRSxL2';

const result = await registry.dissociateBond(recordId, privateKey);

Dissociate all records from bond:

let privateKey = 'b1e4e95dd3e3294f15869b56697b5e3bdcaa24d9d0af1be9ee57d5a59457843a';
let bondId = '8e340dd7cf6fc91c27eeefce9cca1406c262e93fd6f3a4f3b1e99b01161fcef3';

const result = await registry.dissociateRecords(bondId, privateKey);

Reassociate records with new bond (switch bond):

let privateKey = 'b1e4e95dd3e3294f15869b56697b5e3bdcaa24d9d0af1be9ee57d5a59457843a';
let oldBondId = '8e340dd7cf6fc91c27eeefce9cca1406c262e93fd6f3a4f3b1e99b01161fcef3';
let newBondId = 'e205a46f6ec6f662cbfad84f4f926973422bf6217d8d2c2eebff94d148fd486d';

const result = await registry.reassociateRecords(oldBondId, newBondId, privateKey);

Tests

yarn test allows to run tests against an external GQL endpoint (of a real Registry server). By default http://localhost:9473/api endpoint is used, but could be changed by DX_REGISTRY_ENDPOINT ENV var.

$ DX_REGISTRY_ENDPOINT=http://xbox.local:9473/api yarn test

yarn test:memory spins up an in-process mock GQL server for the duration of the tests.

$ yarn test:memory

It's also possible to run the mock GQL server standalone and interact with it using the GQL Playground (http://127.0.0.1:4000/).

$ yarn start:memory --help
Options:
  --help     Show help                                                 [boolean]
  --version  Show version number                                       [boolean]
  --host     GQL server host                     [string] [default: "127.0.0.1"]
  --port     GQL server port                            [number] [default: 4000]

$ yarn start:memory
yarn run v1.17.3
$ BABEL_DISABLE_CACHE=1 DEBUG=test babel-node src/mock/main.js
Mock server running on http://127.0.0.1:4000/