@sec-block/secjs-util

1.1.22 • Public • Published


SecUtils

JavaScript Style Guide

Definition: A collection of utility functions for secblock. It can be used in node.js or can be in the browser with browserify. -->

Kind: global class


Install

npm install @sec-block/secjs-util --save 

Usage

class SecUtils {
    ...
    func1()
    func2()
    ...
}
const util = new SecUtils() 
util.func1()           

currentUnixTimeSecond

A utility function of getting a Unix timestamp in second.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.currentUnixTimeSecond()

currentUnixTimeInMillisecond

A utility function of getting a Unix timestamp in millisecond.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.currentUnixTimeInMillisecond()

getDatetime

A utility function of converting a standard GMT time to a Unix timestamp.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.getDatetime()

getUnixtime

A utility function of converting a Unix timestamp to a standard GMT time.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.getUnixtime()

asyncGetUTCTimeFromServer

A utility function of get utc time from ntp server.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.asyncGetUTCTimeFromeServer().then(callback).catch(err)

refreshTimeDifference

A utility function to refresh the time difference between local host and ntp server.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.refreshTimeDifference( (err, timeDiff) => {
    if (err) {
        console.log(err)
    }
    console.log(timeDiff)
})

hasha256(data)

A small function created as there is a lot of sha256 hashing.

Kind: instance method of secUtil

Param Type Description
data Buffer creat sha256 hash buffer

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.hasha256(data)

generatePrivateKey()

0x00 P2PKH Mainnet, 0x6f P2PKH Testnet 0x80 Mainnet, 0xEF Testnet (or Test Network: 0x6f and Namecoin Net:0x34) generate private key through sha256 random values. and translate to hex get usedful private key. It will be used for secp256k1 generate check code. two times SHA256 at privatKey. base58(privat key + the version number + check code). it is used as WIF(Wallet import Format) privatKey

Kind: instance method of secUtil Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.generatePrivateKey()

generatePublicKey(key, addrVer)

generate public key

Kind: instance method of secUtil

Param Type Description
key Buffer
addrVer Buffer input addVer from generatePrivateKey() set elliptic point and x,y axis not sure whether useful let x = pubPoint.getX() let y = pubPoint.getY() use secp256k1. generate public key structe public key: 1(network ID) + 32bytes(from x axis) + 32bytes(from y axis) ripemd160(sha256(public key))
Example
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.generatePublicKey(key, addrVer)

generateAddress(publicKey, addrVer)

double sha256 generate hashExtRipe2. sha256(sha256(version number + hashBuffer)). the first 4 bytes of hashExtRipe2 are used as a checksum and placed at the end of the 21 byte array. structe secBinary: 1(network ID) + concatHash + 4 byte(checksum)

Kind: instance method of secUtil

Param Type Description
publicKey Buffer input public key from generatePublicKey()
addrVer Buffer input addVer from generatePrivateKey() generate WIF private key and translate to hex generate SEC Address and translate to hex
Example
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.generateAddress(publicKey, addrVer)

getPrivateKey()

return four private key, wif private key, public key and sec address

Kind: instance method of secUtil
Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.getPrivateKey()

generateContractAddress

A utility function of generating an address of a newly created contract

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.generateContractAddress(from, nonce)


defineProperties

A utility function of generating an address of a newly created contract

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.defineProperties(from, nonce)

padToEven

A utility function of adding a value in type of string.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.padToEven(value)

toBuffer

A utility function of adding a value to buffer.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.toBuffer(v)

baToJSON

A utility function of converting buffer value to JSON format.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.baToJSON(from, nonce)

stripZeros

A utility function of stripping zeros.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.stripZeros(a)

unpad

A utility function of leading zeros from a Buffer or an Array.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.unpad(a)

isHexString

A utility function of confirming a hex string.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.isHexString(value, length)

intToBuffer

A utility function of converting a Number to a Buffer.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.intToBuffer(i)

intToHex

A utility function of converting Number into a hex String.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.intToHex(i)

rlphash

A utility function of creating SHA-3 hash of the RLP encoded version of the input.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.rlphash(a)

keccak

A utility function of creating Keccak hash of the input.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.keccak(a, bits)

zeros

A utility function of returning a buffer filled with 0s.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.zeros(bytes)

getBinarySize

A utility function of getting the binary size of a string.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.getBinarySize(str)

arrayContainsArray

A utility function of returning 'TRUE' if the first specified array contains all elements from the second one and returning 'FALSE' otherwise.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.arrayContainsArray(superset, subset, some)

toUtf8

A utility function of getting utf8 from its hex representation.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.toUtf8(hex)

toAscii

A utility function of getting ascii from its hex representation.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.toAscii(hex)

fromUtf8

A utility function of getting hex representation (prefixed by 0x) of utf8 string.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.fromUtf8(stringValue)

fromAscii

A utility function of getting hex representation (prefixed by 0x) of ascii string.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.fromAscii(stringValue)

getKeys

A utility function of getting specific key from inner object array of objects.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.getKeys(params, key, allowEmptyfrom)

bufferToHex

A utility function of converting a Buffer into a hex String.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.bufferToHex(buff)

zeroAddress

A utility function of returning a zero address.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.zeroAddress()

setLengthLeft

A utility function of left padding an Array or Buffer with leading zeros till it has length bytes.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.setLengthLeft(msg, length, right)

setLength

A utility function of padding an Array or Buffer with leading zeros till it has length bytes.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.setLength(msg, length, right)

setLengthRight

A utility function of right padding an Array or Buffer with leading zeros till it has length bytes. Or it truncates the beginning if it exceeds.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.setLengthRight(msg, length)

bufferToInt

A utility function of Converting a Buffer to a Number.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.bufferToInt(buf)

fromSigned

A utility function of interpreting a Buffer as a signed integer and returns a BN. Assumes 256-bit numbers.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.fromSigned(num)

toUnsigned

A utility function of converting a BN to an unsigned integer and returns it as a Buffer. Assumes 256-bit numbers.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.toUnsigned(num)

keccak256

A utility function of creating Keccak-256 hash of the input, alias for keccak(a, 256).

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.keccak256(a)

sha3

A utility function of creating a keccak hash of input.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.sha3()

sha256

A utility function of creating SHA256 hash of the input.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.sha256(a)

ripemd160

A utility function of creating RIPEMD160 hash of the input.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.ripemd160(a,padded)

isValidPrivate

A utility function of checking if the private key satisfies the rules of the curve secp256k1.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.isValidPrivate(privateKey)

isValidPublic

A utility function of checking if the public key satisfies the rules of the curve secp256k1 and the requirements of SEC.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.isValidPublic(publicKey, sanitize)

publicToAddress

A utility function of returning the SEC address of a given public key accepting "SEC public keys" and SEC1 encoded keys.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.publicToAddress(pubKey, sanitize)

privateToPublic

A utility function of returning the SEC public key of a given private key.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.privateToPublic(privateKey)

importPublic

A utility function of converting a public key to the SEC format.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.importPublic(publicKey)

ecsign

A utility function of ECDSA sign.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.ecsign(msgHash, privateKey)

hashPersonalMessage

A utility function of Returns the keccak-256 hash of message, prefixed with the header used by the SEC_sign RPC call. The output of this function can be fed into ecsign to produce the same signature as the SEC_sign call for a given message, or fed to ecrecover along with a signature to recover the public key used to produce the signature.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.hashPersonalMessage(message)

ecrecover

A utility function of ECDSA public key recovery from signature.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.ecrecover(msgHash, v, r, s)

toRpcSig

A utility function of converting signature parameters into the format of SEC_sign RPC method.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.toRpcSig(v, r, s)

fromRpcSig

A utility function of converting signature format of the SEC_sign RPC method to signature parameters.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.fromRpcSig(sig)

privateToAddress

A utility function of returning the SEC address of a given private key.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.privateToAddress(privateKey)

isValidAddress

A utility function of checking if the address is a valid. Accepts checksummed addresses too.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.isValidAddress(address)

isZeroAddress

A ultiity function of checking if a given address is a zero address.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.isZeroAddress(address)

toChecksumAddress

A utility function of returning a checksummed address.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.toChecksumAddress(address)

isValidChecksumAddress

A utility function of checking if the address is a valid checksummed address.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.isValidChecksumAddress(address)

isPrecompiled

A utility function of returning true if the supplied address belongs to a precompiled account (Byzantium).

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.isPrecompiled(address)

addHexPrefix

A utility function of adding "0x" to a given String if it does not already start with "0x".

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.addHexPrefix(str)

isValidSignature

A utility function of validating ECDSA signature.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.isValidSignature(v, r, s, homestead)

stripHexPrefix

A utility function of stripping hex sting.

Example

const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
    timeServer: 'DE'
})
util.stripHexPrefix()

LICENSE

MPL-2.0


SEC工具库-中文简介

Utils,是英语Utility(意思是功能,工具)的复数,Utilities的简写;是区块链开发的工具库;是内部由封装了多个功能函数(例如获取时间戳函数UnixTime等)的库组成。其作用是为进一步开发提供可直接调用的函数,可在下一步SEC区块数据结构和交易流程的开发中直接调用,使整个SEC区块链系统轻量、高效。

SEC地址是为了减少接收方所需标识的字节数。SEC地址(secAddress)的生成步骤如下:

  1. 将公钥通过SHA256哈希算法处理得到32字节的哈希值,
  2. 后对得到的哈希值通过RIPEMD-160算法来得到20字节的哈希值 —— Hash160 即ripemd160(sha256(publicKey))
  3. 把版本号[2]+Hash160组成的21字节数组进行双次SHA256哈希运算,得到的哈希值的头4个字节作为校验和,放置21字节数组的末尾。
  4. 对组成25位数组进行Base58编码,就得到地址。

Readme

Keywords

Package Sidebar

Install

npm i @sec-block/secjs-util

Weekly Downloads

6

Version

1.1.22

License

MPL-2.0

Unpacked Size

77.5 kB

Total Files

4

Last publish

Collaborators

  • abysmli
  • bi7012xiao
  • feiminhao
  • g277321
  • tornadoming
  • xinnnnn
  • zuweihan1992