加入默克根计算函数
This commit is contained in:
parent
906316d38f
commit
e968ea686f
30
index.js
30
index.js
@ -364,6 +364,36 @@ module.exports = {
|
||||
return null
|
||||
}
|
||||
,
|
||||
getMerkleRoot:function(todoHashList, option){
|
||||
//深拷贝传入数组,防止引用对象被改变
|
||||
let hashList = [...todoHashList]
|
||||
if(!Array.isArray(hashList))
|
||||
return null
|
||||
var border = hashList.length;
|
||||
if(border == 0)
|
||||
return this.hash('')
|
||||
if(border == 1)
|
||||
return this.hash(hashList[0]);
|
||||
while(1){
|
||||
let i = 1,j = 0;
|
||||
for(; i < border; i = i + 2){
|
||||
hashList[j] = this.hash(hashList[i - 1] + hashList[i]);
|
||||
if(border == 2){
|
||||
return hashList[0];
|
||||
}
|
||||
if(i + 1 == border) break;
|
||||
j = j + 1;
|
||||
if(i + 2 == border){
|
||||
i = i + 1;
|
||||
hashList[j] = this.hash(hashList[i]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
border = j + 1;
|
||||
}
|
||||
return hashList
|
||||
}
|
||||
,
|
||||
distanceSig:function(hash, sig){ // hash为64hex字符,sig为128hex字符。返回用hex表达的距离。
|
||||
if (this.isSignature(sig) && this.isHash(hash)){
|
||||
var hashSig=this.hash(sig) // 把签名也转成32字节的哈希,同样长度方便比较
|
||||
|
Loading…
Reference in New Issue
Block a user