1.0.7 • Published 2 years ago

@role2/chain v1.0.7

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

Role2 blockchain SDK for JavaScript

Online block browser: role2.com/chain

Usage

npm i @role2/chain

JavaScript/TypeScript

import { read, verify, serialize /*...*/ } from "@role2/chain";

CLI

read - List blocks from public chain

# chain read [page]
$ chain read
{
  "list": [
    {
      "content": "...",
      "type": "post",
      "format": "json",
      "hash": "feba44c4e068f1f96af9fafb8c040196759ed1c955e0062fcecfc4a307f57c27",
      "parent": null,
      "prev": "a3a142d3659ed9ca37d5230e668f97b6cb24d45f71a5a4cd45fe38f2703b80f3",
      "ts": 1646953669070
    }
  ]
}

verify - Verify a block with a given hash string

$ chain verify '{"ts":1634634273184,"content":"a","format":"text"}' dd69cc70c38242cfca1ddcb67d2d19fe3cfc3bd1868b3dbb176dfcfde8909e8b

hash - Calculate hash from a serialized block

$ chain hash '{"ts":1634634273184,"content":"a","format":"text"}'
dd69cc70c38242cfca1ddcb67d2d19fe3cfc3bd1868b3dbb176dfcfde8909e8b

API

read - List blocks from public chain

await read();
/* Returns:
 {
   list: [
     {
       prev: 'a3a142d3659ed9ca37d5230e668f97b6cb24d45f71a5a4cd45fe38f2703b80f3',
       parent: null,
       ts: 1646953669070,
       tid: '6058',
       type: 'post',
       format: 'json',
       content: '{"content":{"text":"jojo-makes-happy\\nhttps://www.bilibili.com/bangumi/play/ep434543?spm_id_from=333.337.search-card.all.click\\n\\n\\n\\n\\n"},"id":6058,"owner_id":44,"reply_id":null,"type":"activity"}'
     }
   ],
   count: 9
 }
*/

verify - Verify a block with a given hash string

Verify a simple block

// Block data
const block: T_block = {
  ts: 1634634273184,
  content: "a",
  format: N_block_format.text,
};
// Block hash
const hash = "dd69cc70c38242cfca1ddcb67d2d19fe3cfc3bd1868b3dbb176dfcfde8909e8b";
verify(
  block,
  "dd69cc70c38242cfca1ddcb67d2d19fe3cfc3bd1868b3dbb176dfcfde8909e8b"
); // true
verify(block, "invalid_hash"); // false

Verify Role2 chain block

// see: https://www.role2.com/chain/block/17a54ea93574eefba6d030cbee25c97ec718d55852384b0a870547bcd7ff6447
const block: T_block = {
  prev: null,
  parent: null,
  ts: 1645625800722,
  tid: "5569",
  type: "post",
  format: N_block_format.json,
  content:
    '{"content":{"text":"生生不息。"},"id":5569,"owner_id":2,"reply_id":null,"type":"activity"}',
};

verify(
  block,
  "17a54ea93574eefba6d030cbee25c97ec718d55852384b0a870547bcd7ff6447"
); // true
verify(block, "invalid_hash"); // false

serialize - Serialize a block into a stable string

const block: T_block = {
  ts: 1634634273184,
  content: "a",
  format: N_block_format.text,
};
serialize(block); // '{"content":"a","format":"text","ts":1634634273184}'

hash_block - Hash a block object

const block: T_block = {
  ts: 1634634273184,
  content: "a",
  format: N_block_format.text,
};
hash_block(block); // 'dd69cc70c38242cfca1ddcb67d2d19fe3cfc3bd1868b3dbb176dfcfde8909e8b'

hash - Calculate hash from a serialized block

const block: T_block = {
  ts: 1634634273184,
  content: "a",
  format: N_block_format.text,
};
const serialized = serialize(block);
hash(serialized); // 'dd69cc70c38242cfca1ddcb67d2d19fe3cfc3bd1868b3dbb176dfcfde8909e8b'
1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago