nis-ruby 0.0.17 → 0.0.18

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 59a90524a4fe746f16a6fafe9d971f266d0b61e4
4
- data.tar.gz: 56759ab22da26c82ce149a160d0265e7ac49638c
3
+ metadata.gz: 02eded5cc96f4b7d764250ccb50ed847e86568ac
4
+ data.tar.gz: 8bba76f25a95ad6b7dd5ca5d72c4e2e325dae94a
5
5
  SHA512:
6
- metadata.gz: 1860f26e59c0bfe83b9f6aec5f904de94c65448dc7a8a286d92ab0506ad55a3f338985f9fb70a0465f945dbe4ceed7a85b55f9d87f6f60c1c6edaad3242eb4a8
7
- data.tar.gz: a6d46edcbf71a03f5fe9d5221912d49184ec841f3fc04630d8db97649bb0da28320e08d02ef7d3855ab17302c4a17cb2d0d08958b46d960b7300ff66c05b7a43
6
+ metadata.gz: c00ff152a4d0ebddb4d74045545afd2be1ee0d52f4d5304695214a4923d204a2c815059c3164d3c6f8de2ec428e04790d733aa6fcc4b76cecfc41860ab86eeee
7
+ data.tar.gz: ddf8898fa66aef0c3e6f17a870164a6ec4dbf4541a7dd0db4db19844109138b1362ff75071e8f7365bfdcd92018711d4c0d0586ce5f05e6b00246b5422275871
@@ -5,5 +5,5 @@ A_ADDRESS = 'TDPP2C4XQLMESBMCYGWN4NRAJAKZEYRV75KGYSOB'
5
5
 
6
6
  nis = Nis.new(host: '23.228.67.85')
7
7
 
8
- p nis.account_mosaic_definition_page(:address => A_ADDRESS)
8
+ p nis.account_mosaic_definition_page(address: A_ADDRESS)
9
9
  p nis.account_mosaic_owned(address: A_ADDRESS)
@@ -5,4 +5,4 @@ A_ADDRESS = 'TDPP2C4XQLMESBMCYGWN4NRAJAKZEYRV75KGYSOB'
5
5
 
6
6
  nis = Nis.new(host: '23.228.67.85')
7
7
 
8
- p nis.account_namespace_page(:address => A_ADDRESS)
8
+ p nis.account_namespace_page(address: A_ADDRESS)
@@ -0,0 +1,15 @@
1
+ require 'nis'
2
+ Nis.logger.level = Logger::DEBUG
3
+
4
+ nis = Nis.new(host: '23.228.67.85')
5
+
6
+ FIXTURES_PATH = File.expand_path('../../spec/fixtures', __FILE__)
7
+
8
+ tx_hash = '3d7d8a88768ea35f35a4607252ea7bb71fd0951b92a12dfab41c98333b029c9f'
9
+
10
+ tx = nis.transaction_get(hash: tx_hash)
11
+ apostille_hash = tx.transaction.message[:payload] # 'fe4e545902cde315617a435ebfd5fe8875d699e2f2363262f5'
12
+ file = File.open("#{FIXTURES_PATH}/nemLogoV2 -- Apostille TX 3d7d8a88768ea35f35a4607252ea7bb71fd0951b92a12dfab41c98333b029c9f -- Date 2017-10-04.png")
13
+
14
+ apa = Nis::ApostilleAudit.new(file, apostille_hash)
15
+ p apa.valid?
@@ -0,0 +1,29 @@
1
+ require 'nis'
2
+ Nis.logger.level = Logger::DEBUG
3
+
4
+ nis = Nis.new(host: '23.228.67.85')
5
+
6
+ FIXTURES_PATH = File.expand_path('../../spec/fixtures', __FILE__)
7
+
8
+ # sender
9
+ A_PRIVATE_KEY = '4ce5c8f9fce571db0d9ac1adf00b8d3ba0f078ed40835fd3d730a2f24b834214'
10
+
11
+ kp = Nis::Keypair.new(A_PRIVATE_KEY)
12
+
13
+ file = File.open("#{FIXTURES_PATH}/nemLogoV2.png")
14
+ ap = Nis::Apostille.new(kp, file, :sha1,
15
+ multisig: false,
16
+ private: false,
17
+ network: :testnet
18
+ )
19
+ tx = ap.transaction
20
+ p "Fee: #{tx.fee.to_i}"
21
+
22
+ req = Nis::Request::Announce.new(tx, kp)
23
+ res = nis.transaction_announce(req)
24
+
25
+ p "Message: #{res.message}"
26
+ p "TransactionHash: #{res.transaction_hash}"
27
+ p "ApostilleFormat: #{ap.apostille_format(res.transaction_hash)}"
28
+
29
+ FileUtils.cp(file.path, ap.apostille_format(res.transaction_hash))
data/lib/nis.rb CHANGED
@@ -11,6 +11,8 @@ require 'nis/struct'
11
11
  require 'nis/transaction'
12
12
  require 'nis/unit'
13
13
  require 'nis/error'
14
+ require 'nis/apostille'
15
+ require 'nis/apostille_audit'
14
16
 
15
17
  # API Ruby Wrapper for NEM Infrastructure Server
16
18
  class Nis
@@ -0,0 +1,104 @@
1
+ require 'digest/md5'
2
+ require 'digest/sha1'
3
+ require 'digest/sha2'
4
+ require 'digest/sha3'
5
+
6
+ class Nis::Apostille
7
+ CHECKSUM = 'fe4e5459'.freeze
8
+
9
+ # @param [Nis::Keypair] keypair
10
+ # @param [string] file - The file
11
+ # @param [symbol] hashing - An hashing type (md5, sha1, sha256, sha3-256, sha3-512)
12
+ # @param [boolean] multisig - true if transaction is multisig, false otherwise
13
+ # @param [boolean] private - true if apostille is private / transferable / updateable, false if public
14
+ def initialize(keypair, file, hashing = :sha256, multisig: false, private: false, network: :testnet)
15
+ @keypair = keypair
16
+ @file = file
17
+ @hashing = hashing
18
+ @multisig = multisig
19
+ @private = private
20
+ @network = network
21
+ end
22
+
23
+ def private?
24
+ @private
25
+ end
26
+
27
+ def multisig?
28
+ @multisig
29
+ end
30
+
31
+ def transaction
32
+ if private?
33
+ raise 'Not implemented private apostille.'
34
+ else
35
+ dedicated_address = apostille[:sink]
36
+ apostille_hash = calc_hash
37
+ end
38
+
39
+ Nis::Transaction::Transfer.new(dedicated_address, 0, apostille_hash)
40
+ end
41
+
42
+ def apostille_format(transaction_hash)
43
+ ext = File.extname(@file.path)
44
+ name = File.basename(@file.path, ext)
45
+ date = Date.today.strftime('%Y-%m-%d')
46
+ '%s -- Apostille TX %s -- Date %s%s' % [
47
+ name,
48
+ transaction_hash,
49
+ date,
50
+ ext
51
+ ]
52
+ end
53
+
54
+ private
55
+
56
+ def calc_hash
57
+ checksum = "#{CHECKSUM}#{hex_type}"
58
+ hashed = case @hashing
59
+ when /\Amd5\z/ then Digest::MD5.file(@file)
60
+ when /\Asha1\z/ then Digest::SHA1.file(@file)
61
+ when /\Asha256\z/ then Digest::SHA256.file(@file)
62
+ when /\Asha3-256\z/ then Digest::SHA3.file(@file, 256)
63
+ else Digest::SHA3.file(@file, 512)
64
+ end
65
+ checksum << hashed.hexdigest
66
+ end
67
+
68
+ def algo
69
+ case @hashing
70
+ when /\Amd5\z/ then 0x01
71
+ when /\Asha1\z/ then 0x02
72
+ when /\Asha256\z/ then 0x03
73
+ when /\Asha3-256\z/ then 0x08
74
+ when /\Asha3-512\z/ then 0x09
75
+ else raise "Undefined hashing: #{@hashing}"
76
+ end
77
+ end
78
+
79
+ def version
80
+ private? ? 0x80 : 0x00
81
+ end
82
+
83
+ def hex_type
84
+ '%02x' % (algo | version)
85
+ end
86
+
87
+ def apostille
88
+ if @network == :testnet
89
+ if private?
90
+ raise 'Not implemented private apostille.'
91
+ else
92
+ { private_key: nil,
93
+ sink: 'TC7MCY5AGJQXZQ4BN3BOPNXUVIGDJCOHBPGUM2GE' }
94
+ end
95
+ else
96
+ if private?
97
+ raise 'Not implemented private apostille.'
98
+ else
99
+ { private_key: nil,
100
+ sink: 'NCZSJHLTIMESERVBVKOW6US64YDZG2PFGQCSV23J' }
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,47 @@
1
+ require 'digest/md5'
2
+ require 'digest/sha1'
3
+ require 'digest/sha2'
4
+ require 'digest/sha3'
5
+
6
+ class Nis::ApostilleAudit
7
+ CHECKSUM = 'fe4e5459'.freeze
8
+
9
+ # @param [File] file
10
+ # @param [apostille_hash] Apostille formatted hash
11
+ def initialize(file, apostille_hash)
12
+ @file = file
13
+ @apostille_hash = apostille_hash
14
+ @checksum, @version, @algo, @hash = split_apostille_hash
15
+ end
16
+
17
+ def valid?
18
+ raise 'Not implemented private apostille' if private?
19
+ raise "Invalid checksum: #{@checksum}" unless @checksum == CHECKSUM
20
+ @hash == calc_hash
21
+ end
22
+
23
+ def private?
24
+ @version == 0x80
25
+ end
26
+
27
+ def split_apostille_hash
28
+ [ @apostille_hash[0, 8],
29
+ @apostille_hash[8, 1].to_i,
30
+ @apostille_hash[9, 1].to_i,
31
+ @apostille_hash[10, @apostille_hash.size] ]
32
+ end
33
+
34
+ private
35
+
36
+ def calc_hash
37
+ hashed = case @algo
38
+ when 0x01 then Digest::MD5.file(@file)
39
+ when 0x02 then Digest::SHA1.file(@file)
40
+ when 0x03 then Digest::SHA256.file(@file)
41
+ when 0x08 then Digest::SHA3.file(@file, 256)
42
+ when 0x09 then Digest::SHA3.file(@file, 512)
43
+ else raise "Undefined alog #{@algo}"
44
+ end
45
+ hashed.hexdigest
46
+ end
47
+ end
@@ -63,8 +63,8 @@ module Nis::Endpoint
63
63
 
64
64
  def account_transfers_direction(dir)
65
65
  case dir.to_s
66
- when /\Ai/ then :incoming
67
- when /\Ao/ then :outgoing
66
+ when /\Ai/ then :incoming
67
+ when /\Ao/ then :outgoing
68
68
  else :all
69
69
  end
70
70
  end
@@ -41,8 +41,8 @@ module Nis::Endpoint
41
41
 
42
42
  def debug_connections_direction(dir)
43
43
  case dir.to_s
44
- when /\Ai/ then :incoming
45
- when /\Ao/ then :outgoing
44
+ when /\Ai/ then :incoming
45
+ when /\Ao/ then :outgoing
46
46
  else raise "Undefined direction: #{dir}"
47
47
  end
48
48
  end
@@ -37,8 +37,8 @@ module Nis::Endpoint
37
37
 
38
38
  def local_account_transfers_direction(dir)
39
39
  case dir.to_s
40
- when /\Ai/ then :incoming
41
- when /\Ao/ then :outgoing
40
+ when /\Ai/ then :incoming
41
+ when /\Ao/ then :outgoing
42
42
  else :all
43
43
  end
44
44
  end
@@ -36,8 +36,8 @@ module Nis::Endpoint
36
36
 
37
37
  def node_peerlist_state(state)
38
38
  case state.to_s
39
- when 'active' then :active
40
- when 'reachable' then :reachable
39
+ when 'active' then :active
40
+ when 'reachable' then :reachable
41
41
  else :all
42
42
  end
43
43
  end
@@ -73,7 +73,7 @@ class Nis::Struct
73
73
  end
74
74
 
75
75
  def payload
76
- encrypted? ? value : value.unpack('H*').first
76
+ (value =~ /\Afe/ || encrypted?) ? value : value.unpack('H*').first
77
77
  end
78
78
 
79
79
  private
@@ -16,14 +16,14 @@ class Nis::Struct
16
16
  def self.build_transaction_struct(data)
17
17
  type = data[:type]
18
18
  klass = case type
19
- when 0x0101 then Nis::Struct::TransferTransaction
20
- when 0x0801 then Nis::Struct::ImportanceTransferTransaction
21
- when 0x1001 then Nis::Struct::MultisigAggregationModificationTransaction
22
- when 0x1002 then Nis::Struct::MultisigSignatureTransaction
23
- when 0x1004 then Nis::Struct::MultisigTransaction
24
- when 0x2001 then Nis::Struct::ProvisionNamespaceTransaction
25
- when 0x4001 then Nis::Struct::MosaicDefinitionCreationTransaction
26
- when 0x4002 then Nis::Struct::MosaicSupplyChangeTransaction
19
+ when 0x0101 then Nis::Struct::TransferTransaction
20
+ when 0x0801 then Nis::Struct::ImportanceTransferTransaction
21
+ when 0x1001 then Nis::Struct::MultisigAggregationModificationTransaction
22
+ when 0x1002 then Nis::Struct::MultisigSignatureTransaction
23
+ when 0x1004 then Nis::Struct::MultisigTransaction
24
+ when 0x2001 then Nis::Struct::ProvisionNamespaceTransaction
25
+ when 0x4001 then Nis::Struct::MosaicDefinitionCreationTransaction
26
+ when 0x4002 then Nis::Struct::MosaicSupplyChangeTransaction
27
27
  else raise "Not implemented entity type: #{type}"
28
28
  end
29
29
  klass.build(data)
@@ -13,11 +13,6 @@ module Nis::Util
13
13
 
14
14
  NEM_EPOCH = Time.utc(2015, 3, 29, 0, 6, 25, 0)
15
15
 
16
- APOSTILLE_SINK = {
17
- testnet: 'TC7MCY5AGJQXZQ4BN3BOPNXUVIGDJCOHBPGUM2GE',
18
- mainnet: 'NCZSJHLTIMESERVBVKOW6US64YDZG2PFGQCSV23J'
19
- }
20
-
21
16
  def self.parse_version(network, version)
22
17
  parse_network(network) | version
23
18
  end
@@ -1,3 +1,3 @@
1
1
  class Nis
2
- VERSION = '0.0.17'.freeze
2
+ VERSION = '0.0.18'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nis-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.17
4
+ version: 0.0.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoshiyuki Ieyama
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-01 00:00:00.000000000 Z
11
+ date: 2017-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -193,6 +193,8 @@ files:
193
193
  - examples/account/unconfirmed_transactions.rb
194
194
  - examples/account/unlock.rb
195
195
  - examples/account/unlocked.rb
196
+ - examples/apostille_audit.rb
197
+ - examples/apostille_create.rb
196
198
  - examples/block/at_public.rb
197
199
  - examples/block/get.rb
198
200
  - examples/debug/connections.rb
@@ -221,6 +223,8 @@ files:
221
223
  - examples/transactions/transfer_remote.rb
222
224
  - examples/util/deserialize.rb
223
225
  - lib/nis.rb
226
+ - lib/nis/apostille.rb
227
+ - lib/nis/apostille_audit.rb
224
228
  - lib/nis/client.rb
225
229
  - lib/nis/configuration.rb
226
230
  - lib/nis/endpoint.rb