rename all libs from xxx.yyy to xxx-yyy
This commit is contained in:
110
index.js
110
index.js
@@ -45,9 +45,9 @@ my.REGEXP_ALPHABET = {
|
||||
|
||||
/**
|
||||
*
|
||||
* @class TICrypto
|
||||
* @class Ticrypto
|
||||
*/
|
||||
class TICrypto {
|
||||
class Ticrypto {
|
||||
/**
|
||||
* 测试输入数据是否可哈希混淆
|
||||
*
|
||||
@@ -55,7 +55,7 @@ class TICrypto {
|
||||
* @param {*} data 需要被哈希混淆的数据
|
||||
* @param {*} option 可选参数
|
||||
* @return {Boolean}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static isHashable (data, { strict = false } = {}) {
|
||||
if (strict) {
|
||||
@@ -71,7 +71,7 @@ class TICrypto {
|
||||
* @param {String} hash
|
||||
* @param {Object} option [{ hasher = my.HASHER }={}]
|
||||
* @return {Boolean}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static isHash (hash, { hasher = my.HASHER } = {}) {
|
||||
if (my.HASHER_LIST.includes(hasher)) {
|
||||
@@ -97,7 +97,7 @@ class TICrypto {
|
||||
* @param {String} secword
|
||||
* @param {Object} option [{ mode = 'strict' }={}]
|
||||
* @return {Boolean}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static isSecword (secword, { mode = 'strict' } = {}) {
|
||||
// 注意 not all 12 words combinations are valid for both bitcore and bip39, because there are checksum in mnemonic. 另外,实际上bitcore和bip39对12, 15, 18, ... 长度的合法助记词都返回 true。
|
||||
@@ -124,7 +124,7 @@ class TICrypto {
|
||||
* @static
|
||||
* @param {String} seckey
|
||||
* @return {Boolean}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static isSeckey (seckey) {
|
||||
// 比特币、以太坊的私钥:64 hex
|
||||
@@ -138,7 +138,7 @@ class TICrypto {
|
||||
* @static
|
||||
* @param {String} pubkey
|
||||
* @return {Boolean}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static isPubkey (pubkey) {
|
||||
// 比特币的公钥:压缩型 '02|03' + 64 hex 或 无压缩型 '04' + 128 hex
|
||||
@@ -153,7 +153,7 @@ class TICrypto {
|
||||
* @static
|
||||
* @param {String} signature
|
||||
* @return {Boolean}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static isSignature (signature) {
|
||||
return /^[a-fA-F0-9]{128,144}$/.test(signature) && signature.length % 2 === 0 // 128 for nacl, 140/142/144 for crypto and eccrypto in der format.
|
||||
@@ -166,7 +166,7 @@ class TICrypto {
|
||||
* @param {*} data
|
||||
* @param {option} [{ hasher = my.HASHER, salt, input = my.INPUT, output = my.OUTPUT }={}]
|
||||
* @return {String}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static hash (data, { hasher = my.HASHER, salt, input = my.INPUT, output = my.OUTPUT } = {}) {
|
||||
// data can be anything, but converts to string or remains be Buffer/TypedArray/DataView
|
||||
@@ -190,7 +190,7 @@ class TICrypto {
|
||||
* @param {*} data
|
||||
* @param {*} option [{ tool, keytype, key, input, output, cipher }={}]
|
||||
* @return {String}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static async encrypt ({ data, tool = 'crypto', keytype = 'pwd', key, input, output, cipher } = {}) {
|
||||
if (tool === 'eccrypto') {
|
||||
@@ -213,7 +213,7 @@ class TICrypto {
|
||||
return { iv: iv.toString('hex'), ciphertext } // 有 iv,显然每次结果不一样
|
||||
}
|
||||
} else if (keytype === 'seckey') {
|
||||
// 尚未走通,不能使用 ticCrypto 生成的 Elliptic curve 椭圆曲线算法公私钥,只能用 crypto.generateKeypairs() 生成的 rsa 公私钥
|
||||
// 尚未走通,不能使用 ticrypto 生成的 Elliptic curve 椭圆曲线算法公私钥,只能用 crypto.generateKeypairs() 生成的 rsa 公私钥
|
||||
let seckeyPEM = await new keyman.Key('oct', this.hex_to_buf(key), { namedCurve: 'P-256K' }).export('pem') // 私钥导出的der格式为144字节。
|
||||
return crypto.privateEncrypt(seckeyPEM, Buffer.from(data)) // 返回 Buffer。每次结果都一样。
|
||||
} else if (keytype === 'pubkey') {
|
||||
@@ -230,7 +230,7 @@ class TICrypto {
|
||||
* @param {*} data
|
||||
* @param {Object} option [{ keytype, key, input, output, cipher, format }={}]
|
||||
* @return {String}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static async decrypt ({ data = {}, tool = 'crypto', keytype = 'pwd', key, input, output, cipher } = {}) {
|
||||
// data 应当是 encrypt 输出的数据类型
|
||||
@@ -261,7 +261,7 @@ class TICrypto {
|
||||
return decrypted
|
||||
}
|
||||
} else if (keytype === 'seckey') {
|
||||
// 尚未走通,不能使用 ticCrypto 生成的 Elliptic curve 椭圆曲线算法公私钥
|
||||
// 尚未走通,不能使用 ticrypto 生成的 Elliptic curve 椭圆曲线算法公私钥
|
||||
let seckeyPEM = await new keyman.Key('oct', this.hex_to_buf(key), { namedCurve: 'P-256K' }).export('pem') // 私钥导出的der格式为144字节。
|
||||
return crypto.privateDecrypt(seckeyPEM, Buffer.from(data)) // 返回 Buffer。每次结果都一样。
|
||||
} else if (keytype === 'pubkey') {
|
||||
@@ -279,7 +279,7 @@ class TICrypto {
|
||||
* @param {String} seckey
|
||||
* @param {Object} option [option={}]
|
||||
* @return {String}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static async sign ({ data, seckey, tool = 'crypto', hasher }) {
|
||||
// data can be string or buffer or object, results are the same
|
||||
@@ -316,7 +316,7 @@ class TICrypto {
|
||||
* @param {String} pubkey
|
||||
* @param {Object} option [option={}]
|
||||
* @return {Boolean}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static async verify ({ data, signature, pubkey, tool = 'crypto', hasher }) {
|
||||
// data could be anything, but converts to string or remains be Buffer/TypedArray/DataView
|
||||
@@ -357,7 +357,7 @@ class TICrypto {
|
||||
* @param {String} pass
|
||||
* @param {Object} option
|
||||
* @return {Object} {pubkey, seckey, address,}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static pass2keypair (pass, { hasher } = {}) {
|
||||
// 如果使用其他机制,例如密码、随机数,不使用secword,也可生成keypair
|
||||
@@ -383,7 +383,7 @@ class TICrypto {
|
||||
* @static
|
||||
* @param {*} entropy
|
||||
* @return {String}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static entropy2secword (entropy) {
|
||||
// entropy could be hex string or buffer. Byte length could be of 16, 20, 24, 28, ... which outputs mnemonic of length 12, 15, 18, 21, ...
|
||||
@@ -396,7 +396,7 @@ class TICrypto {
|
||||
* @static
|
||||
* @param {String} secword
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static secword2entropy (secword) {
|
||||
// secword could be of length 12, 15, 18, ... which outputs hex of length 32, 40, ...
|
||||
@@ -410,7 +410,7 @@ class TICrypto {
|
||||
* @param {String} secword
|
||||
* @param {Object} option
|
||||
* @return {Object} {pubkey, seckey,}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static secword2keypair (secword, { coin, pass, path, tool, hasher } = {}) {
|
||||
// coin 币种;
|
||||
@@ -471,7 +471,7 @@ class TICrypto {
|
||||
* @param {*} seed
|
||||
* @param {string} option [{ coin = my.COIN }={ coin: my.COIN }]
|
||||
* @return {String} path
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static seed2path ({ seed, coin = my.COIN } = { coin: my.COIN }) {
|
||||
// 路径规范 BIP44: m/Purpose'/Coin'/Account'/Change/Index,
|
||||
@@ -526,7 +526,7 @@ class TICrypto {
|
||||
* @param {String} secword
|
||||
* @param {Object} option
|
||||
* @return {Object}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static secword2account (secword, { coin, pass, path, tool, hasher } = {}) {
|
||||
// account 比 keypair 多了 address 字段。
|
||||
@@ -551,7 +551,7 @@ class TICrypto {
|
||||
* @param {String} secword
|
||||
* @param {Object} option
|
||||
* @return {String} address
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static secword2address (secword, { coin, world, pass, path, tool, hasher } = {}) {
|
||||
coin = coin?.toUpperCase() || my.COIN
|
||||
@@ -575,7 +575,7 @@ class TICrypto {
|
||||
* @param {*} seckey
|
||||
* @param {*} [option={}]
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static seckey2pubkey (seckey, { curve, compress } = {}) {
|
||||
if (this.isSeckey(seckey) && seckey.length === 64) {
|
||||
@@ -607,7 +607,7 @@ class TICrypto {
|
||||
* @param {*} seckey
|
||||
* @param {*} option
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static seckey2address (seckey, { coin, world } = {}) {
|
||||
coin = coin?.toUpperCase() || my.COIN
|
||||
@@ -632,7 +632,7 @@ class TICrypto {
|
||||
* @param {*} pubkey
|
||||
* @param {*} [{ coin }={}]
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
* position 就是通常所说的 PubKeyHash,出现在比特币交易的锁定脚本里
|
||||
*/
|
||||
static pubkey2position (pubkey, { coin } = {}) {
|
||||
@@ -670,7 +670,7 @@ class TICrypto {
|
||||
* @param {*} position
|
||||
* @param {*} [{ coin, world }={}]
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static position2address (position, { coin, world } = {}) {
|
||||
if (!/^[\da-fA-F]{40}$/.test(position)) return null // 不论 tic, btc, eth,其 position 都是 40字符的。
|
||||
@@ -750,7 +750,7 @@ class TICrypto {
|
||||
*
|
||||
* @static
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
* 地址和PubKeyHash(即position)之间能互相转化
|
||||
*/
|
||||
static address2position () {
|
||||
@@ -782,7 +782,7 @@ class TICrypto {
|
||||
* @static
|
||||
* @param {String} address
|
||||
* @return {Boolean}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static isAddress (address) {
|
||||
if (/^(0x)?[\da-fA-F]{40}$/.test(address)) {
|
||||
@@ -811,7 +811,7 @@ class TICrypto {
|
||||
* @param {*} pubkey
|
||||
* @param {*} [option={}]
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static pubkey2address (pubkey, { coin, world } = {}) {
|
||||
// pubkey 应当是string类型
|
||||
@@ -826,7 +826,7 @@ class TICrypto {
|
||||
* @param {*} secword
|
||||
* @param {*} pass
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static secword2seed (secword, pass) {
|
||||
// 遵循bip39的算法。和 ether.HDNode.mnemonic2Seed 结果一样,是64字节的种子。
|
||||
@@ -840,7 +840,7 @@ class TICrypto {
|
||||
* @static
|
||||
* @param {string} [lang='english']
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static randomSecword (lang = 'english') {
|
||||
// accepts case-insensitive lang, such as 'chinese, cn, tw, en'
|
||||
@@ -884,7 +884,7 @@ class TICrypto {
|
||||
* @static
|
||||
* @param {*} [option={}]
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static randomSeckey ({ coin, tool } = {}) {
|
||||
// 跳过 secword 直接产生随机密钥
|
||||
@@ -901,7 +901,7 @@ class TICrypto {
|
||||
* @static
|
||||
* @param {*} [option={}]
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static randomKeypair ({ tool, purpose } = {}) {
|
||||
let kp
|
||||
@@ -931,7 +931,7 @@ class TICrypto {
|
||||
* @static
|
||||
* @param {*} [option={}]
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static randomAccount ({ lang, coin, pass, path, tool, hasher } = {}) {
|
||||
let secword = this.randomSecword(lang)
|
||||
@@ -945,7 +945,7 @@ class TICrypto {
|
||||
* @param {number} [length=6]
|
||||
* @param {*} alphabet
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static randomString (length = 6, alphabet) {
|
||||
// 长度为 length,字母表为 alphabet 的随机字符串
|
||||
@@ -963,7 +963,7 @@ class TICrypto {
|
||||
* @static
|
||||
* @param {*} [{ length, min, max }={}]
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static randomNumber ({ length, min, max } = {}) {
|
||||
// 长度为 length 的随机数字,或者 (min||0) <= num < max
|
||||
@@ -989,7 +989,7 @@ class TICrypto {
|
||||
* @param {*} targetLength
|
||||
* @param {*} symbol
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static padStart (string, targetLength, symbol) {
|
||||
// 2020-03: 发现在浏览器里,还不支持 string.padStart(),只好自己写个暂代。
|
||||
@@ -1004,7 +1004,7 @@ class TICrypto {
|
||||
* 生成 uuid
|
||||
*
|
||||
* @static
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static randomUuid () {
|
||||
return uuid.v4()
|
||||
@@ -1017,7 +1017,7 @@ class TICrypto {
|
||||
* @param {*} hashList
|
||||
* @param {*} [option={}]
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static getMerkleHash (hashList, { output, hasher } = {}) {
|
||||
// merkle算法略有难度,暂时用最简单的hash代替
|
||||
@@ -1038,7 +1038,7 @@ class TICrypto {
|
||||
* @param {*} todoHashList
|
||||
* @param {*} option
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static getMerkleRoot (todoHashList) {
|
||||
//深拷贝传入数组,防止引用对象被改变
|
||||
@@ -1075,7 +1075,7 @@ class TICrypto {
|
||||
* @param {*} hash
|
||||
* @param {*} sig
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static distanceSig (hash, sig) {
|
||||
// hash为64hex字符,sig为128hex字符。返回用hex表达的距离。
|
||||
@@ -1097,7 +1097,7 @@ class TICrypto {
|
||||
* @param {*} sig1
|
||||
* @param {*} sig2
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static compareSig (hash, sig1, sig2) {
|
||||
// 返回距离hash更近的sig
|
||||
@@ -1130,7 +1130,7 @@ class TICrypto {
|
||||
* @param {*} hash
|
||||
* @param {*} sigList
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static sortSigList (hash, sigList) {
|
||||
if (Array.isArray(sigList) && this.isHash(hash)) {
|
||||
@@ -1189,7 +1189,7 @@ class TICrypto {
|
||||
* @param {*} prikey
|
||||
* @param {*} signType
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static rsaSign (string2Sign, prikey, signType) {
|
||||
signType = signType || 'RSA-SHA1' // could be RSA-SHA256, RSA-SHA1 or more
|
||||
@@ -1206,7 +1206,7 @@ class TICrypto {
|
||||
* @param {*} pubkey
|
||||
* @param {*} signType
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static rsaVerify (string2Verify, signature, pubkey, signType) {
|
||||
signType = signType || 'RSA-SHA1' // could be RSA-SHA256, RSA-SHA1 or more
|
||||
@@ -1220,7 +1220,7 @@ class TICrypto {
|
||||
* @static
|
||||
* @param {*} buffer
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static buf_to_hex (buffer) {
|
||||
// buffer is an ArrayBuffer
|
||||
@@ -1233,7 +1233,7 @@ class TICrypto {
|
||||
* @static
|
||||
* @param {*} hex
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static hex_to_buf (hex) {
|
||||
return new Uint8Array(
|
||||
@@ -1249,7 +1249,7 @@ class TICrypto {
|
||||
* @static
|
||||
* @param {*} hex
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
* 如果出现非HEX的字符,从这个字符(及其同Byte的另一个字符)起直到末尾,都会被忽略掉,但仍然成功返回一个串。
|
||||
* bs58check 和 bs58 可接受string, Buffer, ArrayBuffer, Array (包括空字符串'', 各种内容的数组例如包含 undefined,{...},等等);
|
||||
* 不可接受 undefined, null, {...}, 等等,会返回 exception
|
||||
@@ -1276,7 +1276,7 @@ class TICrypto {
|
||||
* @static
|
||||
* @param {*} box
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static b58c_to_hex (box) {
|
||||
try {
|
||||
@@ -1319,7 +1319,7 @@ class TICrypto {
|
||||
* @static
|
||||
* @param {*} hex
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static hex_to_b64t (hex) {
|
||||
if (/^[0-9a-fA-F]+$/.test(hex)) {
|
||||
@@ -1334,7 +1334,7 @@ class TICrypto {
|
||||
* @static
|
||||
* @param {*} b64t
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static b64t_to_hex (b64t) {
|
||||
if (/^[0-9a-zA-Z\._]+$/.test(b64t)) {
|
||||
@@ -1375,7 +1375,7 @@ class TICrypto {
|
||||
* @static
|
||||
* @param {*} hex
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static hex_to_eip55 (hex) {
|
||||
if (/^(0x)?[\da-fA-F]+$/.test(hex)) {
|
||||
@@ -1402,7 +1402,7 @@ class TICrypto {
|
||||
* @static
|
||||
* @param {*} uncompressed
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static compressPubkey (uncompressed) {
|
||||
// test: https://iancoleman.io/bitcoin-key-compression/
|
||||
@@ -1427,7 +1427,7 @@ class TICrypto {
|
||||
* @static
|
||||
* @param {*} compressed
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
* @memberof Ticrypto
|
||||
*/
|
||||
static decompressPubkey (compressed) {
|
||||
// uncompress: https://stackoverflow.com/questions/17171542/algorithm-for-elliptic-curve-point-compression/53478265#53478265
|
||||
@@ -1512,4 +1512,4 @@ class TICrypto {
|
||||
}
|
||||
|
||||
// 必须单独写 module.exports,不要和类定义写在一起,否则会导致 jsdoc 解析不到类内文档。
|
||||
module.exports = TICrypto
|
||||
module.exports = Ticrypto
|
||||
|
||||
Reference in New Issue
Block a user