0

我正在尝试使用链点收据(版本 3)证明哈希的存在。使用他们的 API,我提交了一个哈希并收到了收据,但我不知道如何执行哈希操作来获取 merkle 根。

这是我的收据:

var hash='5826bb4d3411ec5cf6d5052eebf812063197872891d875d7be406400e2931424';
var proof = {
  "@context": "https://w3id.org/chainpoint/v3",
  "type": "Chainpoint",
  "hash": "5826bb4d3411ec5cf6d5052eebf812063197872891d875d7be406400e2931424",
  "hash_id_node": "0e245ec0-8142-11e9-bed4-017015d9bf71",
  "hash_submitted_node_at": "2019-05-28T12:13:51Z",
  "hash_id_core": "0f048590-8142-11e9-be51-01e14610ae21",
  "hash_submitted_core_at": "2019-05-28T12:13:53Z",
  "branches": [
    {
      "label": "cal_anchor_branch",
      "ops": [
        {
          "l": "node_id:0e245ec0-8142-11e9-bed4-017015d9bf71"
        },
        {
          "op": "sha-256"
        },
        {
          "l": "core_id:0f048590-8142-11e9-be51-01e14610ae21"
        },
        {
          "op": "sha-256"
        },
        {
          "l": "nistv2:1559045520000:d9bdc4ddd1b82ed90274022f00bb01c5378357f4013ce81199257efc96e8f3e5d47a315edc7871b328ce683a2c58c468a406fc9e518bcc27abb75602ce01c9fb"
        },
        {
          "op": "sha-256"
        },
        {
          "r": "4ca74e3ec8fafe24e3369ab4ab14a986a3894a135c0ce73e00fe31db50ecf27f"
        },
        {
          "op": "sha-256"
        },
        {
          "r": "80529beed1232c31e9bb8accdd87d6203d9b98621481da2de1fd7d4f3441b820"
        },
        {
          "op": "sha-256"
        },
        {
          "r": "a3688e37b43b4548543fd39261165812d6cf2e70dd50f54aa7132c9d7990c3f4"
        },
        {
          "op": "sha-256"
        },
        {
          "l": "3277842:1559045636:1:https://b.chainpoint.org:cal:3277842"
        },
        {
          "r": "d0a8b8e6d4cb14d2b23836538382757b34480fb3eb9e3e37cae4d3b4a754a228"
        },
        {
          "op": "sha-256"
        },
        {
          "anchors": [
            {
              "type": "cal",
              "anchor_id": "3277842",
              "uris": [
                "https://b.chainpoint.org/calendar/3277842/hash"
              ]
            }
          ]
        }
      ]
    }
  ]
}

默克尔树的默克尔根,如果我做得对的话,就是地址https://b.chainpoint.org/calendar/3277842/hash

var merkle_root = '2bf7a252fb641a5b67e9563e5e4fc4f9fc11d5a74ffa4ef303db7678a1b521f9';

我写了一个简单的脚本来证明我的哈希的存在:

var string = hash;
var obj = null;
var ops = proof.branches[0];
for ( var i = 0; i < ops.length; i++ ) {
    obj = ops[ i ];
    if ( Object.keys( obj )[0] === 'l' ) {
        string = `${obj['l']}${string}`;
    } else if ( Object.keys( obj )[0] === 'r' ) {
        string = `${string}${obj['r']}`;
    } else if ( Object.keys( obj )[0] === 'op' ) {
        string = h256(string);
    }
}

console.log( string === merkle_root );

我找不到我的错误在哪里,任何帮助将不胜感激。

4

1 回答 1

1

我找到了阅读 chanpoint-parse 代码的解决方案。我走在正确的轨道上。这是工作代码:


function isHex(value) {
    var hexRegex = /^[0-9A-Fa-f]{2,}$/
    var result = hexRegex.test(value)
    if (result) result = !(value.length % 2)
    return result
}

var string = Buffer.from(hash, 'hex');
var obj = null;
for ( var i = 0; i < ops.length; i++ ) {
    obj = ops[ i ];
    if ( Object.keys( obj )[0] === 'l' ) {
        let concat_value = isHex(obj['l'])
          ? Buffer.from(obj['l'], 'hex')
          : Buffer.from(obj['l'], 'utf8');
        string = Buffer.concat( [ concat_value, string ] );
    } else if ( Object.keys( obj )[0] === 'r' ) {
        let concat_value = isHex(obj['r'])
          ? Buffer.from(obj['r'], 'hex')
          : Buffer.from(obj['r'], 'utf8');
        string = Buffer.concat( [ string, concat_value ] );
    } else if ( Object.keys( obj )[0] === 'op' ) {
        string = crypto
            .createHash('sha256')
            .update(string)
            .digest()
    }
}

console.log( string.toString('hex') === m_root );
// true
于 2019-06-01T09:48:27.267 回答