Module: OpenAssets::Util
- Extended by:
- Bitcoin::Util
- Includes:
- Bitcoin::Util
- 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
Instance Method Summary collapse
-
#address_to_oa_address(btc_address) ⇒ String
convert bitcoin address to open assets address.
-
#decode_leb128(value) ⇒ Object
LEB128 decode.
-
#encode_leb128(value) ⇒ Object
LEB128 encode.
-
#generate_asset_id(pub_key) ⇒ Object
generate asset ID from public key.
-
#oa_address_to_address(oa_address) ⇒ String
convert open assets address to bitcoin address.
- #pubkey_hash_to_asset_id(hash) ⇒ Object
-
#satoshi_to_coin(satoshi) ⇒ String
Convert satoshi to coin.
-
#script_to_address(script) ⇒ String
Get address from script.
- #to_bytes(string) ⇒ Object
-
#validate_address(addresses) ⇒ Object
validate bitcoin address.
Instance Method Details
#address_to_oa_address(btc_address) ⇒ String
convert bitcoin address to open assets address
15 16 17 18 19 20 21 22 |
# File 'lib/openassets/util.rb', line 15 def address_to_oa_address(btc_address) btc_hex = decode_base58(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 = checksum(named_addr) encode_base58(named_addr + oa_checksum) end |
#decode_leb128(value) ⇒ Object
LEB128 decode
57 58 59 60 61 62 |
# File 'lib/openassets/util.rb', line 57 def decode_leb128(value) mbs = to_bytes(value).map{|x|(x.to_i(16)>=128 ? x : x+"|")}.join.split('|') num=->a{(a.size==1 ? a[0] : (num[a[0..-2]]<<7)|a[-1])} r7=->n{to_bytes(n).map{|x|(x.to_i(16))&127}} mbs.map{|x|num[r7[x].reverse]} end |
#encode_leb128(value) ⇒ Object
LEB128 encode
49 50 51 52 53 54 |
# File 'lib/openassets/util.rb', line 49 def encode_leb128(value) d7=->n{(n>>7)==0 ? [n] : d7[n>>7]+[127 & n]} msb=->a{a0=a[0].to_s(16);[(a[0]< 16 ? "0"+a0 : a0)]+a[1..-1].map{|x|(x|128).to_s(16)}} leb128=->n{msb[d7[n]].reverse.join} leb128[value] end |
#generate_asset_id(pub_key) ⇒ Object
generate asset ID from public key.
35 36 37 |
# File 'lib/openassets/util.rb', line 35 def generate_asset_id(pub_key) pubkey_hash_to_asset_id(hash160(pub_key)) end |
#oa_address_to_address(oa_address) ⇒ String
convert open assets address to bitcoin address
27 28 29 30 31 32 |
# File 'lib/openassets/util.rb', line 27 def oa_address_to_address(oa_address) decode_address = decode_base58(oa_address) btc_addr = decode_address[2..-9] btc_checksum = checksum(btc_addr) encode_base58(btc_addr + btc_checksum) end |
#pubkey_hash_to_asset_id(hash) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/openassets/util.rb', line 39 def pubkey_hash_to_asset_id(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) script = hash160(["76", "a9", "14", hash, "88", "ac"].join) script = OA_VERSION_BYTE.to_s(16) + script # add version byte to script hash encode_base58(script + checksum(script)) # add checksum & encode end |
#satoshi_to_coin(satoshi) ⇒ String
Convert satoshi to coin.
71 72 73 |
# File 'lib/openassets/util.rb', line 71 def satoshi_to_coin(satoshi) "%.8f" % (satoshi / 100000000.0) end |
#script_to_address(script) ⇒ String
Get address from script.
78 79 80 81 82 83 84 |
# File 'lib/openassets/util.rb', line 78 def script_to_address(script) return script.get_pubkey_address if script.is_pubkey? return script.get_hash160_address if script.is_hash160? return script.get_multisig_addresses if script.is_multisig? return script.get_p2sh_address if script.is_p2sh? nil end |
#to_bytes(string) ⇒ Object
64 65 66 |
# File 'lib/openassets/util.rb', line 64 def to_bytes(string) string.each_char.each_slice(2).map{|v|v.join} end |
#validate_address(addresses) ⇒ Object
validate bitcoin address
87 88 89 90 91 |
# File 'lib/openassets/util.rb', line 87 def validate_address(addresses) addresses.each{|a| raise ArgumentError, "#{a} is invalid bitcoin address. " unless valid_address?(a) } end |