Skip to content

Commit

Permalink
Merge pull request #303 from shaojunda/shaojunda-update-address-gener…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
shaojunda authored Apr 21, 2022
2 parents d9611c7 + 8848dbf commit 69cecd0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
42 changes: 19 additions & 23 deletions lib/ckb/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,7 @@ def initialize(script, mode: DEFAULT_MODE, version: CKB::Address::Version::CKB20
# payload = type(01) | code hash index(00) | pubkey blake160
# see https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0021-ckb-address-format/0021-ckb-address-format.md for more info.
def generate
unless CKB::ScriptHashType::TYPE == script.hash_type && script.has_args? && SHORT_PAYLOAD_AVAILABLE_ARGS_LEN.include?(CKB::Utils.hex_to_bin(script.args).bytesize)
return generate_full_payload_address
end

if SystemCodeHash::SECP256K1_BLAKE160_SIGHASH_ALL_TYPE_HASH == script.code_hash
generate_short_payload_singlesig_address
elsif SystemCodeHash::SECP256K1_BLAKE160_MULTISIG_ALL_TYPE_HASH == script.code_hash
generate_short_payload_multisig_address
elsif [SystemCodeHash::ANYONE_CAN_PAY_CODE_HASH_ON_LINA,
SystemCodeHash::ANYONE_CAN_PAY_CODE_HASH_ON_AGGRON].include?(script.code_hash)
generate_short_payload_anyone_can_pay_address
else
generate_full_payload_address
end
generate_full_payload_address
end

alias to_s generate
Expand All @@ -63,21 +50,15 @@ def self.prefix(mode: DEFAULT_MODE)
end
end

private

def short_payload(code_hash_index)
blake160_bin = CKB::Utils.hex_to_bin(script.args)
type = [SHORT_FORMAT].pack("H*")
code_hash_index = [code_hash_index].pack("H*")
type + code_hash_index + blake160_bin
end

# Generates short payload format address
# payload = type(01) | code hash index(00) | single_arg
# see https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0021-ckb-address-format/0021-ckb-address-format.md for more info.
#
# @return [String]
def generate_short_payload_singlesig_address
warn "[DEPRECATION] `generate_short_payload_singlesig_address` is no longer recommended."
return unless CKB::ScriptHashType::TYPE == script.hash_type && script.has_args? && SHORT_PAYLOAD_AVAILABLE_ARGS_LEN.include?(CKB::Utils.hex_to_bin(script.args).bytesize)

ConvertAddress.encode(prefix, short_payload(CODE_HASH_INDEX_SINGLESIG), Bech32::Encoding::BECH32)
end

Expand All @@ -87,6 +68,9 @@ def generate_short_payload_singlesig_address
#
# @return [String]
def generate_short_payload_multisig_address
warn "[DEPRECATION] `generate_short_payload_multisig_address` is no longer recommended."
return unless SystemCodeHash::SECP256K1_BLAKE160_MULTISIG_ALL_TYPE_HASH == script.code_hash

ConvertAddress.encode(prefix, short_payload(CODE_HASH_INDEX_MULTISIG_SIG), Bech32::Encoding::BECH32)
end

Expand All @@ -96,9 +80,21 @@ def generate_short_payload_multisig_address
#
# @return [String]
def generate_short_payload_anyone_can_pay_address
warn "[DEPRECATION] `generate_short_payload_anyone_can_pay_address` is no longer recommended."
return unless [SystemCodeHash::ANYONE_CAN_PAY_CODE_HASH_ON_LINA, SystemCodeHash::ANYONE_CAN_PAY_CODE_HASH_ON_AGGRON].include?(script.code_hash)

ConvertAddress.encode(prefix, short_payload(CODE_HASH_INDEX_ANYONE_CAN_PAY), Bech32::Encoding::BECH32)
end

private

def short_payload(code_hash_index)
blake160_bin = CKB::Utils.hex_to_bin(script.args)
type = [SHORT_FORMAT].pack("H*")
code_hash_index = [code_hash_index].pack("H*")
type + code_hash_index + blake160_bin
end

# Generates full payload format address
# payload = 0x02/0x04 | code_hash | args
# see https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0021-ckb-address-format/0021-ckb-address-format.md for more info.
Expand Down
20 changes: 10 additions & 10 deletions spec/ckb/address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,38 @@
it "generate short payload singlesig address" do
addr = CKB::Address.new(singlesig_script)
expect(
addr.to_s
addr.generate_short_payload_singlesig_address
).to eq "ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqswmu83"
end

it "generate short payload multisig address" do
address = CKB::Address.new(multisig_script)
expect(
address.generate
address.generate_short_payload_multisig_address
).to eq "ckt1qyqlqn8vsj7r0a5rvya76tey9jd2rdnca8lqh4kcuq"
end

it "generate short payload anyone can pay address without minimum limit" do
acp_lock = CKB::Types::Script.new(code_hash: CKB::SystemCodeHash::ANYONE_CAN_PAY_CODE_HASH_ON_AGGRON, hash_type: "type", args: "0x4fb2be2e5d0c1a3b8694f832350a33c1685d477a")
address = CKB::Address.new(acp_lock)
expect(
address.generate
address.generate_short_payload_anyone_can_pay_address
).to eq "ckt1qypylv479ewscx3ms620sv34pgeuz6zagaaq3xzhsz"
end

it "generate short payload anyone can pay address with ckb minimum limit" do
acp_lock = CKB::Types::Script.new(code_hash: CKB::SystemCodeHash::ANYONE_CAN_PAY_CODE_HASH_ON_AGGRON, hash_type: "type", args: "0x4fb2be2e5d0c1a3b8694f832350a33c1685d477a0c")
address = CKB::Address.new(acp_lock)
expect(
address.generate
address.generate_short_payload_anyone_can_pay_address
).to eq "ckt1qypylv479ewscx3ms620sv34pgeuz6zagaaqc9q8fqw"
end

it "generate short payload anyone can pay address with ckb and udt minimum limit" do
acp_lock = CKB::Types::Script.new(code_hash: CKB::SystemCodeHash::ANYONE_CAN_PAY_CODE_HASH_ON_AGGRON, hash_type: "type", args: "0x4fb2be2e5d0c1a3b8694f832350a33c1685d477a0c01")
address = CKB::Address.new(acp_lock)
expect(
address.generate
address.generate_short_payload_anyone_can_pay_address
).to eq "ckt1qypylv479ewscx3ms620sv34pgeuz6zagaaqcqgr072sz"
end

Expand Down Expand Up @@ -116,38 +116,38 @@
it "generate short payload singlesig address" do
addr = CKB::Address.new(singlesig_script, mode: CKB::MODE::MAINNET)
expect(
addr.to_s
addr.generate_short_payload_singlesig_address
).to eq "ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd"
end

it "generate short payload multisig address" do
address = CKB::Address.new(multisig_script, mode: CKB::MODE::MAINNET)
expect(
address.generate
address.generate_short_payload_multisig_address
).to eq "ckb1qyqlqn8vsj7r0a5rvya76tey9jd2rdnca8lq2sg8su"
end

it "generate short payload anyone can pay address without minimum limit" do
acp_lock = CKB::Types::Script.new(code_hash: CKB::SystemCodeHash::ANYONE_CAN_PAY_CODE_HASH_ON_LINA, hash_type: "type", args: "0x4fb2be2e5d0c1a3b8694f832350a33c1685d477a")
address = CKB::Address.new(acp_lock, mode: CKB::MODE::MAINNET)
expect(
address.generate
address.generate_short_payload_anyone_can_pay_address
).to eq "ckb1qypylv479ewscx3ms620sv34pgeuz6zagaaqvrugu7"
end

it "generate short payload anyone can pay address with ckb minimum limit" do
acp_lock = CKB::Types::Script.new(code_hash: CKB::SystemCodeHash::ANYONE_CAN_PAY_CODE_HASH_ON_LINA, hash_type: "type", args: "0x4fb2be2e5d0c1a3b8694f832350a33c1685d477a0c")
address = CKB::Address.new(acp_lock, mode: CKB::MODE::MAINNET)
expect(
address.generate
address.generate_short_payload_anyone_can_pay_address
).to eq "ckb1qypylv479ewscx3ms620sv34pgeuz6zagaaqcehzz9g"
end

it "generate short payload anyone can pay address with ckb and udt minimum limit" do
acp_lock = CKB::Types::Script.new(code_hash: CKB::SystemCodeHash::ANYONE_CAN_PAY_CODE_HASH_ON_LINA, hash_type: "type", args: "0x4fb2be2e5d0c1a3b8694f832350a33c1685d477a0c01")
address = CKB::Address.new(acp_lock, mode: CKB::MODE::MAINNET)
expect(
address.generate
address.generate_short_payload_anyone_can_pay_address
).to eq "ckb1qypylv479ewscx3ms620sv34pgeuz6zagaaqcqgzc5xlw"
end

Expand Down

0 comments on commit 69cecd0

Please sign in to comment.