Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
build/
docs/
node_modules/
lib/
proto/
scripts/
*.d.ts
22 changes: 15 additions & 7 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ jobs:
strategy:
matrix:
node-version: [12.x]
redis-version: [5]

env:
CLOUDSDK_CORE_PROJECT: dev

steps:
- name: Checkout code
Expand All @@ -41,12 +37,24 @@ jobs:
- name: Run build lib, types and docs
run: yarn build
- name: Copy LICENSE
run: cp LICENSE lib/LICENSE
run: cp LICENSE build/LICENSE
- name: Copy README
run: cp README.md lib/README.md
run: cp README.md build/README.md
- name: Copy sanitized package.json
run: cp package.json lib/package.json
run: cp package.json build/package.json

- name: Publish package to NPM
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}

- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
draft: false
prerelease: false
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ jobs:
strategy:
matrix:
node-version: [10.x, 12.x, 14.x, 15.x]
redis-version: [4, 5, 6]

env:
CLOUDSDK_CORE_PROJECT: dev
Expand Down
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,7 @@ dist
.tern-port

# Build output
/lib
/typings
/build

# Proto files
/proto
5 changes: 5 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.github/workflows
build
docs
node_modules
proto
5 changes: 0 additions & 5 deletions DOCUMENTATION.md

This file was deleted.

68 changes: 51 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,75 @@

This Javascript SDK enables browsers and NodeJS clients to interact with the Lum Network.

## SDK Usage

### Node version

The library is tested using **NodeJS 10.x, 12.x, 14.x, 15.x**.

It should also work in all recent browsers.

### Installation

```bash
yarn add @lum-network/sdk-javascript
```

### Documentation

The SDK code should be documented enough for developers to explore and use it easily. Therefore the documentation might not cover all the capabilities of the SDK. Feel free to contribute if you wish to improve the code documentation and/or the provided samples.

The [Documentation](./docs/README.md) contains:

- Installation instructions
- Basic usage
- Code samples
- Code auto-generated documentation

## SDK Features

This SDK basically provides an easy access to all the available Lum Network blockchain RPCs as well as the payload generation and the cryptographic features to properly consume those RPCs.
This SDK provides an easy access to all the available Lum Network blockchain RPCs as well as the payload generation and the cryptographic features to properly consume those RPCs.

**Most commonly used features:**

- Core cryptographic tools and functions
- Core cryptographic tools:
- Seed, private key and encrypted mnemonic generation
- Private and public keys management
- Transaction payload generation
- Transaction signature and verification
- Wallets:
- Unlock wallets from private keys, keystore and mnemonic
- Sign transaction using unlocked wallets
- Client service:
- Connection to a blockchain node (http and socket mode)
- Commonly used Tendermint and Cosmos RPCs
- All Lum Network dedicated RPCs
- Transaction broadcast
- Blockchain RPCs
- Accounts and wallets:
- Accounts seed and encrypted mnemonic generation
- Unlock accounts from private keys, keystore and mnemonic
- Wallet balance
- Transactions
- Payload generation
- Signature
- Messages & Types:
- Cosmos & Lum messages payload building
- Typescript implementation of RPCs requests and responses
- Other utils:
- Encoding data from/to: Uint8Array, base64 and hex
- Build Transaction search queries
- Log & event parsing

## Documentation
## Code structure

The SDK code should be documented enough for developers to explore and use it easily. Therefore the documentation might not cover all the capabilities of the SDK. Feel free to contribute if you wish to improve the code documentation and/or the provided samples.
The SDK is based on the [CosmJS](https://github.com/cosmos/cosmjs) implementation and heavily relies on it.

The [Documentation](./DOCUMENTATION.md) contains:
It is intented to be used standalone, without having to import specific CosmJS packages which can be tricky and messy.

- Installation instructions
- Basic usage
- Code samples
Therefore all codecs, types, functions are features from the CosmJS SDK are either re-implemented by this SDK or re-exporter for simplicity purposes.

Directly importing the CosmJS SDK or other cryptographic library should be considered bad practice for most use cases.

Do not hesitate to contribute to this repository. This SDK is intended to be a one-stop-shop for all Lum Network javascript implementations and should definitely be improved over time by all its users.

## Contributing

Contributions are most welcome.

Please test your changes with a local client and add unit tests coverage for your code before submission.

## Special notes

Thanks to the Binance team for their work on the Binance Javascript SDK which inspired this project structure and development.
121 changes: 121 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# Documentation

The code should be documented enough to make this library easy to use for anyone familiar with blockchain technology and especially the Tendermint engine and the Cosmos SDK.

You can find more details by browsing the [code documentation](./lib).

## Examples

A couple examples to help you get started.

### Imports

```typescript
import {
LumWallet,
LumClient,
LumTypes,
LumUtils,
LumConstants,
LumMessages,
} from '@lum-network/sdk-javascript'
```

### Create a wallet

#### Mnemonic
```typescript
// Create a new cryptographically secure random mnemonic
const mnemonic = LumUtils.generateMnemonic(12);

// Create a wallet instance based on this fresh mnemonic
const wallet = await LumWallet.fromMnemonic(mnemonic);
```

#### Private key
```typescript
// Create a new cryptographically secure random private key
const privateKey = LumUtils.generatePrivateKey();

// Create a wallet instance based on this fresh private key
const wallet = await LumWallet.fromPrivateKey(mnemonic);
console.log(`Wallet address: ${wallet.address}`);

// Create a wallet instance based on an hexadecimal private key (ex: user input - 0x is optional)
const hexPrivateKey = '0xb8e62c34928025cdd3aef6cbebc68694b5ad9209b2aff6d3891c8e61d22d3a3b';
const existingWallet = await LumWallet.fromPrivateKey(LumUtils.keyFromHex(hexPrivateKey));
console.log(`Existing wallet address: ${wallet.address}`);
```

#### Keystore
```typescript
// Create a random private key for the sake of this example
const privateKey = LumUtils.generatePrivateKey();
// Create a keystore (or consume user input)
const keystore = LumUtils.generateKeyStore(privateKey, 'some-password');
const wallet = await LumWallet.fromKeyStore(keystore, 'some-password');
console.log(`Wallet address: ${wallet.address}`);
```

### Connect to the testnet

```typescript
const testnetClient = await LumClient.connect('http://localhost:26657');
```

### Account information & balance

#### Get account information
```typescript
const account = await testnetClient.getAccount(wallet.address);
if (account === null) {
console.log('Account: not found');
} else {
console.log(`Account: ${account.address}, ${account.accountNumber}, ${account.sequence}`);
}
```

#### Get account balances
```typescript
const balances = await testnetClient.getBalancesUnverified(wallet.address);
if (balances.length === 0) {
console.log('Balances: empty account');
} else {
console.log(
`Balances: ${balances.map((coin) => {
coin.denom + ': ' + coin.amount;
})}`,
);
}
```

### Transactions

#### Get account transactions (sent and received)
```typescript
// The client search feature supports multiple searches and merge+store the results
const transactions = await testnetClient.searchTx([
LumUtils.searchTxFrom(wallet.address),
LumUtils.searchTxTo(wallet.address),
]);
console.log(`Transactions: ${transactions.map((tx) => tx.hash).join(', ')}`);
```

#### Send transaction
```typescript
// Build transaction message (Send 100 LUM)
const sendMsg = LumMessages.BuildMsgSend(
wallet.address,
toAddress,
[{ denom: LumConstants.LumDenom, amount: '100' }],
);
// Define fees (1 LUM)
const fee = {
amount: [{ denom: LumConstants.LumDenom, amount: '1' }],
gas: '100000',
};
// Sign and broadcast the transaction using the client
const broadcastResult = await clt.signAndBroadcastTx(w1, [sendMsg], fee, 'hello memo!');
// Verify the transaction was succesfully broadcasted and made it into a block
console.log(`Broadcast success: ${LumUtils.broadcastTxCommitSuccess(broadcastResult)}`);
```
1 change: 1 addition & 0 deletions docs/lib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @lum-network/sdk-javascript
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
testMatch: ['**/*test.(ts|js)'],
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/lib/', '<rootDir>/typings'],
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/build/', '<rootDir>/typings'],
transform: {
'^.+\\.(ts|js)?$': 'ts-jest',
},
Expand All @@ -12,4 +12,5 @@ module.exports = {
diagnostics: false,
},
},
testTimeout: 10000,
};
38 changes: 28 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,39 @@
"type": "git",
"url": "https://github.com/lum-network/sdk-javascript.git"
},
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"main": "build/index.js",
"typings": "build/index.d.ts",
"files": [
"lib/*"
"build/*"
],
"scripts": {
"test": "cross-env NODE_ENV=test jest",
"build": "yarn clean && yarn build:types && yarn build:js && yarn build:docs",
"build:types": "tsc --emitDeclarationOnly",
"build:js": "babel src --out-dir lib --extensions \".ts,.tsx\" --source-maps inline",
"build:docs": "rimraf docs/lib && typedoc --excludeExternals --excludeNotDocumented --readme none --out docs/lib --hideBreadcrumbs --disableSources",
"clean": "rimraf lib",
"build": "yarn clean && yarn build:lib && yarn build:docs",
"build:lib": "tsc",
"build:docs": "rimraf docs/lib && typedoc --excludeExternals --readme none --out docs/lib --hideBreadcrumbs --disableSources",
"clean": "rimraf build",
"lint": "eslint '**/*.{ts,js}'",
"format": "prettier --write '**/*.{js,jsx,ts,tsx,css,json,md,html,yml}'",
"bump": "npm version"
"bump": "npm version",
"preget-proto": "rm -rf proto",
"get-proto": "REF=v0.40.0 sh ./scripts/get-proto.sh",
"define-proto": "sh ./scripts/define-proto.sh",
"postdefine-proto": "prettier --write \"src/codec/**/*.ts\""
},
"dependencies": {
"@cosmjs/crypto": "^0.24.0-alpha.25",
"@cosmjs/encoding": "^0.24.0-alpha.25",
"@cosmjs/math": "^0.24.0-alpha.25",
"@cosmjs/proto-signing": "^0.24.0-alpha.25",
"@cosmjs/stargate": "^0.24.0-alpha.25",
"@cosmjs/tendermint-rpc": "^0.24.0-alpha.25",
"@cosmjs/utils": "^0.23.2",
"@types/uuid": "^8.3.0",
"crypto-browserify": "^3.12.0",
"crypto-js": "^4.0.0",
"long": "^4.0.0",
"uuid": "^8.3.2"
},
"dependencies": {},
"optionalDependencies": {},
"devDependencies": {
"@babel/cli": "^7.8.3",
Expand All @@ -39,6 +55,7 @@
"@babel/plugin-transform-runtime": "^7.12.10",
"@babel/preset-env": "^7.8.3",
"@babel/preset-typescript": "^7.8.3",
"@types/crypto-js": "^4.0.1",
"@types/jest": "^26.0.20",
"cross-env": "^7.0.3",
"eslint": "^7.19.0",
Expand All @@ -47,6 +64,7 @@
"prettier": "^2.2.1",
"rimraf": "^3.0.2",
"ts-jest": "^26.5.0",
"ts-proto": "^1.67.0",
"typedoc": "^0.20.25",
"typedoc-plugin-markdown": "^3.5.0",
"typescript": "^4.1.3"
Expand Down
Loading