Skip to content

Instantly share code, notes, and snippets.

@shaojunda
Last active July 29, 2020 14:38
Show Gist options
  • Save shaojunda/17610478c5e0e1d3a5e72252bad21dce to your computer and use it in GitHub Desktop.
Save shaojunda/17610478c5e0e1d3a5e72252bad21dce to your computer and use it in GitHub Desktop.

不使用 Type ID 部署可升级脚本

部署 always success script

api = CKB::API.new
wallet = CKB::Wallets::NewWallet.new(api: api, from_addresses: "ckt1qyqvsv5240xeh85wvnau2eky8pwrhh4jr8ts8vyj37")
always_success_data = File.read("/your-path-to/always_success")
tx_generator = wallet.generate("ckt1qyqvsv5240xeh85wvnau2eky8pwrhh4jr8ts8vyj37", 8000*10**8, {data: CKB::Utils.bin_to_hex(always_success_data)})
always_success_tx = wallet.sign(tx_generator, "0xd00c06bfd800d27397002dca6fb0993d5ba6399b4238b2f29ee9deb97593d2bc")

always_success_tx_hash = api.send_transaction(always_success_tx) # 0xa055eba9f93a9f0c49aa6983990cce5dc9c83f3c998e0bb6dbd09fb9b75f76b1
always_success_code_hash = CKB::Blake2b.hexdigest(always_success_data)
always_success_dep = CKB::Types::CellDep.new(out_point: CKB::Types::OutPoint.new(tx_hash: "0xa055eba9f93a9f0c49aa6983990cce5dc9c83f3c998e0bb6dbd09fb9b75f76b1", index: 0))
always_success_script = CKB::Types::Script.new(code_hash: always_success_code_hash, args: "0x", hash_type: "data")

部署 carrot type script

api = CKB::API.new
wallet = CKB::Wallet.from_hex(api, "0xd00c06bfd800d27397002dca6fb0993d5ba6399b4238b2f29ee9deb97593d2bc")
carrot_data = File.read("/your-path-to/scripts/carrot")
carrot_tx = wallet.generate_tx("ckt1qyqvsv5240xeh85wvnau2eky8pwrhh4jr8ts8vyj37", 8000*10**8, fee: 8245*1000)
carrot_tx.cell_deps.push(always_success_dep.dup)
carrot_tx.outputs[0].type = always_success_script
carrot_tx.outputs_data[0] = CKB::Utils.bin_to_hex(carrot_data)
carrot_tx = carrot_tx.sign(wallet.key)
carrot_tx_hash = api.send_transaction(carrot_tx) # 0xbb4339d859b3e043ba6f9def9b044706d651b1ffb9341eb76229f5ce73e05a9b

使用 carrot type script

  1. carrot validate success
carrot_dep = CKB::Types::CellDep.new(out_point: CKB::Types::OutPoint.new(tx_hash: "0xbb4339d859b3e043ba6f9def9b044706d651b1ffb9341eb76229f5ce73e05a9b", index: 0))
carrot_type_script = CKB::Types::Script.new(code_hash: always_success_script.compute_hash, hash_type: "type", args: "0x")
api = CKB::API.new
wallet = CKB::Wallet.from_hex(api, "0xd00c06bfd800d27397002dca6fb0993d5ba6399b4238b2f29ee9deb97593d2bc")
tx = wallet.generate_tx("ckt1qyqvsv5240xeh85wvnau2eky8pwrhh4jr8ts8vyj37", 1000*10**8, fee: 1000)
tx.cell_deps.push(carrot_dep.dup)
tx.outputs[0].type = carrot_type_script
tx = tx.sign(wallet.key)
tx_hash = api.send_transaction(tx)
  1. carrot validate failure
carrot_dep = CKB::Types::CellDep.new(out_point: CKB::Types::OutPoint.new(tx_hash: "0xbb4339d859b3e043ba6f9def9b044706d651b1ffb9341eb76229f5ce73e05a9b", index: 0))
carrot_type_script = CKB::Types::Script.new(code_hash: always_success_script.compute_hash, hash_type: "type", args: "0x")
api = CKB::API.new
wallet = CKB::Wallet.from_hex(api, "0xd00c06bfd800d27397002dca6fb0993d5ba6399b4238b2f29ee9deb97593d2bc")
tx1 = wallet.generate_tx("ckt1qyqvsv5240xeh85wvnau2eky8pwrhh4jr8ts8vyj37", 1000*10**8, fee: 1000)
tx1.cell_deps.push(carrot_dep.dup)
tx1.outputs[0].type = carrot_type_script
tx1.outputs_data[0] = CKB::Utils.bin_to_hex("carrot123")
tx1 = tx1.sign(wallet.key)
tx1_hash = api.send_transaction(tx1)

upgrade carrot script

修改 carrot 合约,将校验规则改为如果 data 以 carrot007 开头则校验失败

部署 new carrot type script

api = CKB::API.new
wallet = CKB::Wallet.from_hex(api, "0xd00c06bfd800d27397002dca6fb0993d5ba6399b4238b2f29ee9deb97593d2bc")
new_carrot_data = File.read("/your-path-to/scripts/new_carrot")
new_carrot_tx = wallet.generate_tx("ckt1qyqvsv5240xeh85wvnau2eky8pwrhh4jr8ts8vyj37", 8000*10**8, fee: 8245*1000)
new_carrot_tx.cell_deps.push(always_success_dep.dup)
new_carrot_tx.outputs[0].type = always_success_script
new_carrot_tx.outputs_data[0] = CKB::Utils.bin_to_hex(new_carrot_data)
new_carrot_tx = new_carrot_tx.sign(wallet.key)
new_carrot_tx_hash = api.send_transaction(new_carrot_tx) # 0xfc10bc5ee2fe8743520c0257404c49978c56124feacf5f96e54ba9ef81655cf1

使用 new carrot script

  1. new carrot validate success for no data
new_carrot_dep = CKB::Types::CellDep.new(out_point: CKB::Types::OutPoint.new(tx_hash: "0xfc10bc5ee2fe8743520c0257404c49978c56124feacf5f96e54ba9ef81655cf1", index: 0))
carrot_type_script = CKB::Types::Script.new(code_hash: always_success_script.compute_hash, hash_type: "type", args: "0x")
api = CKB::API.new
wallet = CKB::Wallet.from_hex(api, "0xd00c06bfd800d27397002dca6fb0993d5ba6399b4238b2f29ee9deb97593d2bc")
tx2 = wallet.generate_tx("ckt1qyqvsv5240xeh85wvnau2eky8pwrhh4jr8ts8vyj37", 1000*10**8, fee: 1000)
tx2.cell_deps.push(new_carrot_dep.dup)
tx2.outputs[0].type = carrot_type_script
tx2 = tx2.sign(wallet.key)
tx2_hash = api.send_transaction(tx2)
  1. new carrot validate success with old version key word
new_carrot_dep = CKB::Types::CellDep.new(out_point: CKB::Types::OutPoint.new(tx_hash: "0xfc10bc5ee2fe8743520c0257404c49978c56124feacf5f96e54ba9ef81655cf1", index: 0))
carrot_type_script = CKB::Types::Script.new(code_hash: always_success_script.compute_hash, hash_type: "type", args: "0x")
api = CKB::API.new
wallet = CKB::Wallet.from_hex(api, "0xd00c06bfd800d27397002dca6fb0993d5ba6399b4238b2f29ee9deb97593d2bc")
tx3 = wallet.generate_tx("ckt1qyqvsv5240xeh85wvnau2eky8pwrhh4jr8ts8vyj37", 1000*10**8, fee: 1000)
tx3.cell_deps.push(new_carrot_dep.dup)
tx3.outputs[0].type = carrot_type_script
tx3.outputs_data[0] = CKB::Utils.bin_to_hex("carrot123")
tx3 = tx3.sign(wallet.key)
tx3_hash = api.send_transaction(tx3)
  1. new carrot validate failure
new_carrot_dep = CKB::Types::CellDep.new(out_point: CKB::Types::OutPoint.new(tx_hash: "0xfc10bc5ee2fe8743520c0257404c49978c56124feacf5f96e54ba9ef81655cf1", index: 0))
carrot_type_script = CKB::Types::Script.new(code_hash: always_success_script.compute_hash, hash_type: "type", args: "0x")
api = CKB::API.new
wallet = CKB::Wallet.from_hex(api, "0xd00c06bfd800d27397002dca6fb0993d5ba6399b4238b2f29ee9deb97593d2bc")
tx4 = wallet.generate_tx("ckt1qyqvsv5240xeh85wvnau2eky8pwrhh4jr8ts8vyj37", 1000*10**8, fee: 1000)
tx4.cell_deps.push(new_carrot_dep.dup)
tx4.outputs[0].type = carrot_type_script
tx4.outputs_data[0] = CKB::Utils.bin_to_hex("carrot007")
tx4 = tx4.sign(wallet.key)
tx4_hash = api.send_transaction(tx4)

使用 Type ID 部署可升级脚本

api = CKB::API.new
wallet = CKB::Wallet.from_hex(api, "0xd00c06bfd800d27397002dca6fb0993d5ba6399b4238b2f29ee9deb97593d2bc")
carrot_data = File.read("/your-path-to/scripts/carrot")
tx5 = wallet.generate_tx("ckt1qyqvsv5240xeh85wvnau2eky8pwrhh4jr8ts8vyj37", 8000*10**8, fee: 8245*1000)

serializer = CKB::Serializers::InputSerializer.new(tx5.inputs.first)
blake2b = CKB::Blake2b.new
blake2b.update(CKB::Utils.hex_to_bin(serializer.serialize))
blake2b.update([0].pack("Q<"))
type_id_args = blake2b.hexdigest

type_id_type = CKB::Types::Script.new(code_hash: "0x00000000000000000000000000000000000000000000000000545950455f4944", hash_type: "type", args: type_id_args)
tx5.outputs.first.type = type_id_type
tx5.outputs_data[0] = CKB::Utils.bin_to_hex(carrot_data)

tx5 = tx5.sign(wallet.key)
tx5_hash = api.send_transaction(tx5) # 0xc66cd6fdabcdee803077f6aaeef84c058fb121e553426f67c71c94e7c5d24bb7

使用 Type ID 版本的 carrot script

  1. carrot validate success
carrot_dep = CKB::Types::CellDep.new(out_point: CKB::Types::OutPoint.new(tx_hash: "0xc66cd6fdabcdee803077f6aaeef84c058fb121e553426f67c71c94e7c5d24bb7", index: 0))
carrot_type_script = CKB::Types::Script.new(code_hash: type_id_type.compute_hash, hash_type: "type", args: "0x")
api = CKB::API.new
wallet = CKB::Wallet.from_hex(api, "0xd00c06bfd800d27397002dca6fb0993d5ba6399b4238b2f29ee9deb97593d2bc")
tx6 = wallet.generate_tx("ckt1qyqvsv5240xeh85wvnau2eky8pwrhh4jr8ts8vyj37", 1000*10**8, fee: 1000)
tx6.cell_deps.push(carrot_dep.dup)
tx6.outputs[0].type = carrot_type_script
tx6 = tx6.sign(wallet.key)
tx6_hash = api.send_transaction(tx6)
  1. carrot validate failure
carrot_dep = CKB::Types::CellDep.new(out_point: CKB::Types::OutPoint.new(tx_hash: "0xc66cd6fdabcdee803077f6aaeef84c058fb121e553426f67c71c94e7c5d24bb7", index: 0))
carrot_type_script = CKB::Types::Script.new(code_hash: type_id_type.compute_hash, hash_type: "type", args: "0x")
api = CKB::API.new
wallet = CKB::Wallet.from_hex(api, "0xd00c06bfd800d27397002dca6fb0993d5ba6399b4238b2f29ee9deb97593d2bc")
tx7 = wallet.generate_tx("ckt1qyqvsv5240xeh85wvnau2eky8pwrhh4jr8ts8vyj37", 1000*10**8, fee: 1000)
tx7.cell_deps.push(carrot_dep.dup)
tx7.outputs[0].type = carrot_type_script
tx7.outputs_data[0] = CKB::Utils.bin_to_hex("carrot770")
tx7 = tx7.sign(wallet.key)
tx7_hash = api.send_transaction(tx7)

upgrade carrot script

修改 carrot 合约,将校验规则改为如果 data 以 carrot007 开头则校验失败

部署 new carrot type script

api = CKB::API.new
wallet = CKB::Wallet.from_hex(api, "0xd00c06bfd800d27397002dca6fb0993d5ba6399b4238b2f29ee9deb97593d2bc")
new_carrot_data = File.read("/your-path-to/scripts/new_carrot")
tx8 = wallet.generate_tx("ckt1qyqvsv5240xeh85wvnau2eky8pwrhh4jr8ts8vyj37", 8000*10**8, fee: 8245*1000)
tx8.inputs[1] = CKB::Types::Input.new(previous_output: CKB::Types::OutPoint.new(tx_hash: "0xc66cd6fdabcdee803077f6aaeef84c058fb121e553426f67c71c94e7c5d24bb7", index: 0))

type_id_type = CKB::Types::Script.new(code_hash: "0x00000000000000000000000000000000000000000000000000545950455f4944", hash_type: "type", args: type_id_args)
tx8.outputs.first.type = type_id_type
tx8.outputs_data[0] = CKB::Utils.bin_to_hex(new_carrot_data)

tx8 = tx8.sign(wallet.key)
tx8_hash = api.send_transaction(tx8) #0x16657fe1d83bc435f3a869209e17e68ae5e4708e894e45c2b9e90ae48712f515

使用 Type ID 版本的 new carrot script

  1. carrot validate success
new_carrot_dep = CKB::Types::CellDep.new(out_point: CKB::Types::OutPoint.new(tx_hash: "0x16657fe1d83bc435f3a869209e17e68ae5e4708e894e45c2b9e90ae48712f515", index: 0))
carrot_type_script = CKB::Types::Script.new(code_hash: type_id_type.compute_hash, hash_type: "type", args: "0x")
api = CKB::API.new
wallet = CKB::Wallet.from_hex(api, "0xd00c06bfd800d27397002dca6fb0993d5ba6399b4238b2f29ee9deb97593d2bc")
tx9 = wallet.generate_tx("ckt1qyqvsv5240xeh85wvnau2eky8pwrhh4jr8ts8vyj37", 1000*10**8, fee: 1000)
tx9.cell_deps.push(new_carrot_dep.dup)
tx9.outputs[0].type = carrot_type_script
tx9 = tx9.sign(wallet.key)
tx9_hash = api.send_transaction(tx9)
  1. carrot validate failure
new_carrot_dep = CKB::Types::CellDep.new(out_point: CKB::Types::OutPoint.new(tx_hash: "0x16657fe1d83bc435f3a869209e17e68ae5e4708e894e45c2b9e90ae48712f515", index: 0))
carrot_type_script = CKB::Types::Script.new(code_hash: type_id_type.compute_hash, hash_type: "type", args: "0x")
api = CKB::API.new
wallet = CKB::Wallet.from_hex(api, "0xd00c06bfd800d27397002dca6fb0993d5ba6399b4238b2f29ee9deb97593d2bc")
tx10 = wallet.generate_tx("ckt1qyqvsv5240xeh85wvnau2eky8pwrhh4jr8ts8vyj37", 1000*10**8, fee: 1000)
tx10.cell_deps.push(new_carrot_dep.dup)
tx10.outputs[0].type = carrot_type_script
tx10.outputs_data[0] = CKB::Utils.bin_to_hex("carrot007")
tx10 = tx10.sign(wallet.key)
tx10_hash = api.send_transaction(tx10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment