update to standard .gitignore

This commit is contained in:
Luk Lu
2022-12-10 19:12:14 +08:00
parent ab105032ad
commit cdeb44f43f
2 changed files with 79 additions and 89 deletions

63
ticc.js
View File

@@ -114,7 +114,7 @@ class TicCrypto {
* @return {Boolean}
* @memberof TicCrypto
*/
static is_secword ({ secword, mode = 'strict', lang } = {}) {
static is_secword ({ secword = '', mode = 'strict', lang } = {}) {
// 注意 not all 12 words combinations are valid for both bitcore and bip39, because there are checksum in mnemonic. 另外实际上bitcore和bip39对 12, 15, 18, 21, 24 长度的合法助记词都返回 true。
//// for bitcore-mnemonic. 注意bitcore-mnemonic 对少于12词的会抛出异常很蠢。
@@ -201,10 +201,7 @@ class TicCrypto {
if (salt && typeof salt === 'string') data = data + this.hash_easy(salt)
let inputEncoding = input // my.INPUT_LIST.includes(input)?input:my.INPUT // 'utf8', 'ascii' or 'latin1' for string data, default to utf8 if not specified; ignored for Buffer, TypedArray, or DataView.
let outputEncoding = output === 'buf' ? undefined : output // (my.OUTPUT_LIST.includes(output)?output:my.OUTPUT) // output: 留空=》默认输出hex格式或者手动指定 'buf', hex', 'latin1' or 'base64'
return crypto
.createHash(hasher)
.update(data, inputEncoding)
.digest(outputEncoding)
return crypto.createHash(hasher).update(data, inputEncoding).digest(outputEncoding)
}
return null
}
@@ -396,10 +393,7 @@ class TicCrypto {
static pass_to_keypair ({ pass, hasher = my.HASHER } = {}) {
// 如果使用其他机制例如密码、随机数不使用secword也可生成keypair
if (this.is_hashable({ data: pass })) {
var hashBuf = crypto
.createHash(hasher)
.update(pass)
.digest()
var hashBuf = crypto.createHash(hasher).update(pass).digest()
var keypair = nacl.sign.keyPair.fromSeed(hashBuf) // nacl的seed要求是32字节
return {
hash: hashBuf.toString('hex'),
@@ -469,10 +463,7 @@ class TicCrypto {
}
if (tool === 'nacl') {
// 采用自己的算法bip39算法从secword到种子hash后用 nacl.sign.keyPair.fromSeed()方法。
let hashBuf = crypto
.createHash(hasher)
.update(this.secword_to_seed({ secword, pass }))
.digest()
let hashBuf = crypto.createHash(hasher).update(this.secword_to_seed({ secword, pass })).digest()
let keypair = nacl.sign.keyPair.fromSeed(hashBuf) // nacl.sign.keyPair.fromSeed 要求32字节的种子而 this.secword2seed生成的是64字节种子所以要先做一次sha256
return {
pubkey: Buffer.from(keypair.publicKey).toString('hex'), // 测试过 不能直接keypair.publicKey.toString('hex')不是buffer类型
@@ -688,14 +679,8 @@ class TicCrypto {
.digest('hex')
.slice(-40)
} else {
let h256buf = crypto
.createHash('sha256')
.update(Buffer.from(pubkey, 'hex'))
.digest()
let h160 = crypto
.createHash('ripemd160')
.update(h256buf)
.digest('hex')
let h256buf = crypto.createHash('sha256').update(Buffer.from(pubkey, 'hex')).digest()
let h160 = crypto.createHash('ripemd160').update(h256buf).digest('hex')
return h160
}
}
@@ -718,9 +703,7 @@ class TicCrypto {
if (coin === 'ETH' || coinFamily === 'ETH') {
// 对以太坊,按照 EIP55把纯位置转换为大小写敏感能自我验证的hex地址。仍然为20节=40符。
position = position.toLowerCase().replace('0x', '')
let hash = keccak('keccak256')
.update(position)
.digest('hex')
let hash = keccak('keccak256').update(position).digest('hex')
address = '0x'
for (var i = 0; i < position.length; i++) {
if (parseInt(hash[i], 16) >= 8) {
@@ -1100,10 +1083,7 @@ class TicCrypto {
// hash为64hex字符sig为128hex字符。返回用hex表达的距离。
if (this.is_signature({ sig: sig }) && this.is_hash({ hash })) {
var hashSig = this.hash_easy(sig) // 把签名也转成32字节的哈希同样长度方便比较
return new BigInt(hash, 16)
.subtract(new BigInt(hashSig, 16))
.abs()
.toString(16)
return new BigInt(hash, 16).subtract(new BigInt(hashSig, 16)).abs().toString(16)
}
return null
}
@@ -1322,10 +1302,7 @@ class TicCrypto {
* @returns
*/
static b64_to_b64t (b64 = '') {
return b64
.replace(/\+/g, '.')
.replace(/\//g, '_')
.replace(/=/g, '')
return b64.replace(/\+/g, '.').replace(/\//g, '_').replace(/=/g, '')
}
static b64t_to_b64 (b64t = '') {
@@ -1399,9 +1376,7 @@ class TicCrypto {
static hex_to_eip55 (hex) {
if (/^(0x)?[\da-fA-F]+$/.test(hex)) {
hex = hex.toLowerCase().replace('0x', '')
let hash = keccak('keccak256')
.update(hex)
.digest('hex')
let hash = keccak('keccak256').update(hex).digest('hex')
let result = ''
for (var i = 0; i < hex.length; i++) {
if (parseInt(hash[i], 16) >= 8) {
@@ -1457,11 +1432,7 @@ class TicCrypto {
const pIdent = new BigInt('3fffffffffffffffffffffffffffffffffffffffffffffffffffffffbfffff0c', 16) // prime.add(1).divide(4);
var signY = new Number(compressed[1]) - 2
var x = new BigInt(compressed.substr(2), 16)
var y = x
.modPow(3, prime)
.add(7)
.mod(prime)
.modPow(pIdent, prime) // y mod p = +-(x^3 + 7)^((p+1)/4) mod p
var y = x.modPow(3, prime).add(7).mod(prime).modPow(pIdent, prime) // y mod p = +-(x^3 + 7)^((p+1)/4) mod p
if (y.mod(2).toJSNumber() !== signY) {
// If the parity doesn't match it's the *other* root
y = prime.subtract(y) // y = prime - y
@@ -1527,21 +1498,15 @@ class TicCrypto {
const fullHex = `01${multicodec[cidCodec]}${multialgo[cidAlgo]}${Number(cosh.length / 2).toString(16)}${cosh}`
let converted = ''
if (cidBase === 'b32') {
converted = this.hex_to_b32(fullHex)
.toLowerCase()
.replace(/=/g, '')
converted = this.hex_to_b32(fullHex).toLowerCase().replace(/=/g, '')
} else if (cidBase === 'B32') {
converted = this.hex_to_b32(fullHex)
.toUpperCase()
.replace(/=/g, '')
converted = this.hex_to_b32(fullHex).toUpperCase().replace(/=/g, '')
} else if (cidBase === 'b58') {
converted = this.hex_to_b58(fullHex)
} else if (cidBase === 'b64p') {
converted = Buffer.from(fullHex, 'hex').toString('base64')
} else if (cidBase === 'b64') {
converted = Buffer.from(fullHex, 'hex')
.toString('base64')
.replace(/=/g, '')
converted = Buffer.from(fullHex, 'hex').toString('base64').replace(/=/g, '')
}
return multibase[cidBase] + converted
}