a collection of utility functions for SECBlock
npm install @sec-block/secjs-utiljs
npm install @sec-block/secjs-util --save
`
Usage
`js
class SecUtils {
...
func1()
func2()
...
}
const util = new SecUtils()
util.func1()
`
*
$3
A utility function of getting a Unix timestamp in second.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.currentUnixTimeSecond()
`
*
$3
A utility function of getting a Unix timestamp in millisecond.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.currentUnixTimeInMillisecond()
`
*
$3
A utility function of converting a standard GMT time to a Unix timestamp.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.getDatetime()
`
*
$3
A utility function of converting a Unix timestamp to a standard GMT time.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.getUnixtime()
`
*
$3
A utility function of get utc time from ntp server.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.asyncGetUTCTimeFromeServer().then(callback).catch(err)
`
*
$3
A utility function to refresh the time difference between local host and ntp server.
Example
`js
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)
})
`
*
$3
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
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.hasha256(data)
`
*
$3
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
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.generatePrivateKey()
`
*
$3
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
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.generatePublicKey(key, addrVer)
`
*
$3
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
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.generateAddress(publicKey, addrVer)
`
*
$3
return four private key, wif private key, public key
and sec address
Kind: instance method of secUtil
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.getPrivateKey()
`
*
$3
A utility function of generating an address of a newly created contract
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.generateContractAddress(from, nonce)
`
*
$3
A utility function of generating an address of a newly created contract
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.defineProperties(from, nonce)
`
*
$3
A utility function of adding a value in type of string.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.padToEven(value)
`
*
$3
A utility function of adding a value to buffer.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.toBuffer(v)
`
*
$3
A utility function of converting buffer value to JSON format.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.baToJSON(from, nonce)
`
*
$3
A utility function of stripping zeros.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.stripZeros(a)
`
*
$3
A utility function of leading zeros from a Buffer or an Array.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.unpad(a)
`
*
$3
A utility function of confirming a hex string.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.isHexString(value, length)
`
*
$3
A utility function of converting a Number to a Buffer.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.intToBuffer(i)
`
*
$3
A utility function of converting Number into a hex String.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.intToHex(i)
`
*
$3
A utility function of creating SHA-3 hash of the RLP encoded version of the input.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.rlphash(a)
`
*
$3
A utility function of creating Keccak hash of the input.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.keccak(a, bits)
`
*
$3
A utility function of returning a buffer filled with 0s.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.zeros(bytes)
`
*
$3
A utility function of getting the binary size of a string.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.getBinarySize(str)
`
*
$3
A utility function of returning 'TRUE' if the first specified array contains all elements from the second one and returning 'FALSE' otherwise.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.arrayContainsArray(superset, subset, some)
`
*
$3
A utility function of getting utf8 from its hex representation.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.toUtf8(hex)
`
*
$3
A utility function of getting ascii from its hex representation.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.toAscii(hex)
`
*
$3
A utility function of getting hex representation (prefixed by 0x) of utf8 string.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.fromUtf8(stringValue)
`
*
$3
A utility function of getting hex representation (prefixed by 0x) of ascii string.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.fromAscii(stringValue)
`
*
$3
A utility function of getting specific key from inner object array of objects.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.getKeys(params, key, allowEmptyfrom)
`
*
$3
A utility function of converting a Buffer into a hex String.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.bufferToHex(buff)
`
*
$3
A utility function of returning a zero address.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.zeroAddress()
`
*
$3
A utility function of left padding an Array or Buffer with leading zeros till it has length bytes.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.setLengthLeft(msg, length, right)
`
*
$3
A utility function of padding an Array or Buffer with leading zeros till it has length bytes.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.setLength(msg, length, right)
`
*
$3
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
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.setLengthRight(msg, length)
`
*
$3
A utility function of Converting a Buffer to a Number.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.bufferToInt(buf)
`
*
$3
A utility function of interpreting a Buffer as a signed integer and returns a BN. Assumes 256-bit numbers.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.fromSigned(num)
`
*
$3
A utility function of converting a BN to an unsigned integer and returns it as a Buffer. Assumes 256-bit numbers.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.toUnsigned(num)
`
*
$3
A utility function of creating Keccak-256 hash of the input, alias for keccak(a, 256).
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.keccak256(a)
`
*
$3
A utility function of creating a keccak hash of input.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.sha3()
`
*
$3
A utility function of creating SHA256 hash of the input.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.sha256(a)
`
*
$3
A utility function of creating RIPEMD160 hash of the input.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.ripemd160(a,padded)
`
*
$3
A utility function of checking if the private key satisfies the rules of the curve secp256k1.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.isValidPrivate(privateKey)
`
*
$3
A utility function of checking if the public key satisfies the rules of the curve secp256k1 and the requirements of SEC.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.isValidPublic(publicKey, sanitize)
`
*
$3
A utility function of returning the SEC address of a given public key accepting "SEC public keys" and SEC1 encoded keys.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.publicToAddress(pubKey, sanitize)
`
*
$3
A utility function of returning the SEC public key of a given private key.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.privateToPublic(privateKey)
`
*
$3
A utility function of converting a public key to the SEC format.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.importPublic(publicKey)
`
*
$3
A utility function of ECDSA sign.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.ecsign(msgHash, privateKey)
`
*
$3
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
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.hashPersonalMessage(message)
`
*
$3
A utility function of ECDSA public key recovery from signature.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.ecrecover(msgHash, v, r, s)
`
*
$3
A utility function of converting signature parameters into the format of SEC_sign RPC method.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.toRpcSig(v, r, s)
`
*
$3
A utility function of converting signature format of the SEC_sign RPC method to signature parameters.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.fromRpcSig(sig)
`
*
$3
A utility function of returning the SEC address of a given private key.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.privateToAddress(privateKey)
`
*
$3
A utility function of checking if the address is a valid. Accepts checksummed addresses too.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.isValidAddress(address)
`
*
$3
A ultiity function of checking if a given address is a zero address.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.isZeroAddress(address)
`
*
$3
A utility function of returning a checksummed address.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.toChecksumAddress(address)
`
*
$3
A utility function of checking if the address is a valid checksummed address.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.isValidChecksumAddress(address)
`
*
$3
A utility function of returning true if the supplied address belongs to a precompiled account (Byzantium).
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.isPrecompiled(address)
`
*
$3
A utility function of adding "0x" to a given String if it does not already start with "0x".
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.addHexPrefix(str)
`
*
$3
A utility function of validating ECDSA signature.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.isValidSignature(v, r, s, homestead)
`
*
$3
A utility function of stripping hex sting.
Example
`js
const SecUtils = require('@sec-block/secjs-util')
const util = new SecUtils({
timeServer: 'DE'
})
util.stripHexPrefix()
``