openassets-ruby 0.3.3 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +15 -0
- data/lib/openassets/api.rb +21 -8
- data/lib/openassets/protocol/transaction_output.rb +3 -4
- data/lib/openassets/provider/bitcoin_core_provider.rb +1 -1
- data/lib/openassets/transaction/transaction_builder.rb +23 -0
- data/lib/openassets/util.rb +10 -0
- data/lib/openassets/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bcc95426e060b6c7e00a0e7e42ddb497ea4621d5
|
4
|
+
data.tar.gz: f50086936278851d11ed8136e7ab19a210f87efe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ea9d0902779030e3b9a80174bc5646c714f60904298d5a3196556920bd3eecf34ec6c805db40ae68c9f338c7c6a071c2f089b1c89ad34e559f81045183be35a
|
7
|
+
data.tar.gz: 94b2f561b2c7601faf9b7388253fa84baf32db56e203306d19d3541ff5a9afad3707cd5ad288b55bfda0aef1f38a782b908b569398d6c2cca14957bc15dc3aa3
|
data/README.md
CHANGED
@@ -215,6 +215,21 @@ Creates a transaction for sending **multiple** asset from the open asset address
|
|
215
215
|
tx = api.send_assets(from, params)
|
216
216
|
```
|
217
217
|
|
218
|
+
* **burn_asset**
|
219
|
+
Creates a transaction for burn asset.
|
220
|
+
This API is to burn the asset by spending the all UTXO of specified asset as Bitcoin.
|
221
|
+
```ruby
|
222
|
+
# burn_asset
|
223
|
+
# api.burn_asset(<from open asset address>, <asset ID>,, <fees (The fess in satoshis for the transaction. use 10000 satoshi if specified nil)>, <mode=('broadcast', 'signed', 'unsigned')>, <output_qty default value is 1.>)
|
224
|
+
|
225
|
+
# example
|
226
|
+
oa_address = 'bX2vhttomKj2fdd7SJV2nv8U4zDjusE5Y4B'
|
227
|
+
asset_id = 'oGu4VXx2TU97d9LmPP8PMCkHckkcPqC5RY'
|
228
|
+
tx = api.burn_asset(oa_address, asset_id, 10000)
|
229
|
+
```
|
230
|
+
|
231
|
+
**Note:** Burned asset will not be able to again get.
|
232
|
+
|
218
233
|
## Command line interface
|
219
234
|
|
220
235
|
Openassets-ruby comes with a `openassets` command line interface that allows easy interaction with OpenAssets.
|
data/lib/openassets/api.rb
CHANGED
@@ -93,10 +93,9 @@ module OpenAssets
|
|
93
93
|
# @return[Bitcoin::Protocol::Tx] The Bitcoin::Protocol::Tx object.
|
94
94
|
def issue_asset(from, amount, metadata = nil, to = nil, fees = nil, mode = 'broadcast', output_qty = 1)
|
95
95
|
to = from if to.nil?
|
96
|
-
builder = OpenAssets::Transaction::TransactionBuilder.new(@config[:dust_limit])
|
97
96
|
colored_outputs = get_unspent_outputs([oa_address_to_address(from)])
|
98
97
|
issue_param = OpenAssets::Transaction::TransferParameters.new(colored_outputs, to, from, amount, output_qty)
|
99
|
-
tx =
|
98
|
+
tx = create_tx_builder.issue_asset(issue_param, metadata, fees.nil? ? @config[:default_fees]: fees)
|
100
99
|
tx = process_transaction(tx, mode)
|
101
100
|
tx
|
102
101
|
end
|
@@ -112,10 +111,9 @@ module OpenAssets
|
|
112
111
|
# 'unsigned' for getting the raw unsigned transaction without broadcasting"""='broadcast'
|
113
112
|
# @return[Bitcoin::Protocol:Tx] The resulting transaction.
|
114
113
|
def send_asset(from, asset_id, amount, to, fees = nil, mode = 'broadcast', output_qty = 1)
|
115
|
-
builder = OpenAssets::Transaction::TransactionBuilder.new(@config[:dust_limit])
|
116
114
|
colored_outputs = get_unspent_outputs([oa_address_to_address(from)])
|
117
115
|
asset_transfer_spec = OpenAssets::Transaction::TransferParameters.new(colored_outputs, to, from, amount, output_qty)
|
118
|
-
tx =
|
116
|
+
tx = create_tx_builder.transfer_asset(asset_id, asset_transfer_spec, from, fees.nil? ? @config[:default_fees]: fees)
|
119
117
|
tx = process_transaction(tx, mode)
|
120
118
|
tx
|
121
119
|
end
|
@@ -129,12 +127,11 @@ module OpenAssets
|
|
129
127
|
# 'unsigned' for getting the raw unsigned transaction without broadcasting"""='broadcast'
|
130
128
|
# @return[Bitcoin::Protocol:Tx] The resulting transaction.
|
131
129
|
def send_assets(from, send_asset_params, fees = nil, mode = 'broadcast')
|
132
|
-
builder = OpenAssets::Transaction::TransactionBuilder.new(@config[:dust_limit])
|
133
130
|
colored_outputs = get_unspent_outputs([oa_address_to_address(from)])
|
134
131
|
transfer_specs = send_asset_params.map{|param|
|
135
132
|
[param.asset_id, OpenAssets::Transaction::TransferParameters.new(colored_outputs, param.to, from, param.amount)]
|
136
133
|
}
|
137
|
-
tx =
|
134
|
+
tx = create_tx_builder.transfer_assets(transfer_specs, from, fees.nil? ? @config[:default_fees]: fees)
|
138
135
|
tx = process_transaction(tx, mode)
|
139
136
|
tx
|
140
137
|
end
|
@@ -152,10 +149,22 @@ module OpenAssets
|
|
152
149
|
# @return[Bitcoin::Protocol:Tx] The resulting transaction.
|
153
150
|
def send_bitcoin(from, amount, to, fees = nil, mode = 'broadcast', output_qty = 1)
|
154
151
|
validate_address([from, to])
|
155
|
-
builder = OpenAssets::Transaction::TransactionBuilder.new(@config[:dust_limit])
|
156
152
|
colored_outputs = get_unspent_outputs([from])
|
157
153
|
btc_transfer_spec = OpenAssets::Transaction::TransferParameters.new(colored_outputs, to, from, amount, output_qty)
|
158
|
-
tx =
|
154
|
+
tx = create_tx_builder.transfer_btc(btc_transfer_spec, fees.nil? ? @config[:default_fees]: fees)
|
155
|
+
process_transaction(tx, mode)
|
156
|
+
end
|
157
|
+
|
158
|
+
# Creates a transaction for burn asset.
|
159
|
+
# @param[String] oa_address The open asset address to burn asset.
|
160
|
+
# @param[String] asset_id The asset ID identifying the asset to burn.
|
161
|
+
# @param[Integer] fees The fess in satoshis for the transaction.
|
162
|
+
# @param[String] mode 'broadcast' (default) for signing and broadcasting the transaction,
|
163
|
+
# 'signed' for signing the transaction without broadcasting,
|
164
|
+
# 'unsigned' for getting the raw unsigned transaction without broadcasting"""='broadcast'
|
165
|
+
def burn_asset(oa_address, asset_id, fees = nil, mode = 'broadcast')
|
166
|
+
unspents = get_unspent_outputs([oa_address_to_address(oa_address)])
|
167
|
+
tx = create_tx_builder.burn_asset(unspents, asset_id, fees.nil? ? @config[:default_fees]: fees)
|
159
168
|
process_transaction(tx, mode)
|
160
169
|
end
|
161
170
|
|
@@ -309,6 +318,10 @@ module OpenAssets
|
|
309
318
|
end
|
310
319
|
end
|
311
320
|
|
321
|
+
def create_tx_builder
|
322
|
+
OpenAssets::Transaction::TransactionBuilder.new(@config[:dust_limit])
|
323
|
+
end
|
324
|
+
|
312
325
|
end
|
313
326
|
|
314
327
|
end
|
@@ -54,7 +54,6 @@ module OpenAssets
|
|
54
54
|
|
55
55
|
# convert to hash object.
|
56
56
|
def to_hash
|
57
|
-
address = script_to_address(@script)
|
58
57
|
{
|
59
58
|
'address' => address,
|
60
59
|
'oa_address' => address.nil? ? nil : address_to_oa_address(address),
|
@@ -63,14 +62,14 @@ module OpenAssets
|
|
63
62
|
'asset_id' => @asset_id,
|
64
63
|
'asset_quantity' => @asset_quantity.to_s,
|
65
64
|
'asset_amount' => asset_amount.to_s,
|
66
|
-
'account' => account,
|
65
|
+
'account' => @account,
|
67
66
|
'asset_definition_url' => @asset_definition_url,
|
68
67
|
'proof_of_authenticity' => proof_of_authenticity
|
69
68
|
}
|
70
69
|
end
|
71
70
|
|
72
|
-
def
|
73
|
-
@
|
71
|
+
def address
|
72
|
+
script_to_address(@script)
|
74
73
|
end
|
75
74
|
|
76
75
|
private
|
@@ -106,7 +106,7 @@ module OpenAssets
|
|
106
106
|
:id => 'jsonrpc'
|
107
107
|
}
|
108
108
|
RestClient.post(server_url, data.to_json, content_type: :json) do |respdata, request, result|
|
109
|
-
response = JSON.parse(respdata)
|
109
|
+
response = JSON.parse(respdata.gsub(/\\u([\da-fA-F]{4})/) { [$1].pack('H*').unpack('n*').pack('U*').encode('ISO-8859-1').force_encoding('UTF-8') })
|
110
110
|
raise ApiError, response['error'] if response['error']
|
111
111
|
response['result']
|
112
112
|
end
|
@@ -65,6 +65,29 @@ module OpenAssets
|
|
65
65
|
transfer([], btc_transfer_spec, fees)
|
66
66
|
end
|
67
67
|
|
68
|
+
# Create a transaction for burn asset
|
69
|
+
def burn_asset(unspents, asset_id, fee)
|
70
|
+
tx = Bitcoin::Protocol::Tx.new
|
71
|
+
targets = unspents.select{|o|o.output.asset_id == asset_id}
|
72
|
+
raise TransactionBuildError.new('There is no asset.') if targets.length == 0
|
73
|
+
total_amount = targets.inject(0){|sum, o|o.output.value + sum}
|
74
|
+
otsuri = total_amount - fee
|
75
|
+
if otsuri < @amount
|
76
|
+
uncolored_outputs, uncolored_amount =
|
77
|
+
TransactionBuilder.collect_uncolored_outputs(unspents, @amount - otsuri)
|
78
|
+
targets = targets + uncolored_outputs
|
79
|
+
otsuri += uncolored_amount
|
80
|
+
end
|
81
|
+
targets.each{|o|
|
82
|
+
script_sig = o.output.script.to_binary
|
83
|
+
tx_in = Bitcoin::Protocol::TxIn.from_hex_hash(o.out_point.hash, o.out_point.index)
|
84
|
+
tx_in.script_sig = script_sig
|
85
|
+
tx.add_in(tx_in)
|
86
|
+
}
|
87
|
+
tx.add_out(create_uncolored_output(targets[0].output.address, otsuri))
|
88
|
+
tx
|
89
|
+
end
|
90
|
+
|
68
91
|
# collect uncolored outputs in unspent outputs(contains colored output).
|
69
92
|
# @param [Array[OpenAssets::Transaction::SpendableOutput]] unspent_outputs The Array of available outputs.
|
70
93
|
# @param [Integer] amount The amount to collect.
|
data/lib/openassets/util.rb
CHANGED
@@ -99,6 +99,16 @@ module OpenAssets
|
|
99
99
|
}
|
100
100
|
end
|
101
101
|
|
102
|
+
# validate asset ID
|
103
|
+
def valid_asset_id?(asset_id)
|
104
|
+
return false if asset_id.nil? || asset_id.length != 34
|
105
|
+
decoded = decode_base58(asset_id)
|
106
|
+
return false if decoded[0,2].to_i(16) != oa_version_byte
|
107
|
+
p2pkh_script_hash = decoded[2..-9]
|
108
|
+
address = hash160_to_address(p2pkh_script_hash)
|
109
|
+
valid_address?(address)
|
110
|
+
end
|
111
|
+
|
102
112
|
# generate Asset ID from open asset address.
|
103
113
|
def oa_address_to_asset_id(oa_address)
|
104
114
|
address_to_asset_id(oa_address_to_address(oa_address))
|
data/lib/openassets/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: openassets-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- azuchi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02-
|
11
|
+
date: 2016-02-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bitcoin-ruby
|