Skip to content

Commit 8bdac02

Browse files
authored
Merge pull request #69 from virtualeconomy/develop
release: v0.2.1
2 parents 45c3df6 + 2ad7765 commit 8bdac02

27 files changed

+1031
-833
lines changed

README.md

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![License](https://img.shields.io/badge/License-BSD_4--Clause-green.svg)](./LICENSE)
44

5-
> ***Under active maintenance. Contributions are always welcome!***
5+
> **_Under active maintenance. Contributions are always welcome!_**
66
77
The official Javascript SDK for VSYS APIs. The [old Javascript SDK](https://github.com/virtualeconomy/js-v-sdk) is deprecated and will be archived soon.
88

@@ -20,9 +20,11 @@ The official Javascript SDK for VSYS APIs. The [old Javascript SDK](https://gith
2020
## Installation
2121

2222
Install from Github
23+
2324
```bash
2425
git clone [email protected]:virtualeconomy/js-vsys.git
2526
```
27+
2628
Then locate to the folder and run `npm install`.
2729

2830
## Quick Example
@@ -36,70 +38,67 @@ import * as jv from '@virtualeconomy/js-vsys';
3638
const host = 'http://veldidina.vos.systems:9928';
3739

3840
// A test net wallet seed
39-
const seed = new jv.Seed(
40-
'your seed'
41-
);
42-
43-
function printHeading(msg){
44-
function times(char, num){
45-
let ret = "";
46-
while(num-->0) ret += char;
47-
48-
return ret;
49-
}
50-
console.log(times("=", 10), msg, times("=", 10))
41+
const seed = new jv.Seed('your seed');
42+
43+
function printHeading(msg) {
44+
function times(char, num) {
45+
let ret = '';
46+
while (num-- > 0) ret += char;
47+
48+
return ret;
49+
}
50+
console.log(times('=', 10), msg, times('=', 10));
5151
}
5252

53-
printHeading("Try out NodeAPI");
53+
printHeading('Try out NodeAPI');
5454
// NodeAPI is the wrapper for RESTful APIs
5555
const api = jv.NodeAPI.new(host);
5656
// GET /blocks/last
5757
console.log(await api.blocks.getHeight());
5858
// GET /node/version
5959
console.log(await api.node.getVersion());
6060

61-
printHeading("Try out Chain");
61+
printHeading('Try out Chain');
6262
// Chain represents the chain itself
6363
const ch = new jv.Chain(api, jv.ChainID.TEST_NET);
6464
// Get chain's height
65-
console.log("Height:", await ch.getHeight());
65+
console.log('Height:', await ch.getHeight());
6666
// Get chain's last block
67-
console.log("Last block: \n", await ch.getLastBlock());
67+
console.log('Last block: \n', await ch.getLastBlock());
6868

69-
printHeading("Try out Account")
69+
printHeading('Try out Account');
7070
// Account represents an account in the net
7171
const wal = new jv.Wallet(seed);
7272
const acnt = wal.getAcnt(ch, 0);
7373
// Get the account's balance
74-
console.log("Balance:", await acnt.getBal());
75-
// Get the account's nonce'
76-
console.log("Nonce:", acnt.nonce);
74+
console.log('Balance:', await acnt.getBal());
7775
// Get the account's public key
78-
console.log("Public key:", acnt.keyPair.pub);
76+
console.log('Public key:', acnt.keyPair.pub);
7977
// Get the account's private key
80-
console.log("Private key:", acnt.keyPair.pri);
78+
console.log('Private key:', acnt.keyPair.pri);
8179
// Get the account's address
82-
console.log("Account address:", acnt.addr);
80+
console.log('Account address:', acnt.addr);
8381

84-
printHeading("Try out Smart Contract");
85-
const ctrtId = "CF3cK7TJFfw1AcPk74osKyGeGxee6u5VNXD";
82+
printHeading('Try out Smart Contract');
83+
const ctrtId = 'CF3cK7TJFfw1AcPk74osKyGeGxee6u5VNXD';
8684
const ctrt = new jv.NFTCtrt(ctrtId, ch);
8785
// Get the contract's maker
88-
console.log("Maker:", await ctrt.getMaker());
86+
console.log('Maker:', await ctrt.getMaker());
8987
// Get the contract's issuer
90-
console.log("Issuer:", await ctrt.getIssuer());
88+
console.log('Issuer:', await ctrt.getIssuer());
9189
// Get the contract's ID
92-
console.log("Contract id:", ctrt.ctrtId);
90+
console.log('Contract id:', ctrt.ctrtId);
9391
```
9492

9593
Example output
94+
9695
```
9796
========== Try out NodeAPI ==========
9897
{ height: 2767707 }
9998
{ version: 'VSYS Core v0.4.1' }
10099
========== Try out Chain ==========
101100
Height: 2767707
102-
Last block:
101+
Last block:
103102
{
104103
version: 1,
105104
timestamp: 1655457048010768100,
@@ -147,14 +146,17 @@ Contract id: CtrtID { data: 'CF3cK7TJFfw1AcPk74osKyGeGxee6u5VNXD' }
147146
## Docs
148147

149148
### Account & Wallet
149+
150150
- [Account](./doc/account.md)
151151
- [Wallet](./doc/wallet.md)
152152

153153
### Chain & API
154+
154155
- [Chain](./doc/chain.md)
155156
- [Api](./doc/api.md)
156157

157158
### Smart Contracts
159+
158160
- [NFT Contract V1](./doc/smart_contract/nft_ctrt.md)
159161
- [NFT Contract V2](./doc/smart_contract/nft_ctrt_v2.md)
160162
- [Token Contract V1 without split](./doc/smart_contract/tok_ctrt_no_split.md)
@@ -172,30 +174,37 @@ Contract id: CtrtID { data: 'CF3cK7TJFfw1AcPk74osKyGeGxee6u5VNXD' }
172174
## Run Tests
173175

174176
### Specification Tests
177+
175178
Specification tests are scripts that simulate the behaviour of a normal user to interact wtih `js_vsys`(e.g. register a smart contract & call functions of it).
176179

177180
To run it, ensure that you have `jasmine` properly installed.
178181

179182
First set up the global variables like below.
183+
180184
```bash
181185
export JS_VSYS_HOST='http://veldidina.vos.systems:9928'
182186
export JS_VSYS_AVG_BLOCK_DELAY=6
183187
export JS_VSYS_SEED='your_seed'
184188
```
189+
185190
Then go to the root of the project and run (take NFT contract as an example).
191+
186192
```bash
187193
npx jasmine './spec/contract_spec/nft_ctrt_spec.js'
188194
```
195+
189196
The above command will test each aspect(e.g. function `send` of NFT contract) individually and have required resources set up before testing(e.g. register a new contract, issue a token, etc). It's good for testing a specific aspect while it might consume too much resources to test every aspect in this way.
190197

191198
Take NFT contract as an example, it will register a contract first and then execute functions like `send`, `transfer`, `deposit`, etc in a pre-orchestrated manner so that some common set up(e.g. register a contract) will be done only once.
192199

193200
To run a single test, say `issue`, just comment all parts except `beforeAll` and `test method issue`, run
201+
194202
```bash
195203
npx jasmine './spec/contract_spec/nft_ctrt_spec.js'
196204
```
205+
197206
## Contributing
198207

199208
**Contributions are always welcome!**
200209

201-
See [the development documentation](./doc/dev.md) for more details and please adhere to conventions mentioned in it.
210+
See [the development documentation](./doc/dev.md) for more details and please adhere to conventions mentioned in it.

doc/account.md

Lines changed: 32 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
- [Account](#account)
44
- [Introduction](#introduction)
5-
- [Usage with Javascript SDK](#usage-with-javascript-sdk)
5+
- [Usage with JavaScript SDK](#usage-with-javascript-sdk)
6+
- [Create Account](#create-account)
7+
- [From Wallet](#from-wallet)
8+
- [From Private Key & Public Key](#from-private-key--public-key)
69
- [Properties](#properties)
710
- [Chain](#chain)
811
- [Api](#api)
9-
- [Wallet](#wallet)
10-
- [Nonce](#nonce)
11-
- [Account Seed Hash](#account-seed-hash)
1212
- [Key Pair](#key-pair)
1313
- [Address](#address)
1414
- [VSYS Balance](#vsys-balance)
@@ -33,9 +33,35 @@ There are 2 kinds of accounts:
3333

3434
The key difference between them lies in whether they have a private key.
3535

36-
## Usage with Javascript SDK
36+
## Usage with JavaScript SDK
3737

38-
In JS SDK we have an `Account` module that represents a user account on the VSYS blockchain.
38+
In JS SDK we have an `Account` class that represents a user account on the VSYS blockchain.
39+
40+
### Create Account
41+
42+
#### From Wallet
43+
The `Account` object can be contructed by a `Wallet` object given the `Chain` object & nonce.
44+
45+
```javascript
46+
import * as jv from '@virtualeconomy/js-vsys';
47+
// ch // a jv.Chain object
48+
// wal // a jv.Wallet object
49+
const acnt0 = wal.getAcnt(ch, 0); // get the account of nonce 0 of the wallet.
50+
```
51+
52+
#### From Private Key & Public Key
53+
The `Account` object can be constructed by a private key & opionally along with a public key.
54+
55+
If the public key is omitted, it will be derived from the private key.
56+
If the public key is provided, it will be verified against the private key.
57+
58+
```javascript
59+
import * as jv from '@virtualeconomy/js-vsys';
60+
// ch // a jv.Chain object
61+
const acnt0 = jv.Account.fromPriKeyStr(ch, 'your_private_key');
62+
const acnt1 = new jv.Account(ch, new jv.PriKey('your_private_key'));
63+
const acnt2 = new jv.Account(ch, new jv.PriKey('your_private_key'), new jv.PubKey('your_public_key'));
64+
```
3965

4066
### Properties
4167

@@ -100,60 +126,6 @@ NodeAPI {
100126
}
101127
```
102128

103-
#### Wallet
104-
105-
The `Wallet` object that represents the wallet that contains this account.
106-
107-
```javascript
108-
// acnt: Account
109-
console.log(acnt.wallet);
110-
```
111-
112-
Example output
113-
114-
```
115-
Wallet {
116-
seed: Seed {
117-
data: 'your_seed'
118-
}
119-
}
120-
```
121-
122-
#### Nonce
123-
124-
The nonce of this account in the wallet.
125-
126-
```javascript
127-
// acnt: Account
128-
console.log(acnt.nonce);
129-
```
130-
131-
Example output
132-
133-
```
134-
Nonce { data: 0 }
135-
```
136-
137-
#### Account Seed Hash
138-
139-
Account Seed Hash is the hashing result of
140-
141-
- the seed of the wallet the account is in
142-
- the nonce of the account that
143-
144-
Account Seed Hash can be used to generate the private/public key pair of the account.
145-
146-
```javascript
147-
// acnt: Account
148-
console.log(acnt.acntSeedHash);
149-
```
150-
151-
Example output
152-
153-
```
154-
<Buffer 44 d2 3e 1f fc 9d 6b 4b 7b 9c a6 fe 23 26 26 61 c5 54 c0 6d a4 62 5b bb d6 e5 44 d1 43 e9 9f>
155-
```
156-
157129
#### Key Pair
158130

159131
The private/public key pair of the account.

0 commit comments

Comments
 (0)