glueby 0.5.2 → 0.6.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 70bc38595142acecb4510e2bae12054de9bf4d8da4ddacbe220be0dab7c76324
4
- data.tar.gz: 87f1bc6a29b3aa38a35aa929c1cf8cc5190ec768265b873cdebc7c4b47481996
3
+ metadata.gz: 6dfda968fd96c80d2e47dafcaba138721e4250a45193e3091e05f37816a2da5c
4
+ data.tar.gz: 53cb96baec24e35afb9a271496009b034bea26c013b1cc4aab6ee306acbfe3de
5
5
  SHA512:
6
- metadata.gz: c87104af19662964d241fbd132fa351b3919c7c4c3079fbb3a0b1f1a2275b24c400d07edc570f63875b3f5884a18bae2720519377ea17e6bda38bfd1717d0702
7
- data.tar.gz: aeae4f6edae4081df4288ee2ac35188abefefd8e240b2536447890de547f5b4136ed95caf8142fd4dc85b57947253221d641fde1c0fcd8a8fae1d22ae09ae420
6
+ metadata.gz: eb7e3fd70fdf180ef3442f269fba947a542986a588b12dac8ad42a5fb2e54b3ee97955c63ae16a6739105475834ad61de27fe6989e0a7fb2d045dcd0a9ad746e
7
+ data.tar.gz: 3ddc3b8eaab504a46337222577613082e44f6a8d047aa80b7084ea9030c39432fc9b763bf7cde25b3f2d122a49441411e327912215bd070d073349631bc9f421
@@ -2,10 +2,13 @@ class CreateTimestamp < ActiveRecord::Migration<%= migration_version %>
2
2
  def change
3
3
  create_table :glueby_timestamps<%= table_options %> do |t|
4
4
  t.string :txid
5
- t.integer :status
5
+ t.integer :status, null: false, default: 0
6
6
  t.string :content_hash
7
7
  t.string :prefix
8
8
  t.string :wallet_id
9
+ t.integer :timestamp_type, null: false, default: 0
10
+ t.string :p2c_address
11
+ t.string :payment_base
9
12
  end
10
13
  end
11
14
  end
@@ -4,15 +4,17 @@ module Glueby
4
4
  class Timestamp < ::ActiveRecord::Base
5
5
  include Glueby::Contract::Timestamp::Util
6
6
  enum status: { init: 0, unconfirmed: 1, confirmed: 2 }
7
+ enum timestamp_type: { simple: 0, trackable: 1 }
7
8
 
8
9
  # @param [Hash] attributes attributes which consist of:
9
10
  # - wallet_id
10
11
  # - content
11
12
  # - prefix(optional)
13
+ # - timestamp_type(optional)
12
14
  def initialize(attributes = nil)
13
15
  @content_hash = Tapyrus.sha256(attributes[:content]).bth
14
16
  super(wallet_id: attributes[:wallet_id], content_hash: @content_hash,
15
- prefix: attributes[:prefix] ? attributes[:prefix] : '', status: :init)
17
+ prefix: attributes[:prefix] ? attributes[:prefix] : '', status: :init, timestamp_type: attributes[:timestamp_type] || :simple)
16
18
  end
17
19
  end
18
20
  end
@@ -5,6 +5,7 @@ module Glueby
5
5
  class InsufficientTokens < StandardError; end
6
6
  class InvalidAmount < StandardError; end
7
7
  class InvalidTokenType < StandardError; end
8
+ class InvalidTimestampType < StandardError; end
8
9
  class TxAlreadyBroadcasted < StandardError; end
9
10
  class UnsupportedTokenType < StandardError; end
10
11
  class UnknownScriptPubkey < StandardError; end
@@ -9,6 +9,8 @@ module Glueby
9
9
  # Storing timestamp transaction to the blockchain enables everyone to verify that the data existed at that time and a user signed it.
10
10
  class Timestamp
11
11
  include Glueby::Contract::TxBuilder
12
+
13
+ P2C_DEFAULT_VALUE = 1_000
12
14
 
13
15
  autoload :Syncer, 'glueby/contract/timestamp/syncer'
14
16
 
@@ -16,9 +18,29 @@ module Glueby
16
18
  include Glueby::Internal::Wallet::TapyrusCoreWalletAdapter::Util
17
19
  module_function
18
20
 
19
- def create_txs(wallet, prefix, data, fee_estimator, utxo_provider)
21
+ # @param [Glueby::Wallet] wallet
22
+ # @param [Array] data The data to be used for generating pay-to-contract address
23
+ # @return [Array(String, String)]
24
+ # Return (pay-to-contract address, public key used for generating address)
25
+ def create_pay_to_contract_address(wallet, contents: nil)
26
+ pubkey = wallet.internal_wallet.create_pubkey.pubkey
27
+ # Calculate P + H(P || contents)G
28
+ group = ECDSA::Group::Secp256k1
29
+ p = Tapyrus::Key.new(pubkey: pubkey).to_point # P
30
+ commitment = Tapyrus.sha256(p.to_hex(true).htb + contents.join).bth.to_i(16) % group.order # H(P || contents)
31
+ point = p + group.generator.multiply_by_scalar(commitment) # P + H(P || contents)G
32
+ [Tapyrus::Key.new(pubkey: point.to_hex(true)).to_p2pkh, pubkey] # [p2c address, P]
33
+ end
34
+
35
+ def create_txs(wallet, prefix, data, fee_estimator, utxo_provider, type: :simple)
20
36
  txb = Tapyrus::TxBuilder.new
21
- txb.data(prefix + data)
37
+ if type == :simple
38
+ txb.data(prefix + data)
39
+ elsif type == :trackable
40
+ p2c_address, payment_base = create_pay_to_contract_address(wallet, contents: [prefix, data])
41
+ txb.pay(p2c_address, P2C_DEFAULT_VALUE)
42
+ end
43
+
22
44
  fee = fee_estimator.fee(dummy_tx(txb.build))
23
45
  if utxo_provider
24
46
  script_pubkey = Tapyrus::Script.parse_from_addr(wallet.internal_wallet.receive_address)
@@ -54,7 +76,7 @@ module Glueby
54
76
  end
55
77
 
56
78
  txb.fee(fee).change_address(wallet.internal_wallet.change_address)
57
- [funding_tx, wallet.internal_wallet.sign_tx(txb.build, prev_txs)]
79
+ [funding_tx, wallet.internal_wallet.sign_tx(txb.build, prev_txs), p2c_address, payment_base]
58
80
  end
59
81
 
60
82
  def get_transaction(tx)
@@ -65,6 +87,9 @@ module Glueby
65
87
 
66
88
  attr_reader :tx, :txid
67
89
 
90
+ # p2c_address and payment_base is used in `trackable` type
91
+ attr_reader :p2c_address, :payment_base
92
+
68
93
  # @param [String] content Data to be hashed and stored in blockchain.
69
94
  # @param [String] prefix prefix of op_return data
70
95
  # @param [Glueby::Contract::FeeEstimator] fee_estimator
@@ -72,21 +97,29 @@ module Glueby
72
97
  # - :sha256
73
98
  # - :double_sha256
74
99
  # - :none
75
- # @raise [Glueby::Contract::Errors::UnsupportedDigestType] if digest unsupport
100
+ # @param [Symbol] timestamp_type
101
+ # - :simple
102
+ # - :trackable
103
+ # @raise [Glueby::Contract::Errors::UnsupportedDigestType] if digest is unsupported
104
+ # @raise [Glueby::Contract::Errors::InvalidTimestampType] if timestamp_type is unsupported
76
105
  def initialize(
77
106
  wallet:,
78
107
  content:,
79
108
  prefix: '',
80
109
  fee_estimator: Glueby::Contract::FixedFeeEstimator.new,
81
110
  digest: :sha256,
82
- utxo_provider: nil
111
+ utxo_provider: nil,
112
+ timestamp_type: :simple
83
113
  )
84
114
  @wallet = wallet
85
115
  @content = content
86
116
  @prefix = prefix
87
117
  @fee_estimator = fee_estimator
118
+ raise Glueby::Contract::Errors::UnsupportedDigestType, "#{digest} is invalid digest, supported digest are :sha256, :double_sha256, and :none." unless [:sha256, :double_sha256, :none].include?(digest)
88
119
  @digest = digest
89
120
  @utxo_provider = utxo_provider
121
+ raise Glueby::Contract::Errors::InvalidTimestampType, "#{timestamp_type} is invalid type, supported types are :simple, and :trackable." unless [:simple, :trackable].include?(timestamp_type)
122
+ @timestamp_type = timestamp_type
90
123
  end
91
124
 
92
125
  # broadcast to Tapyrus Core
@@ -96,7 +129,7 @@ module Glueby
96
129
  def save!
97
130
  raise Glueby::Contract::Errors::TxAlreadyBroadcasted if @txid
98
131
 
99
- funding_tx, @tx = create_txs(@wallet, @prefix, digest_content, @fee_estimator, @utxo_provider)
132
+ funding_tx, @tx, @p2c_address, @payment_base = create_txs(@wallet, @prefix, digest_content, @fee_estimator, @utxo_provider, type: @timestamp_type)
100
133
  @wallet.internal_wallet.broadcast(funding_tx) if funding_tx
101
134
  @txid = @wallet.internal_wallet.broadcast(@tx)
102
135
  end
@@ -1,3 +1,3 @@
1
1
  module Glueby
2
- VERSION = "0.5.2"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -12,7 +12,7 @@ module Glueby
12
12
  timestamps.each do |t|
13
13
  begin
14
14
  wallet = Glueby::Wallet.load(t.wallet_id)
15
- funding_tx, tx = create_txs(wallet, t.prefix, t.content_hash, Glueby::Contract::FixedFeeEstimator.new, utxo_provider)
15
+ funding_tx, tx, p2c_address, payment_base = create_txs(wallet, t.prefix, t.content_hash, Glueby::Contract::FixedFeeEstimator.new, utxo_provider, type: t.timestamp_type.to_sym)
16
16
  if funding_tx
17
17
  ::ActiveRecord::Base.transaction do
18
18
  wallet.internal_wallet.broadcast(funding_tx)
@@ -21,7 +21,7 @@ module Glueby
21
21
  end
22
22
  ::ActiveRecord::Base.transaction do
23
23
  wallet.internal_wallet.broadcast(tx) do |tx|
24
- t.update(txid: tx.txid, status: :unconfirmed)
24
+ t.update(txid: tx.txid, status: :unconfirmed, p2c_address: p2c_address, payment_base: payment_base)
25
25
  end
26
26
  puts "timestamp tx was broadcasted (id=#{t.id}, txid=#{tx.txid})"
27
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glueby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - azuchi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-12-06 00:00:00.000000000 Z
11
+ date: 2021-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tapyrus