Module: OpenAssets::Util
- Extended by:
- Bitcoin::Util
- Includes:
- Bitcoin::Util
- Included in:
- OpenAssets, Api, Protocol::AssetDefinitionLoader, Protocol::MarkerOutput, Protocol::MarkerOutput, Protocol::TransactionOutput, Transaction::TransactionBuilder
- Defined in:
- lib/openassets/util.rb
Constant Summary collapse
- OA_NAMESPACE =
namespace of Open Asset
19- OA_VERSION_BYTE =
version byte for Open Assets Address
23- OA_VERSION_BYTE_TESTNET =
115
Instance Method Summary collapse
-
#address_to_oa_address(btc_address) ⇒ String
convert bitcoin address to open assets address.
-
#coin_to_satoshi(coin) ⇒ String
Convert coin unit to satoshi.
-
#decode_leb128(value) ⇒ Object
LEB128 decode.
-
#encode_leb128(value) ⇒ Object
LEB128 encode.
-
#generate_asset_id(pub_key) ⇒ Object
generate asset ID from public key.
- #hash_to_asset_id(hash) ⇒ Object
-
#oa_address_to_address(oa_address) ⇒ String
convert open assets address to bitcoin address.
- #pubkey_hash_to_asset_id(pubkey_hash) ⇒ Object
-
#read_leb128(data, offset = 0) ⇒ [Integer, Integer]
read leb128 value.
-
#read_var_integer(data, offset = 0) ⇒ [Integer, Integer]
read variable integer en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer.
-
#satoshi_to_coin(satoshi) ⇒ String
Convert satoshi to coin.
- #script_to_asset_id(script) ⇒ Object
- #to_bytes(string) ⇒ Object
-
#valid_asset_id?(asset_id) ⇒ Boolean
validate asset ID.
-
#validate_address(addresses) ⇒ Object
validate bitcoin address.
Instance Method Details
#address_to_oa_address(btc_address) ⇒ String
convert bitcoin address to open assets address
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/openassets/util.rb', line 17 def address_to_oa_address(btc_address) begin btc_hex = Bitcoin::Base58.decode(btc_address) btc_hex = '0' +btc_hex if btc_hex.size==47 address = btc_hex[0..-9] # bitcoin address without checksum named_addr = OA_NAMESPACE.to_s(16) + address oa_checksum = Bitcoin.calc_checksum(named_addr) Bitcoin::Base58.encode(named_addr + oa_checksum) rescue ArgumentError nil # bech32 format fails to decode. TODO define OA address for segwit end end |
#coin_to_satoshi(coin) ⇒ String
Convert coin unit to satoshi.
94 95 96 |
# File 'lib/openassets/util.rb', line 94 def coin_to_satoshi(coin) BigDecimal(coin) * BigDecimal(100000000) end |
#decode_leb128(value) ⇒ Object
LEB128 decode
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/openassets/util.rb', line 67 def decode_leb128(value) results = [] sio = StringIO.new value.htb.each_byte{|b| sio.putc(b) if b < 128 results << LEB128.decode_unsigned(sio) sio = StringIO.new end } results end |
#encode_leb128(value) ⇒ Object
LEB128 encode
62 63 64 |
# File 'lib/openassets/util.rb', line 62 def encode_leb128(value) LEB128.encode_unsigned(value).read.bth end |
#generate_asset_id(pub_key) ⇒ Object
generate asset ID from public key.
41 42 43 |
# File 'lib/openassets/util.rb', line 41 def generate_asset_id(pub_key) pubkey_hash_to_asset_id(hash160(pub_key)) end |
#hash_to_asset_id(hash) ⇒ Object
56 57 58 59 |
# File 'lib/openassets/util.rb', line 56 def hash_to_asset_id(hash) hash = oa_version_byte.to_s(16) + hash # add version byte to script hash Bitcoin::Base58.encode(hash + Bitcoin.calc_checksum(hash)) # add checksum & encode end |
#oa_address_to_address(oa_address) ⇒ String
convert open assets address to bitcoin address
33 34 35 36 37 38 |
# File 'lib/openassets/util.rb', line 33 def oa_address_to_address(oa_address) decode_address = Bitcoin::Base58.decode(oa_address) btc_addr = decode_address[2..-9] btc_checksum = Bitcoin.calc_checksum(btc_addr) Bitcoin::Base58.encode(btc_addr + btc_checksum) end |
#pubkey_hash_to_asset_id(pubkey_hash) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/openassets/util.rb', line 45 def pubkey_hash_to_asset_id(pubkey_hash) # gen P2PKH script hash # P2PKH script = OP_DUP OP_HASH160 <PubKeyHash> OP_EQUALVERIFY OP_CHECKSIG # (76=OP_DUP, a9=OP_HASH160, 14=Bytes to push, 88=OP_EQUALVERIFY, ac=OP_CHECKSIG) hash_to_asset_id(hash160(["76", "a9", "14", pubkey_hash, "88", "ac"].join)) end |
#read_leb128(data, offset = 0) ⇒ [Integer, Integer]
read leb128 value
140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/openassets/util.rb', line 140 def read_leb128(data, offset = 0) bytes = [data].pack('H*').bytes result = 0 shift = 0 while true return [nil, offset] if bytes.length < 1 + offset byte = bytes[offset..(offset + 1)][0] result |= (byte & 0x7f) << shift break if byte & 0x80 == 0 shift += 7 offset += 1 end [result, offset + 1] end |
#read_var_integer(data, offset = 0) ⇒ [Integer, Integer]
read variable integer en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/openassets/util.rb', line 119 def read_var_integer(data, offset = 0) raise ArgumentError, "data is nil." unless data packed = [data].pack('H*') return [nil, 0] if packed.bytesize < 1+offset bytes = packed.bytes[offset..(offset + 9)] # 9 is variable integer max storage length. first_byte = bytes[0] if first_byte < 0xfd [first_byte, offset + 1] elsif first_byte == 0xfd [calc_var_integer_val(bytes[1..2]), offset + 3] elsif first_byte == 0xfe [calc_var_integer_val(bytes[1..4]), offset + 5] elsif first_byte == 0xff [calc_var_integer_val(bytes[1..8]), offset + 9] end end |
#satoshi_to_coin(satoshi) ⇒ String
Convert satoshi to coin.
87 88 89 |
# File 'lib/openassets/util.rb', line 87 def satoshi_to_coin(satoshi) "%.8f" % (satoshi / 100000000.0) end |
#script_to_asset_id(script) ⇒ Object
52 53 54 |
# File 'lib/openassets/util.rb', line 52 def script_to_asset_id(script) hash_to_asset_id(hash160(script)) end |
#to_bytes(string) ⇒ Object
80 81 82 |
# File 'lib/openassets/util.rb', line 80 def to_bytes(string) string.each_char.each_slice(2).map{|v|v.join} end |
#valid_asset_id?(asset_id) ⇒ Boolean
validate asset ID
106 107 108 109 110 111 112 |
# File 'lib/openassets/util.rb', line 106 def valid_asset_id?(asset_id) return false if asset_id.nil? || asset_id.length != 34 decoded = Bitcoin::Base58.decode(asset_id) return false if decoded[0,2].to_i(16) != oa_version_byte p2pkh_script_hash = decoded[2..-9] p2pkh_script_hash.htb.bytesize == 20 end |
#validate_address(addresses) ⇒ Object
validate bitcoin address
99 100 101 102 103 |
# File 'lib/openassets/util.rb', line 99 def validate_address(addresses) addresses.each{|a| raise ArgumentError, "#{a} is invalid bitcoin address. " unless Bitcoin.valid_address?(a) } end |