Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commitment of RGB++ Tx should be verified #622

Closed
Keith-CY opened this issue Apr 14, 2024 · 3 comments
Closed

Commitment of RGB++ Tx should be verified #622

Keith-CY opened this issue Apr 14, 2024 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@Keith-CY
Copy link
Member

This transaction https://pudge.explorer.nervos.org/transaction/0x235835820029c1438a11f1ee62056e353aa5f36dedb1320025960604a312a464 contains an RGB++ Cell, which is expected to be bound to BTC Tx https://mempool.space/testnet/tx/aee4e8e3aa95e9e9ab1f0520714031d92d3263262099dcc7f7d64e62fa2fcb44

But the commitment in that BTC tx is invalid, it doesn't cover the CKB Transaction, so it's not an effective RGB++ isomorphic transaction.

To make the RGB++ recognition more precise, the commitment should be verified.

Noted that the calculation might lead to performance issue, please make it asynchronous and return its process status in API for clarity.

Ref:

@Keith-CY Keith-CY added the bug Something isn't working label Apr 14, 2024
@Keith-CY Keith-CY moved this to 🆕 New in CKB Explorer Apr 14, 2024
@poor-defined poor-defined assigned rabbitz and unassigned poor-defined Apr 16, 2024
@poor-defined poor-defined moved this from 🆕 New to 📌CurrentWeek in CKB Explorer Apr 16, 2024
@poor-defined poor-defined self-assigned this Apr 16, 2024
@rabbitz
Copy link

rabbitz commented Apr 18, 2024

  def calculate_commitment
    data = JSON.parse('{
      "inputs": [
        {
          "previous_output": {
            "tx_hash": "0x047b6894a0b7a4d7a73b1503d1ae35c51fc5fa6306776dcf22b1fb3daaa32a29",
            "index": "0x0"
          },
          "since": "0x0"
        }
      ],
      "outputs": [
        {
          "lock": {
            "code_hash": "0xd5a4e241104041f6f12f11bddcf30bd7b2f818722f78353fde019f5081cd6b49",
            "hash_type": "type",
            "args": "0x010000000000000000000000000000000000000000000000000000000000000000000000"
          },
          "capacity": "0x0000000000000000",
          "type": {
            "code_hash": "0xc4957f239eb3db9f5c5fb949e9dd99adbb8068b8ac7fe7ae49495486d5e5d235",
            "hash_type": "type",
            "args": "0x43094caf2f2bcdf6f5ab02c2de744936897278d558a2b6924db98a4f27d629e2"
          }
        },
        {
          "lock": {
            "code_hash": "0xd5a4e241104041f6f12f11bddcf30bd7b2f818722f78353fde019f5081cd6b49",
            "hash_type": "type",
            "args": "0x010000000000000000000000000000000000000000000000000000000000000000000000"
          },
          "capacity": "0x0000000000000000",
          "type": {
            "code_hash": "0xc4957f239eb3db9f5c5fb949e9dd99adbb8068b8ac7fe7ae49495486d5e5d235",
            "hash_type": "type",
            "args": "0x43094caf2f2bcdf6f5ab02c2de744936897278d558a2b6924db98a4f27d629e2"
          }
        }
      ],
      "outputs_data": [
        "0x2c010000000000000000000000000000",
        "0xbc020000000000000000000000000000"
      ],
      "cell_deps": []
    }').with_indifferent_access

    transaction = CKB::Types::Transaction.from_h(data)

    hash = Digest::SHA256.new
    hash.update("RGB++")
    version = [0, 0].pack("C*")
    hash.update(version)
    hash.update([transaction.inputs.length, transaction.outputs.length].pack("C*"))

    transaction.inputs.each do |input|
      out_point = input.previous_output
      binary_out_point = CKB::Utils.hex_to_bin(CKB::Serializers::OutPointSerializer.new(out_point).serialize)
      hash.update(binary_out_point.bytes.pack("C*"))
    end

    transaction.outputs.each_with_index do |output, index|
      binary_output = CKB::Utils.hex_to_bin(CKB::Serializers::OutputSerializer.new(output).serialize)
      hash.update(binary_output.bytes.pack("C*"))

      output_data = transaction.outputs_data[index]

      output_data_serializer = CKB::Serializers::OutputDataSerializer.new(output_data)
      output_data_length = output_data_serializer.as_json["items_count"]
      binary_output_data_length = CKB::Utils.hex_to_bin("0x#{[output_data_length].pack('V').unpack1('H*')}")
      hash.update(binary_output_data_length.bytes.pack("C*"))

      binary_output_data = CKB::Utils.hex_to_bin(output_data).bytes.pack("C*")
      hash.update(binary_output_data.bytes.pack("C*"))
    end

    Digest::SHA256.hexdigest(hash.digest.bytes.pack("C*"))
  end

The commitment calculated using the above code is consistent with the test case results (7cdecc8cc293d491a0cbf44e92feabfc29e79408c1d2f7547b334c42efe13131) in the SDK, but the results on the mainnet are inconsistent. I wonder where the error is?

https://github.com/ckb-cell/rgbpp-sdk/blob/a39caa889d0ac639c145c0ec10a7dbcea624d504/packages/ckb/src/utils/rgbpp.spec.ts#L30-L72

@rabbitz
Copy link

rabbitz commented Apr 18, 2024

@rabbitz
Copy link

rabbitz commented May 15, 2024

@poor-defined poor-defined moved this from 📌CurrentWeek to ✅ Done in CKB Explorer Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

No branches or pull requests

3 participants