1.0.12 • Published 3 years ago

@mosteast/chain v1.0.12

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

Mosteast general data chain SDK for JavaScript

Usage

npm i @mosteast/chain

JavaScript/TypeScript

import { verify, serialize /*...*/ } from '@mosteast/chain';

CLI

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

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.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago