glueby 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-gemset +1 -1
- data/.ruby-version +1 -1
- data/.travis.yml +3 -2
- data/README.md +27 -17
- data/glueby.gemspec +1 -1
- data/lib/generators/glueby/{initializer_generator.rb → contract/initializer_generator.rb} +0 -0
- data/lib/generators/glueby/contract/templates/initializer.rb.erb +3 -0
- data/lib/generators/glueby/contract/templates/key_table.rb.erb +15 -0
- data/lib/generators/glueby/{templates → contract/templates}/timestamp_table.rb.erb +2 -1
- data/lib/generators/glueby/contract/templates/utxo_table.rb.erb +15 -0
- data/lib/generators/glueby/contract/templates/wallet_table.rb.erb +10 -0
- data/lib/generators/glueby/contract/timestamp_generator.rb +26 -0
- data/lib/generators/glueby/contract/wallet_adapter_generator.rb +46 -0
- data/lib/glueby.rb +18 -1
- data/lib/glueby/contract.rb +3 -14
- data/lib/glueby/contract/active_record/timestamp.rb +8 -5
- data/lib/glueby/contract/errors.rb +6 -0
- data/lib/glueby/contract/payment.rb +54 -0
- data/lib/glueby/contract/timestamp.rb +39 -38
- data/lib/glueby/contract/token.rb +193 -0
- data/lib/glueby/contract/tx_builder.rb +197 -31
- data/lib/glueby/generator.rb +5 -0
- data/lib/glueby/generator/migrate_generator.rb +38 -0
- data/lib/glueby/internal.rb +6 -0
- data/lib/glueby/internal/rpc.rb +35 -0
- data/lib/glueby/internal/wallet.rb +122 -0
- data/lib/glueby/internal/wallet/abstract_wallet_adapter.rb +131 -0
- data/lib/glueby/internal/wallet/active_record.rb +15 -0
- data/lib/glueby/internal/wallet/active_record/key.rb +72 -0
- data/lib/glueby/internal/wallet/active_record/utxo.rb +50 -0
- data/lib/glueby/internal/wallet/active_record/wallet.rb +54 -0
- data/lib/glueby/internal/wallet/active_record_wallet_adapter.rb +133 -0
- data/lib/glueby/internal/wallet/errors.rb +11 -0
- data/lib/glueby/internal/wallet/tapyrus_core_wallet_adapter.rb +158 -0
- data/lib/glueby/version.rb +1 -1
- data/lib/glueby/wallet.rb +51 -0
- data/lib/tasks/glueby/contract/timestamp.rake +5 -5
- data/lib/tasks/glueby/contract/wallet_adapter.rake +42 -0
- metadata +30 -10
- data/lib/generators/glueby/templates/initializer.rb.erb +0 -4
- data/lib/generators/glueby/timestamp_generator.rb +0 -57
- data/lib/glueby/contract/rpc.rb +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d988a55f8acc63e5c2abb8f30ab88030a19c8e42212e372a8eeea6a2c5f3b27d
|
4
|
+
data.tar.gz: 9cee6ba595dd3ba576d1190350396a4c55923c58620a529ec0ff55cc4249b69b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 610751093275239e15a70726033be9a3eb4ca65e65c1b86874f09471f4391fbc764463f76da36619b7429dac7c2b70b1f6596e2e3b2eacb397f11029018fb8e1
|
7
|
+
data.tar.gz: c48b89c3b4a61f652b051642420f1d8b3aa08ad3d90f8807ec8803f63d64e675a924a56990f42fc3d4201d6a90c99fa8101f423bbe66143ae735efdfae758cbf
|
data/.ruby-gemset
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
glueby
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
ruby-3.0.0
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Glueby
|
1
|
+
# Glueby [![Build Status](https://travis-ci.org/chaintope/glueby.svg?branch=master)](https://travis-ci.org/chaintope/glueby) [![Gem Version](https://badge.fury.io/rb/glueby.svg)](https://badge.fury.io/rb/glueby) [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE)
|
2
2
|
|
3
3
|
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/glueby`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
4
|
|
@@ -22,12 +22,19 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
+
Glueby has below features.
|
26
|
+
|
27
|
+
- [Timestamp](#Timestamp)
|
28
|
+
|
29
|
+
### Timestamp
|
30
|
+
|
25
31
|
```ruby
|
26
32
|
|
27
|
-
config = {schema: 'http', host: '127.0.0.1', port: 12381, user: 'user', password: 'pass'}
|
28
|
-
Glueby::
|
33
|
+
config = {adapter: 'core', schema: 'http', host: '127.0.0.1', port: 12381, user: 'user', password: 'pass'}
|
34
|
+
Glueby::Wallet.configure(config)
|
29
35
|
|
30
|
-
|
36
|
+
wallet = Glueby::Wallet.create
|
37
|
+
timestamp = Glueby::Contract::Timestamp.new(wallet: wallet, content: "\x01\x02\x03")
|
31
38
|
timestamp.save!
|
32
39
|
# "a01eace94ce6cdc30f389609de8a7584a4e208ee82fec33a2f5875b7cee47097"
|
33
40
|
|
@@ -89,26 +96,28 @@ We can see the timestamp transaction using getrawblockchain command
|
|
89
96
|
}
|
90
97
|
```
|
91
98
|
|
92
|
-
|
99
|
+
#### Rails support
|
93
100
|
|
94
|
-
|
101
|
+
Glueby supports ruby on rails integration.
|
95
102
|
|
96
103
|
To use in rails, Add dependency to Gemfile.
|
97
104
|
|
98
105
|
Then invoke install task.
|
106
|
+
|
99
107
|
```
|
100
108
|
bin/rails glueby:contract:install
|
101
109
|
```
|
102
110
|
|
103
111
|
Install task creates a file `glueby.rb` in `config/initializers` directory like this.
|
112
|
+
|
104
113
|
```
|
105
|
-
require 'tapyrus'
|
106
114
|
# Edit configuration for connection to tapyrus core
|
107
|
-
config = {schema: 'http', host: '127.0.0.1', port: 12381, user: 'user', password: 'pass'}
|
108
|
-
Glueby::
|
115
|
+
config = {adapter: 'core', schema: 'http', host: '127.0.0.1', port: 12381, user: 'user', password: 'pass'}
|
116
|
+
Glueby::Wallet.configure(config)
|
109
117
|
```
|
110
118
|
|
111
119
|
If you use timestamp feature, use `glueby:contract:timestamp` generator.
|
120
|
+
|
112
121
|
```
|
113
122
|
bin/rails g glueby:contract:timestamp
|
114
123
|
create db/migrate/20200613065511_create_timestamp.rb
|
@@ -122,13 +131,15 @@ bin/rails db:migrate
|
|
122
131
|
Now, Glueby::Contract::AR::Timestamp model is available
|
123
132
|
|
124
133
|
```ruby
|
125
|
-
irb(main):001:0>
|
126
|
-
|
127
|
-
|
128
|
-
|
134
|
+
irb(main):001:0> wallet = Glueby::Wallet.create
|
135
|
+
=> #<Glueby::Wallet:0x00007fe8333f7d98 @internal_wallet=#<Glueby::Internal::Wallet:0x00007fe8333f7dc0 @id="70a58204a7f4cb10d973b762f17fdb4b">>
|
136
|
+
irb(main):003:0> t = Glueby::Contract::AR::Timestamp.new(wallet_id: wallet.id, content:"\x01010101", prefix: "app")
|
137
|
+
(0.5ms) SELECT sqlite_version(*)
|
138
|
+
=> #<Glueby::Contract::AR::Timestamp id: nil, txid: nil, status: "init", content_hash: "9ccc644b03a88358a754962903a659a2d338767ee61674dde5...", prefix: "app", wallet_id: "70a58204a7f4cb10d973b762f17fdb4b">
|
139
|
+
irb(main):004:0> t.save
|
129
140
|
(0.1ms) begin transaction
|
130
|
-
Glueby::AR::
|
131
|
-
(2.
|
141
|
+
Glueby::Contract::AR::Timestamp Create (0.4ms) INSERT INTO "timestamps" ("status", "content_hash", "prefix", "wallet_id") VALUES (?, ?, ?, ?) [["status", 0], ["content_hash", "9ccc644b03a88358a754962903a659a2d338767ee61674dde5434702a6256e6d"], ["prefix", "app"], ["wallet_id", "70a58204a7f4cb10d973b762f17fdb4b"]]
|
142
|
+
(2.1ms) commit transaction
|
132
143
|
=> true
|
133
144
|
```
|
134
145
|
|
@@ -140,12 +151,12 @@ broadcasted (id=1, txid=8d602ca8ebdd50fa70b5ee6bc6351965b614d0a4843adacf9f43fedd
|
|
140
151
|
```
|
141
152
|
|
142
153
|
Run `glueby:contract:timestamp:confirm` task to confirm the transaction and update status(unconfirmed -> confirmded).
|
154
|
+
|
143
155
|
```
|
144
156
|
bin/rails glueby:contract:timestamp:confirm
|
145
157
|
confirmed (id=1, txid=8d602ca8ebdd50fa70b5ee6bc6351965b614d0a4843adacf9f43fedd7112fbf4)
|
146
158
|
```
|
147
159
|
|
148
|
-
|
149
160
|
## Development
|
150
161
|
|
151
162
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -156,7 +167,6 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
156
167
|
|
157
168
|
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/glueby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/tapyrus-contractrb/blob/master/CODE_OF_CONDUCT.md).
|
158
169
|
|
159
|
-
|
160
170
|
## License
|
161
171
|
|
162
172
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/glueby.gemspec
CHANGED
@@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
|
|
26
26
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
27
|
spec.require_paths = ["lib"]
|
28
28
|
|
29
|
-
spec.add_runtime_dependency 'tapyrus'
|
29
|
+
spec.add_runtime_dependency 'tapyrus', '>= 0.2.6'
|
30
30
|
spec.add_development_dependency 'activerecord'
|
31
31
|
spec.add_development_dependency 'sqlite3'
|
32
32
|
end
|
File without changes
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class CreateKey < ActiveRecord::Migration<%= migration_version %>
|
2
|
+
def change
|
3
|
+
create_table :keys<%= table_options %> do |t|
|
4
|
+
t.string :private_key
|
5
|
+
t.string :public_key
|
6
|
+
t.string :script_pubkey
|
7
|
+
t.integer :purpose
|
8
|
+
t.belongs_to :wallet
|
9
|
+
t.timestamps
|
10
|
+
end
|
11
|
+
|
12
|
+
add_index :keys, [:script_pubkey], unique: true
|
13
|
+
add_index :keys, [:private_key], unique: true
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class CreateUtxo < ActiveRecord::Migration<%= migration_version %>
|
2
|
+
def change
|
3
|
+
create_table :utxos<%= table_options %> do |t|
|
4
|
+
t.string :txid
|
5
|
+
t.integer :index
|
6
|
+
t.bigint :value
|
7
|
+
t.string :script_pubkey
|
8
|
+
t.integer :status
|
9
|
+
t.belongs_to :key, null: true
|
10
|
+
t.timestamps
|
11
|
+
end
|
12
|
+
|
13
|
+
add_index :utxos, [:txid, :index], unique: true
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Glueby
|
2
|
+
module Contract
|
3
|
+
class TimestampGenerator < Rails::Generators::Base
|
4
|
+
include ::Rails::Generators::Migration
|
5
|
+
include Glueby::Generator::MigrateGenerator
|
6
|
+
extend Glueby::Generator::MigrateGenerator::ClassMethod
|
7
|
+
|
8
|
+
source_root File.expand_path('templates', __dir__)
|
9
|
+
|
10
|
+
def create_migration_file
|
11
|
+
migration_dir = File.expand_path("db/migrate")
|
12
|
+
|
13
|
+
if self.class.migration_exists?(migration_dir, "create_timestamp")
|
14
|
+
::Kernel.warn "Migration already exists: create_timestamp"
|
15
|
+
else
|
16
|
+
migration_template(
|
17
|
+
"timestamp_table.rb.erb",
|
18
|
+
"db/migrate/create_timestamp.rb",
|
19
|
+
migration_version: migration_version,
|
20
|
+
table_options: table_options,
|
21
|
+
)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Glueby
|
2
|
+
module Contract
|
3
|
+
class WalletAdapterGenerator < Rails::Generators::Base
|
4
|
+
include ::Rails::Generators::Migration
|
5
|
+
include Glueby::Generator::MigrateGenerator
|
6
|
+
extend Glueby::Generator::MigrateGenerator::ClassMethod
|
7
|
+
|
8
|
+
source_root File.expand_path('templates', __dir__)
|
9
|
+
|
10
|
+
def create_migration_file
|
11
|
+
migration_dir = File.expand_path("db/migrate")
|
12
|
+
|
13
|
+
if self.class.migration_exists?(migration_dir, "create_wallet")
|
14
|
+
::Kernel.warn "Migration already exists: create_wallet"
|
15
|
+
else
|
16
|
+
migration_template(
|
17
|
+
"wallet_table.rb.erb",
|
18
|
+
"db/migrate/create_wallet.rb",
|
19
|
+
migration_version: migration_version,
|
20
|
+
table_options: table_options,
|
21
|
+
)
|
22
|
+
end
|
23
|
+
if self.class.migration_exists?(migration_dir, "create_key")
|
24
|
+
::Kernel.warn "Migration already exists: create_key"
|
25
|
+
else
|
26
|
+
migration_template(
|
27
|
+
"key_table.rb.erb",
|
28
|
+
"db/migrate/create_key.rb",
|
29
|
+
migration_version: migration_version,
|
30
|
+
table_options: table_options,
|
31
|
+
)
|
32
|
+
end
|
33
|
+
if self.class.migration_exists?(migration_dir, "create_utxo")
|
34
|
+
::Kernel.warn "Migration already exists: create_utxo"
|
35
|
+
else
|
36
|
+
migration_template(
|
37
|
+
"utxo_table.rb.erb",
|
38
|
+
"db/migrate/create_utxo.rb",
|
39
|
+
migration_version: migration_version,
|
40
|
+
table_options: table_options,
|
41
|
+
)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/glueby.rb
CHANGED
@@ -1,5 +1,22 @@
|
|
1
1
|
require "glueby/version"
|
2
|
+
require 'tapyrus'
|
2
3
|
|
3
4
|
module Glueby
|
4
5
|
autoload :Contract, 'glueby/contract'
|
5
|
-
|
6
|
+
autoload :Generator, 'glueby/generator'
|
7
|
+
autoload :Wallet, 'glueby/wallet'
|
8
|
+
autoload :Internal, 'glueby/internal'
|
9
|
+
|
10
|
+
begin
|
11
|
+
class Railtie < ::Rails::Railtie
|
12
|
+
rake_tasks do
|
13
|
+
load "tasks/glueby/contract.rake"
|
14
|
+
load "tasks/glueby/contract/timestamp.rake"
|
15
|
+
load "tasks/glueby/contract/wallet_adapter.rake"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
rescue
|
19
|
+
# Rake task is unavailable
|
20
|
+
puts "Rake task is unavailable"
|
21
|
+
end
|
22
|
+
end
|
data/lib/glueby/contract.rb
CHANGED
@@ -3,22 +3,11 @@ module Glueby
|
|
3
3
|
autoload :Errors, 'glueby/contract/errors'
|
4
4
|
autoload :FeeProvider, 'glueby/contract/fee_provider'
|
5
5
|
autoload :FixedFeeProvider, 'glueby/contract/fee_provider'
|
6
|
-
autoload :
|
6
|
+
autoload :Payment, 'glueby/contract/payment'
|
7
7
|
autoload :Timestamp, 'glueby/contract/timestamp'
|
8
|
+
autoload :Token, 'glueby/contract/token'
|
8
9
|
autoload :TxBuilder, 'glueby/contract/tx_builder'
|
9
|
-
autoload :WalletFeature, 'glueby/contract/wallet_feature'
|
10
10
|
autoload :AR, 'glueby/contract/active_record'
|
11
|
-
|
12
|
-
begin
|
13
|
-
class Railtie < ::Rails::Railtie
|
14
|
-
rake_tasks do
|
15
|
-
load "tasks/glueby/contract.rake"
|
16
|
-
load "tasks/glueby/contract/timestamp.rake"
|
17
|
-
end
|
18
|
-
end
|
19
|
-
rescue
|
20
|
-
# Rake task is unavailable
|
21
|
-
puts "Rake task is unavailable"
|
22
|
-
end
|
11
|
+
autoload :Wallet, 'glueby/contract/wallet'
|
23
12
|
end
|
24
13
|
end
|
@@ -5,11 +5,14 @@ module Glueby
|
|
5
5
|
include Glueby::Contract::Timestamp::Util
|
6
6
|
enum status: { init: 0, unconfirmed: 1, confirmed: 2 }
|
7
7
|
|
8
|
-
# @param [
|
9
|
-
#
|
10
|
-
|
11
|
-
|
12
|
-
|
8
|
+
# @param [Hash] attributes attributes which consist of:
|
9
|
+
# - wallet_id
|
10
|
+
# - content
|
11
|
+
# - prefix(optional)
|
12
|
+
def initialize(attributes = nil)
|
13
|
+
@content_hash = Tapyrus.sha256(attributes[:content]).bth
|
14
|
+
super(wallet_id: attributes[:wallet_id], content_hash: @content_hash,
|
15
|
+
prefix: attributes[:prefix] ? attributes[:prefix] : '', status: :init)
|
13
16
|
end
|
14
17
|
end
|
15
18
|
end
|
@@ -2,7 +2,13 @@ module Glueby
|
|
2
2
|
module Contract
|
3
3
|
module Errors
|
4
4
|
class InsufficientFunds < StandardError; end
|
5
|
+
class InsufficientTokens < StandardError; end
|
6
|
+
class InvalidAmount < StandardError; end
|
7
|
+
class InvalidTokenType < StandardError; end
|
5
8
|
class TxAlreadyBroadcasted < StandardError; end
|
9
|
+
class UnsupportedTokenType < StandardError; end
|
10
|
+
class UnknownScriptPubkey < StandardError; end
|
11
|
+
class UnsupportedDigestType < StandardError; end
|
6
12
|
end
|
7
13
|
end
|
8
14
|
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Glueby
|
4
|
+
module Contract
|
5
|
+
# This class can send TPC between wallets.
|
6
|
+
#
|
7
|
+
# Examples:
|
8
|
+
#
|
9
|
+
# sender = Glueby::Wallet.load("wallet_id")
|
10
|
+
# receiver = Glueby::Wallet.load("wallet_id")
|
11
|
+
# or
|
12
|
+
# Glueby::Wallet.create
|
13
|
+
#
|
14
|
+
# Balance of sender and receiver before send
|
15
|
+
# sender.balances[""]
|
16
|
+
# => 100_000(tapyrus)
|
17
|
+
# receiver.balances[""]
|
18
|
+
# => 0(tapyrus)
|
19
|
+
#
|
20
|
+
# Send
|
21
|
+
# Payment.transfer(sender: sender, receiver: receiver, amount: 10_000)
|
22
|
+
# sender.balances[""]
|
23
|
+
# => 90_000
|
24
|
+
# receiver.balances[""]
|
25
|
+
# => 10_000
|
26
|
+
#
|
27
|
+
class Payment
|
28
|
+
extend Glueby::Contract::TxBuilder
|
29
|
+
|
30
|
+
class << self
|
31
|
+
def transfer(sender:, receiver:, amount:, fee_provider: FixedFeeProvider.new)
|
32
|
+
raise Glueby::Contract::Errors::InvalidAmount unless amount.positive?
|
33
|
+
|
34
|
+
tx = Tapyrus::Tx.new
|
35
|
+
dummy_fee = fee_provider.fee(dummy_tx(tx))
|
36
|
+
|
37
|
+
sum, outputs = sender.internal_wallet.collect_uncolored_outputs(dummy_fee + amount)
|
38
|
+
fill_input(tx, outputs)
|
39
|
+
|
40
|
+
receiver_script = Tapyrus::Script.parse_from_addr(receiver.internal_wallet.receive_address)
|
41
|
+
tx.outputs << Tapyrus::TxOut.new(value: amount, script_pubkey: receiver_script)
|
42
|
+
|
43
|
+
fee = fee_provider.fee(tx)
|
44
|
+
|
45
|
+
fill_change_tpc(tx, sender, sum - fee - amount)
|
46
|
+
|
47
|
+
tx = sender.internal_wallet.sign_tx(tx)
|
48
|
+
|
49
|
+
Glueby::Internal::RPC.client.sendrawtransaction(tx.to_hex)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -11,59 +11,37 @@ module Glueby
|
|
11
11
|
include Glueby::Contract::TxBuilder
|
12
12
|
|
13
13
|
module Util
|
14
|
+
include Glueby::Internal::Wallet::TapyrusCoreWalletAdapter::Util
|
14
15
|
module_function
|
15
16
|
|
16
|
-
def create_tx(prefix,
|
17
|
+
def create_tx(wallet, prefix, data, fee_provider)
|
17
18
|
tx = Tapyrus::Tx.new
|
18
|
-
tx.outputs << Tapyrus::TxOut.new(value: 0, script_pubkey: create_script(prefix,
|
19
|
+
tx.outputs << Tapyrus::TxOut.new(value: 0, script_pubkey: create_script(prefix, data))
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
sum, outputs = collect_outputs(results, fee)
|
21
|
+
fee = fee_provider.fee(dummy_tx(tx))
|
22
|
+
sum, outputs = wallet.internal_wallet.collect_uncolored_outputs(fee)
|
23
23
|
fill_input(tx, outputs)
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
tx
|
25
|
+
fill_change_tpc(tx, wallet, sum - fee)
|
26
|
+
wallet.internal_wallet.sign_tx(tx)
|
28
27
|
end
|
29
28
|
|
30
|
-
def create_payload(prefix,
|
29
|
+
def create_payload(prefix, data)
|
31
30
|
payload = +''
|
32
31
|
payload << prefix
|
33
|
-
payload <<
|
32
|
+
payload << data
|
34
33
|
payload
|
35
34
|
end
|
36
35
|
|
37
|
-
def create_script(prefix,
|
36
|
+
def create_script(prefix, data)
|
38
37
|
script = Tapyrus::Script.new
|
39
38
|
script << Tapyrus::Script::OP_RETURN
|
40
|
-
script << create_payload(prefix,
|
39
|
+
script << create_payload(prefix, data)
|
41
40
|
script
|
42
41
|
end
|
43
42
|
|
44
|
-
def create_change_script
|
45
|
-
address = Glueby::Contract::RPC.client.getnewaddress
|
46
|
-
decoded = Tapyrus.decode_base58_address(address)
|
47
|
-
Tapyrus::Script.to_p2pkh(decoded[0])
|
48
|
-
end
|
49
|
-
|
50
|
-
def sign_tx(tx)
|
51
|
-
# TODO: Implement SignatureProvider
|
52
|
-
response = Glueby::Contract::RPC.client.signrawtransactionwithwallet(tx.to_payload.bth)
|
53
|
-
Tapyrus::Tx.parse_from_payload(response['hex'].htb)
|
54
|
-
end
|
55
|
-
|
56
|
-
def list_unspent
|
57
|
-
# TODO: Implement UtxoProvider
|
58
|
-
Glueby::Contract::RPC.client.listunspent(0, 999_999)
|
59
|
-
end
|
60
|
-
|
61
|
-
def broadcast_tx(tx)
|
62
|
-
Glueby::Contract::RPC.client.sendrawtransaction(tx.to_payload.bth)
|
63
|
-
end
|
64
|
-
|
65
43
|
def get_transaction(tx)
|
66
|
-
Glueby::
|
44
|
+
Glueby::Internal::RPC.client.getrawtransaction(tx.txid, 1)
|
67
45
|
end
|
68
46
|
end
|
69
47
|
include Glueby::Contract::Timestamp::Util
|
@@ -73,14 +51,23 @@ module Glueby
|
|
73
51
|
# @param [String] content Data to be hashed and stored in blockchain.
|
74
52
|
# @param [String] prefix prefix of op_return data
|
75
53
|
# @param [Glueby::Contract::FeeProvider] fee_provider
|
54
|
+
# @param [Symbol] digest type which select of:
|
55
|
+
# - :sha256
|
56
|
+
# - :double_sha256
|
57
|
+
# - :none
|
58
|
+
# @raise [Glueby::Contract::Errors::UnsupportedDigestType] if digest unsupport
|
76
59
|
def initialize(
|
60
|
+
wallet:,
|
77
61
|
content:,
|
78
62
|
prefix: '',
|
79
|
-
fee_provider: Glueby::Contract::FixedFeeProvider.new
|
63
|
+
fee_provider: Glueby::Contract::FixedFeeProvider.new,
|
64
|
+
digest: :sha256
|
80
65
|
)
|
66
|
+
@wallet = wallet
|
81
67
|
@content = content
|
82
68
|
@prefix = prefix
|
83
69
|
@fee_provider = fee_provider
|
70
|
+
@digest = digest
|
84
71
|
end
|
85
72
|
|
86
73
|
# broadcast to Tapyrus Core
|
@@ -90,9 +77,23 @@ module Glueby
|
|
90
77
|
def save!
|
91
78
|
raise Glueby::Contract::Errors::TxAlreadyBroadcasted if @txid
|
92
79
|
|
93
|
-
@tx = create_tx(@
|
94
|
-
@
|
95
|
-
|
80
|
+
@tx = create_tx(@wallet, @prefix, digest_content, @fee_provider)
|
81
|
+
@txid = @wallet.internal_wallet.broadcast(@tx)
|
82
|
+
end
|
83
|
+
|
84
|
+
private
|
85
|
+
|
86
|
+
def digest_content
|
87
|
+
case @digest&.downcase
|
88
|
+
when :sha256
|
89
|
+
Tapyrus.sha256(@content)
|
90
|
+
when :double_sha256
|
91
|
+
Tapyrus.double_sha256(@content)
|
92
|
+
when :none
|
93
|
+
@content
|
94
|
+
else
|
95
|
+
raise Glueby::Contract::Errors::UnsupportedDigestType
|
96
|
+
end
|
96
97
|
end
|
97
98
|
end
|
98
99
|
end
|