ckb-sdk-address
@nervosnetwork/ckb-sdk-address
is the address module of @nervosnetwork/ckb-sdk-core
.
Its current responsibility is just to generate an address object, the algorithm will be introduced in an RFC in the near future.
In a nutshell, the address format is similar to BIP-0173
- this module takes a private key and then calculates the corresponding public key with secp256k1 algorithm;
- use blake160 algorithm to hash the public key and get a blake160-ed string;
- bech32 the blake160-ed string with three options(prefix, type, binIndex) to get a bech32-formatted address.
Detailed methods could be found in @nervosnetwork/ckb-sdk-utils
Address configuration
The meanings of the configuration will be explained in the RFC.
-
Prefix
- 'ckb' for the mainnet
- 'ckt' for the testnet
-
type
- '0x00' for wallet build-in binary_hash ref id
- '0x01' for binary_hash
-
binIndex
- 'P2PH'
Address Value
The address.value
is the address string generated by the private key and address configuration you passed to the constructor.
Address identifier
The address.identifier
represents the identity of the address object. It derives from the public key, and used to generate the human readable address value.
address.identifier
is calculated by blake160(publicKey)
, which picks the first 20 bytes of blake2b(publicKey)
.
And address.identifier
is used to generate address.value
in bech32(address.identifier, ...AddressConfiguration)
.
Installation
Install the package with yarn:
$ yarn add @nervosnetwork/ckb-sdk-address
Or install the package with npm:
$ npm install --save @nervosnetwork/ckb-sdk-address
Usage
const Address = require('@nervosnetwork/ckb-sdk-address').default
const privateKey = 'your private key'
const address = new Address(privateKey, { prefix: 'ckt' })
console.log(address.value)
console.log(address.identifier)