Giter Site home page Giter Site logo

colorben / coolwallet-sdk Goto Github PK

View Code? Open in Web Editor NEW

This project forked from coolbitx-technology/coolwallet-sdk

0.0 0.0 0.0 18.9 MB

JavaScript SDK to communicate with CoolWallet Pro.

Home Page: https://coolwallet.io/

License: Apache License 2.0

Shell 0.02% JavaScript 4.69% TypeScript 95.13% CSS 0.11% Makefile 0.02% HTML 0.03%

coolwallet-sdk's Introduction

CoolWallet Javascript SDK

JavaScript SDK to communicate with CoolWallet.

This is the monorepo of all the packages you need to build your own app with CoolWallet hardware wallet.

Quick Start

1. Define your transport layer

Depending on your platform, you may can choose different transport object to use in you application.

2. Register and setup hardware wallet.

To register you application with the wallet, take a look at the wallet module in core package. This guide you through the process of registeration and seed generation.

3. Build your Application

Take a look at all the supported modules at Coin Apps. Used the keys generated in the previous step to initiate coin instances, then you can sign transactions, message with different coin instances.

Packages

Transport

To communicate with CoolWallet device, you need to specify a bluetooth transport.

Package Version Description
@coolwallet/transport-web-ble version Web Bluetooth transport
@coolwallet/transport-react-native-ble version React-Native Bluetooth transport

Core

Package Version Description
@coolwallet/core version APDU commands, default encryptions and keypair generation for other SDKs.

Coin Apps

Used to sign transactions of different cryptocurrencies.

Package Version Coin Name
@coolwallet/atom version Cosmos
@coolwallet/bch version Bitcoin Cash
@coolwallet/bnb version Binance
@coolwallet/bsc version Binance Smart Chain
@coolwallet/btc version Bitcoin/USDT
@coolwallet/dot version Polkadot/Kusama
@coolwallet/eth version Ethereum (Ether, ERC20, Smart Contract, EIP-1559 etc.)
@coolwallet/icx version Icon
@coolwallet/ltc version LiteCoin
@coolwallet/trx version Tron
@coolwallet/xlm version Stellar/Kinesis
@coolwallet/xrp version Ripple
@coolwallet/zen version Zen Cash

Examples: Build ETH in web app

To connect to CoolWallet Pro via BLE

npm install @coolwallet/core
npm install @coolwallet/transport-web-ble
import WebBleTransport from "@coolwallet/transport-web-ble";
import * as core from "@coolwallet/core";

Create a connection to obtain the Card Name and SE Public Key.

connect = async () => {
WebBleTransport.listen(async (error, device) => {
    const cardName = device.name;
    const transport = await WebBleTransport.connect(device);
    const SEPublicKey = await core.config.getSEPublicKey(transport)
    this.setState({ transport, cardName, SEPublicKey });
    localStorage.setItem('cardName', cardName)
    localStorage.setItem('SEPublicKey', SEPublicKey)
  });
};

disconnect = () => {
  WebBleTransport.disconnect(this.state.transport.device.id);
  this.setState({ transport: undefined, cardName: "" });
};
  • transport: The object use to communicate with CoolWallet
  • SEPublicKey: The key used to authenticate SE.

Register application with CoolWallet Pro

Obtain app key pairs.

const keyPair = crypto.key.generateKeyPair()
localStorage.setItem('appPublicKey', keyPair.publicKey)
localStorage.setItem('appPrivateKey', keyPair.privateKey)
  • keyPair: The keys use to check your app.

Register card and obtain the appId.

const name = 'your app name'
const SEPublicKey = localStorage.getItem('SEPublicKey')
const appId = await apdu.pair.register(transport, appPublicKey, password, name, SEPublicKey);
  • password: Pairing password for the app to establish the connection with CoolWallet Pro. The password could be supplied by the user (max length: 8).

NOTE: A single CoolWallet Pro could only be paired to 3 apps.

Create / Recover the wallet

Use function setSeed to create or recover your wallet.

const seedHex = bip39.mnemonicToSeedSync(mnemonic).toString('hex');
await apdu.wallet.setSeed(transport, appId, appPrivateKey, seedHex, SEPublicKey)

If you want to create seed by card, you can use function createSeedByCard. And also choose the length of seed(12, 18, 24).

await apdu.wallet.createSeedByCard(transport, appId, appPrivateKey, 12);

Use coin app

npm install @coolwallet/eth
import cwsETH from '@coolwallet/eth'

const ETH = new cwsETH();

Get Address

const address = await ETH.getAddress(
  transport,
  appPrivateKey,
  appId,
  addressIdx
); 

The address generated is compatible to BIP44 with account and change set to 0, which means calling getAddress(i) will get the address of folllowing BIP44 path:

m/44'/60'/0'/0/{i}

In the design of current hardware, we only support path m/44'/60'/0'/0/{i} for speed optimization. This might change in the future and we will then open a more general interface to deal with custom path.

If you have accountPublicKey and accountChainCode, you can use the function ETH.getAddressByAccountKey() to get the address.

const address = await ETH.getAddressByAccountKey(
  accountPublicKey,
  accountChainCode,
  addressIndex
);

Sign Transaction

The signedTx is signed by CoolWallet, which can be sent directly.

const transaction = {
    nonce: "0x21d",
    gasPrice: "0x59682f00",
    gasLimit: "0x5208",
    to: "0x81bb32e4A7e4d0500d11A52F3a5F60c9A6Ef126C",
    value: "0x5af3107a4000",
    data: "0x00",
    chainId: 1
};
const signTxData = {
  transport,
  appPrivateKey,
  appId,
  transaction,
  addressIndex,
};

const signedTx = await ETH.signTransaction(signTxData);

Scripts

  • bootstrap: Initialize monorepo environment with lerna.
  • build: Build all packages.
  • clean: Remove all packages's node_modules.
  • ci: Script for CI.
  • update:lock: Update package-lock.json information.

Contributing

If you're interested to develop new coin for CoolWallet Pro, please see CONTRIBUTING for more information.

coolwallet-sdk's People

Contributors

a0979470582 avatar ain2211 avatar aitarf0921 avatar akuzmichkin avatar antoncoding avatar coolwallet-team avatar cyje66 avatar dannyyaou avatar dependabot[bot] avatar dorac avatar ellingtonsu avatar ericting2000 avatar gundomlegend avatar hank121314 avatar hankliu418 avatar iancoolbitx avatar kanitsu avatar kunmingliu avatar metapolymer avatar sebas-wits avatar skchang0710 avatar sotatek-khanhle avatar sotatek-toantran avatar srt1678 avatar tp26610 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.