Skip to content

Commit

Permalink
feat: support parse short payload acp address
Browse files Browse the repository at this point in the history
  • Loading branch information
shaojunda committed Nov 24, 2020
1 parent 9496fca commit f747a30
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/ckb/address.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
require "pry"

module CKB
class Address
attr_reader :script, :prefix, :mode
Expand Down
14 changes: 11 additions & 3 deletions lib/ckb/address_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def parse_address_type(format_type, code_hash_index = nil)
"SHORTSINGLESIG"
when Address::CODE_HASH_INDEX_MULTISIG_SIG
"SHORTMULTISIG"
when Address::CODE_HASH_INDEX_ANYONE_CAN_PAY
"SHORTANYONECANPAY"
else
raise InvalidCodeHashIndexError, "Invalid code hash index"
end
Expand All @@ -42,9 +44,9 @@ def parse_short_payload_address(decoded_prefix, data)
format_type = data[0].unpack("H*").first
code_hash_index = data[1].unpack("H*").first
mode = parse_mode(decoded_prefix)
code_hash = parse_code_hash(code_hash_index)
code_hash = parse_code_hash(code_hash_index, mode)
args = CKB::Utils.bin_to_hex(data.slice(2..-1))
if CKB::Utils.hex_to_bin(args).bytesize != 20
if code_hash_index != CKB::Address::CODE_HASH_INDEX_ANYONE_CAN_PAY && CKB::Utils.hex_to_bin(args).bytesize != 20
raise InvalidArgSizeError, "Short payload format address args bytesize must equal to 20"
end

Expand Down Expand Up @@ -77,12 +79,18 @@ def parse_hash_type(format_type)
end
end

def parse_code_hash(code_hash_index)
def parse_code_hash(code_hash_index, mode)
case code_hash_index
when Address::CODE_HASH_INDEX_SINGLESIG
SystemCodeHash::SECP256K1_BLAKE160_SIGHASH_ALL_TYPE_HASH
when Address::CODE_HASH_INDEX_MULTISIG_SIG
SystemCodeHash::SECP256K1_BLAKE160_MULTISIG_ALL_TYPE_HASH
when Address::CODE_HASH_INDEX_ANYONE_CAN_PAY
if mode == CKB::MODE::TESTNET
SystemCodeHash::ANYONE_CAN_PAY_CODE_HASH_ON_AGGRON
else
SystemCodeHash::ANYONE_CAN_PAY_CODE_HASH_ON_LINA
end
else
raise InvalidCodeHashIndexError, "Invalid code hash index"
end
Expand Down

0 comments on commit f747a30

Please sign in to comment.