Skip to content

Commit

Permalink
Merge pull request #128 from hangts/master
Browse files Browse the repository at this point in the history
#merge develop into master
  • Loading branch information
kaifei Hu committed Jan 26, 2021
2 parents 1b726a6 + 37fb4cb commit 6e0840e
Show file tree
Hide file tree
Showing 777 changed files with 405,110 additions and 8,656 deletions.
262 changes: 262 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,265 @@
![Banner](https://raw.githubusercontent.com/irisnet/irishub/master/docs/pics/iris.jpg)
[![License](https://img.shields.io/github/license/irisnet/irishub-sdk-js.svg)](https://github.com/irisnet/irishub-sdk-js/blob/master/LICENSE)
[![Version](https://img.shields.io/github/tag/irisnet/irishub-sdk-js.svg)](https://github.com/irisnet/irishub-sdk-js/releases)

The IRISnet JavaScript SDK allows browsers and Node.js clients to interact with IRISnet. Core functionality and examples
are in the `test` folder.

- client - client that implements IRISnet transactions types, such as for transfers and staking,etc.
- crypto - core cryptographic functions.
- accounts - management of accounts and wallets, including seed and encrypted mnemonic generation, recover account by
mnemonic or keystore, etc.

## Installation

Install the package via npm.

```bash
npm install https://github.com/irisnet/irishub-sdk-js.git
```

## Config

```typescript
interface ClientConfig {
node: string,//address of a rpc node on IRISnet
network: number, //[Number] 0: Mainnet, 1: Testnet
chainId: string,
gas?: string,
fee?: {
denom: string;
amount: string;
},//default fee for transactions
keyDAO: KeyDAO,//key manager for wallet, which must be implemented
bech32Prefix?: {
AccAddr: string,
AccPub: string,
ValAddr: string,
ValPub: string,
ConsAddr: string,
ConsPub: string,
},
rpcConfig?: AxiosRequestConfig// axios request config
}
```

## Client Setup

First you should implement your KeyDAO like follows:

```typescript
class KeyDAO {
/**
* save private key to client
* @param name for account you generated, which will be use to query private key and other account info
* @param wallet: {address: string,privateKey: string,publicKey: string,mnemonic: string}
* @throws error if save failed
*/
write(name, wallet):void {
localStorage.setItem(name, JSON.stringify(wallet));
}

/**
* save walet in client
* @param name
* @throws error if read failed
*/
read(name):wallet{
const wallet = localStorage.getItem(name);
if(wallet) {
return JSON.parse(wallet);
}
throw new Error(`no wallet was found`)
}

/**
* encrypt your private key before save to client
* @param private key
* @param password for encrypt private key
*/
encrypt?(privateKey, password): string {
return CryptoJS.AES.encrypt(msg, password).toString();
}

/**
* decrypto your private key with password
* @param encrypted private key
* @param password that can decrypto private key ecrypted
*/
decrypt?(encryptedPrivateKey, password): string {
const bytes = CryptoJS.AES.decrypt(encryptedPrivateKey, password);
const privateKey = bytes.toString(CryptoJS.enc.Utf8);
if (privateKey) {
return privateKey
} else {
throw new Error('can not decrypto the private key');
}

}

}

```

```typescript
import {newClient as irisSdkClient} from 'irishub-sdk-js';

const client = irisSdkClient(ClientConfig)
.withKeyDAO(new KeyDAO())
.withRpcConfig({timeout: 15000});

```
## Client Usage
The following selected examples demonstrate basic client usage.

- create account
```typescript
const account: { address: string, mnemonic: string } = client.keys.add(`iris_wallet`, 'S8js8Ka82lqAc');
```
- recover account by mnemonic
```typescript
const account: string = client.keys.recover(`iris_wallet`, 'S8js8Ka82lqAc', `fatigue panther innocent dress person fluid animal raven material embark target spread kiss smile cycle begin rocket pull couple mass story analyst guilt network`);
```
- recover account by keystore
```typescript
const account: string = client.keys.recover(`iris_wallet`, 'S8js8Ka82lqAc', `{"version":"1","id":"1d295464-aaa8-418e-b374-3052a91dc26a","address":"faa1eqvkfthtrr93g4p9qspp54w6dtjtrn279vcmpn","crypto":{"ciphertext":"a6ee40e3b38a7b24a373ec006bcc039ccbae45dc3b1f314405ab51ee975d6b1f","cipherparams":{"iv":"453b83b1331d334b70d160616fe43ace"},"cipher":"aes-128-ctr","kdf":"pbkdf2","kdfparams":{"dklen":32,"salt":"e702e41edf7277a39f7f5cc641c19e1b492cc29bf737aec9b53b496c9f217b37","c":10000,"prf":"hmac-sha256"},"mac":"6e8ed2619f0b30f00c20f9f01858368efbd0feae5811792d8b41a60c2d71d310"}}`);
```
- transfer example
```typescript
const res = await client.bank.send({
to:`iaa1eqvkfthtrr93g4p9qspp54w6dtjtrn27ar7rpw`,
amount:[{
denom:`uiris`,
amount:`1000000`
}],
baseTx:{
from:`iris_wallet`,
password:`S8js8Ka82lqAc`,
gas:50000,
fee:{
denom:`uiris`,
amount:`500000`
}
}
})
```
### bank `src/modules/bank.ts`
- send
- multiSend
- queryBalance
- queryAllBalances
- queryTotalSupply
- querySupplyOf
- queryParams
### auth `src/modules/auth.ts`
- newStdTx
- queryAccount
- queryParams
### distribution `src/modules/distribution.ts`
- setWithdrawAddr
- withdrawRewards
- withdrawValidatorCommission
- fundCommunityPool
- queryParams
- queryValidatorOutstandingRewards
- queryValidatorCommission
- queryValidatorSlashes
- queryDelegationRewards
- queryDelegationTotalRewards
- queryDelegatorValidators
- queryDelegatorWithdrawAddress
- queryCommunityPool
### gov `src/modules/gov.ts`
- queryProposal
- queryProposals
- queryVote
- queryVotes
- queryDeposit
- queryDeposits
- queryTally
- submitParameterChangeProposal
- submitPlainTextProposal
- submitCommunityTaxUsageProposal
- deposit
- vote
### keys `src/modules/keys.ts`
- add
- recover
- import
- importPrivateKey
- export
- delete
- show
### nft `src/modules/nft.ts`
- issueDenom
- mintNft
- editNft
- transferNft
- burnNft
- querySupply
- queryOwner
- queryCollection
- queryDenom
- queryDenoms
- queryNFT
### slashing `src/modules/slashing.ts`
- queryParams
- querySigningInfo
- unjail
### staking `src/modules/staking.ts`
- delegate
- undelegate
- redelegate
- queryDelegation
- queryDelegations
- queryUnbondingDelegation
- queryDelegatorUnbondingDelegations
- queryRedelegation
- queryDelegatorValidators
- queryDelegatorValidator
- queryHistoricalInfo
- queryValidatorDelegations
- queryValidatorUnbondingDelegations
- queryValidator
- queryValidators
- queryPool
- queryParams
- appendZero
- createValidator
### tendermint `src/modules/tendermint.ts`
- queryBlock
- queryBlockResult
- queryTx
- queryValidators
- searchTxs
- queryNetInfo
### token `src/modules/token.ts`
- issueToken
- editToken
- mintToken
- transferTokenOwner
- queryTokens
- queryToken
- queryFees
- queryParameters
### tx `src/modules/tx.ts`
- buildTx
- newStdTxFromProtoTxModel
- buildAndSend
- broadcast
- sign
- sign_signDoc
- broadcastTxAsync
- broadcastTxSync
- broadcastTxCommit
- broadcastTx
- newTxResult
- createMsg
### protobuf `src/modules/protobuf.ts`
- deserializeTx
- unpackMsg
- deserializeSignDoc
- deserializeTxRaw
- deserializeSigningInfo
- deserializePubkey

19 changes: 19 additions & 0 deletions build/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"env": {
"commonjs": true,
"es6": true,
"node": true
},
"extends": [
"../.eslintrc.json",
"plugin:node/recommended"
],
"plugins": [ "node" ],
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
"node/no-unpublished-require": "off",
"@typescript-eslint/no-var-requires": "off"
}
}
1 change: 1 addition & 0 deletions build/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./webpack.config');
54 changes: 54 additions & 0 deletions build/webpack.common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const path = require('path');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');

const root = path.resolve(`${__dirname}/..`);
const srcRoot = `${root}/src`;
const production = (process.env.NODE_ENV === 'production');

module.exports = {
entry: `${srcRoot}/index.ts`,
output: {
path: `${root}/dist`,
filename: 'index.js',
library: 'irishub-sdk',
libraryTarget: 'umd'
},
resolve: {
extensions: ['.ts', '.js']
},
module: {
rules: [{
enforce: 'pre',
test: /\.ts$/,
include: [srcRoot],
use: [{
loader: 'eslint-loader',
options: {
failOnWarning: production
}
}]
}, {
test: /\.ts$/,
include: [srcRoot],
use: [
{
loader: 'babel-loader',
options: {
compact: false,
presets: [
'@babel/preset-typescript',
['@babel/preset-env', {
targets: {
chrome: '73',
ie: '11',
firefox: '66',
safari: '12'
}
}]
]
}
}
]
}]
}
};
13 changes: 13 additions & 0 deletions build/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const merge = require('webpack-merge');
const common = require('./webpack.common');
const web = require('./webpack.web');
const node = require('./webpack.node');

const env = (process.env.NODE_ENV === 'production')
? require('./webpack.production')
: require('./webpack.development');

module.exports = [
merge(common, web, env),
merge(common, node, env)
];
4 changes: 4 additions & 0 deletions build/webpack.development.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
mode: 'development',
devtool: 'source-map'
};
6 changes: 6 additions & 0 deletions build/webpack.node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
target: 'node',
output: {
filename: 'node.js'
}
};
4 changes: 4 additions & 0 deletions build/webpack.production.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
mode: 'production',
devtool: false
};
6 changes: 6 additions & 0 deletions build/webpack.web.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
target: 'web',
output: {
filename: 'web.js'
}
};
Loading

0 comments on commit 6e0840e

Please sign in to comment.