1.1.9 • Published 3 months ago

@coolwallet/ada v1.1.9

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
3 months ago

CoolWallet Cardano SDK

Typescript library with support for the integration of Cardano for third party application, include the functionalities of generation of addresses and signed transactions.

Updates

  1. Support combined register and delegate in single transaction.
  2. Support testnet address.

Install

npm install @coolwallet/ada

Usage

import ADA, { TransferWithoutFee, Options } from '@coolwallet/ada';

const ada = new ADA();
const acckey = await ada.getAccountPubKey(transport, appPrivateKey, appId);
const address = ada.getAddressByAccountKey(acckey, addressIndex);

const rawTx = {
  addrIndexes: [0],
  inputs: [{
    txId: '0x8561258e210352fba2ac0488afed67b3427a27ccf1d41ec030c98a8199bc22ec',
    index: 0,
  }],
  output: {
    address: 'addr1qxn5anyxv6dhtl57yvgvpp25emy0pc9wenqzzemxktyr94ahaaap0f0tn4wxaqsydnzty2m0y4gfeu39ckjvsjycs4nssxhc25',
    amount: 10523059,
  },
  change: {
    address: 'addr1q8wyqhxud34ejxjm5tyj74qeuttr7z9vnjuxy6upyn2w8ryau3fvcuaywgncvz89verfyy24vverl9pw2h5uwv30aq9qm6xj7s',
    amount: 360000,
  },
  ttl: '0x641a5',
};

const transferTxSize = await ada.getTransactionSize(rawTx);

const fee = a * transferTxSize + b;
const transaction = {
  fee,
  ...rawTx
};
const options = { transport, appPrivateKey, appId };

const signedTx = await ada.signTransaction(transaction, options);

Methods

getAccountPubKey

Arguments

ArgDescriptionTypeRequired
transportObject to communicate with CoolWallet deviceTransportTrue
appPrivateKeyPrivate key for the connected applicationstringTrue
appIdID for the connected applicationstringTrue

Description

Cardano is using BIP32-Ed25519 and not standard BIP32. Because BIP32-Ed25519 has similar path structure to BIP32, we can still acquire the account public key and use it to generate addresses avoiding exposuring the private keys. Read CIP-1852 to understand the key deriving.

m / 1852' / 1815' / 0'

getAddressByAccountKey

Arguments

ArgDescriptionTypeRequired
acckeyThe account key from getAccountPubKeystringTrue
addressIndexThe address indexnumberTrue

Description

The Cardano address format is combined with different part. Payment part indicates the ownership of the funds associated with the address. Delegation part indicates the owner of the stake rights associated with the address. Read CIP-0019 to understand the address encoding.

Payment key: m / 1852' / 1815' / 0' / 0 / 0
Staking key: m / 1852' / 1815' / 0' / 2 / 0 <- index always be 0

getTransactionSize

Arguments

ArgDescriptionTypeRequired
transactionCardano transaction without feeRawTransactionTrue
txTypeCardano transaction typeTxTypesTrue
export type Integer = string | number;

export enum TxTypes {
  Transfer,
  StakeRegister,
  StakeDelegate,
  StakeDeregister,
  StakeWithdraw,
  StakeRegisterAndDelegate,
}

export interface Input {
  txId: string;
  index: Integer;
}

export interface Output {
  address: string;
  amount: Integer;
}

export interface RawTransaction {
  addrIndexes: number[];
  inputs: Input[];
  change?: Output;
  ttl: Integer;
  output?: Output;
  poolKeyHash?: string;
  withdrawAmount?: Integer;
}

Description

Get Cardano Transaction size for ADA transfer. This method is for fee calculating. The a and b parameters can be fetched via Latest epoch protocol parameters API.

Fee = a * size(tx) + b

References:

signTransaction

Arguments

ArgDescriptionTypeRequired
transactionEssential information/property for Cardano TransactionTransferTrue
optionsArguments for coolwallet communication and authenticationOptionsTrue
txTypeCardano transaction typeTxTypesTrue
export interface Options {
  transport: Transport;
  appPrivateKey: string;
  appId: string;
  confirmCB?: Function;
  authorizedCB?: Function;
}

export interface Transaction extends RawTransaction {
  fee: Integer;
}

Description

Sign Cardano Transaction for ADA transfer.

Error Messages

Submit tx error messagesDescription
ValueNotConservedUTxOoutputs and fee is not equal to utxos
BadInputsUTxOutxo not found
FeeTooSmallUTxOfee small than a * size + b
OutsideValidityIntervalUTxOttl small than latest Slot
OutputTooSmallUTxOoutput amount less than 999978
MissingVKeyWitnessesUTXOWsignature verifying failure
DelegsFailure沒有 Reward,但嘗試用 Withdraw 取值

Staking

Get account info by stake addresss

https://docs.blockfrost.io/#tag/Cardano-Accounts

{
  "stake_address": "stake1ux3g2c9dx2nhhehyrezyxpkstartcqmu9hk63qgfkccw5rqttygt7",
  "active": true,
  "active_epoch": 412,
  "controlled_amount": "619154618165",
  "rewards_sum": "319154618165",
  "withdrawals_sum": "12125369253",
  "reserves_sum": "319154618165",
  "treasury_sum": "12000000",
  "withdrawable_amount": "319154618165",
  "pool_id": "pool1pu5jlj4q9w9jlxeu370a3c9myx47md5j5m2str0naunn2q3lkdy"
}

State Diagram

stateDiagram-v2
    get_account_info --> active=false
    get_account_info --> pool_id
    get_account_info --> withdrawable_amount
    active=false --> active=true : Register
    active=true --> active=false : Deregister

    pool_id --> null
    pool_id --> has_value
    null --> new_pool_id : Delegate
    has_value --> new_pool_id : Delegate

    withdrawable_amount --> has_amount
    has_amount --> remaining_withdrawable_amount : Withdraw

Tx Interfaces Requirement

Transfer

interface RawTransaction {
  addrIndexes: number[];
  inputs: Input[];
  change?: Output;
  ttl: Integer;
  output: Output;
}

Stake Register

interface RawTransaction {
  addrIndexes: number[];
  inputs: Input[];
  change?: Output;
  ttl: Integer;
}

Stake Delegate

interface RawTransaction {
  addrIndexes: number[];
  inputs: Input[];
  change?: Output;
  ttl: Integer;
  poolKeyHash: string;
}

Stake Deregister

interface RawTransaction {
  addrIndexes: number[];
  inputs: Input[];
  change?: Output;
  ttl: Integer;
}

Stake Withdraw

interface RawTransaction {
  addrIndexes: number[];
  inputs: Input[];
  change: Output;
  ttl: Integer;
  withdrawAmount: Integer;
}
1.1.9-beta.0

3 months ago

1.1.9

3 months ago

1.1.8

12 months ago

1.1.7

1 year ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.1

2 years ago