icap 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/icap.rb +109 -0
  3. metadata +60 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 19d8d789aacb1352ceb997b33d2ee570ec2865c8eb1556ba3c7741e3f9565b9c
4
+ data.tar.gz: 67ce7e0f3e81d2ebbf86aa1921e8ef88781225c991abb7ccd0539c0481e1b111
5
+ SHA512:
6
+ metadata.gz: 2b0cb0ddb6004feefbb7b0bdc2b6529c42ace472d1516667e0d558ac79e4aef9c8bf5b64a3ffaecc6832c7897e9059eeaa97179ba89ca862cf95a23be37b60a0
7
+ data.tar.gz: 960fcf1b5d83a8a58480119ffdac684e179b3ab2a9ffc47e04ce40ac233a8646171d2438a666207b9e954bf0465d695d9554661351777d3f19a90b38ef968abc
data/lib/icap.rb ADDED
@@ -0,0 +1,109 @@
1
+
2
+
3
+ module ICAP
4
+ require "base36"
5
+
6
+ # Calculates the checksum of an IBAN
7
+ #
8
+ # Example:
9
+ # >> ICAP::calculate_iban_checksum "XE", "09JD3UD9BFMRCRDS1IC6BS5MTAQ38"
10
+ # => 43
11
+ #
12
+ # Arguments:
13
+ # country_code: (String)
14
+ # bban: (String)
15
+ def self.calculate_iban_checksum country_code, bban
16
+ checksum = "00"
17
+
18
+ prepared_iban = "#{bban}#{country_code}#{checksum}"
19
+ number = ""
20
+ prepared_iban.each_char { |c|
21
+ if ('A'..'Z').to_a.include? c
22
+ number += (c.ord - "A".ord + 10).to_s
23
+ else
24
+ number += c
25
+ end
26
+ }
27
+ checksum = 98 - (number.to_i % 97)
28
+
29
+ return checksum
30
+ end
31
+
32
+ # Calculates the BBAN of an ICAP from the Ethereum address
33
+ #
34
+ # Example:
35
+ # >> ICAP::calculate_bban "0x000072B1A5F4864C2BA2b14471e48Cc118264794"
36
+ # => "9JD3UD9BFMRCRDS1IC6BS5MTAQ38"
37
+ #
38
+ # Arguments:
39
+ # address: (String)
40
+ def self.calculate_bban address
41
+ int = address.delete_prefix("0x").to_i 16
42
+ bban = Base36::encode int
43
+ bban.upcase!
44
+
45
+ return bban
46
+ end
47
+
48
+ # Calculates the ICAP from BBAN
49
+ #
50
+ # Example:
51
+ # >> ICAP::calculate_icap ICAP::calculate_bban "0x000072B1A5F4864C2BA2b14471e48Cc118264794"
52
+ # => "XE43 9JD3 UD9B FMRC RDS1 IC6B S5MT AQ38"
53
+ #
54
+ # Arguments:
55
+ # bban: (String)
56
+ def self.calculate_icap bban
57
+ country_code = "XE"
58
+ checksum = calculate_iban_checksum country_code, bban
59
+
60
+ result = "#{country_code}#{checksum}#{bban}"
61
+ result.gsub!(/(.{4})(?=.)/, '\1 \2')
62
+
63
+ return result
64
+ end
65
+
66
+ # Extracts the Ethereum address from an ICAP. The checksum is not checked.
67
+ #
68
+ # Example:
69
+ # >> ICAP::extract_address "XE43 9JD3 UD9B FMRC RDS1 IC6B S5MT AQ38"
70
+ # => "0x000072B1A5F4864C2BA2b14471e48Cc118264794"
71
+ #
72
+ # Arguments:
73
+ # icap: (String)
74
+ def self.extract_address icap
75
+ iban_ws = icap.delete " " # iban_without_spaces = iban_ws
76
+ bban = iban_ws[4..-1]
77
+ int = Base36.decode bban.downcase
78
+ address_part = int.to_i.to_s 16
79
+
80
+ return "0x#{"0" * (40 - address_part.length)}#{address_part}"
81
+ end
82
+
83
+ # Checks the checksum.
84
+ #
85
+ # Example:
86
+ # >> ICAP::check_checksum? "XE43 9JD3 UD9B FMRC RDS1 IC6B S5MT AQ38"
87
+ # => true
88
+ #
89
+ # Arguments:
90
+ # iban: (String)
91
+ def self.check_checksum? iban
92
+ iban_ws = iban.delete " " # iban_without_spaces = iban_ws
93
+ prepared_iban = "#{iban_ws[4..-1]}#{iban_ws[0...4]}"
94
+
95
+ number = ""
96
+ prepared_iban.each_char { |c|
97
+ if ('A'..'Z').to_a.include? c
98
+ number += (c.ord - "A".ord + 10).to_s
99
+ else
100
+ number += c
101
+ end
102
+ }
103
+
104
+ result = number.to_i % 97
105
+
106
+ return result == 1
107
+ end
108
+ end
109
+
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: icap
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Marek Kuethe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-12-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: base36
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.0
27
+ description: Gem, which can create Inter Exchange Client Address Protocol (ICAP) addresses
28
+ from Ethereum addresses. It can also check the checksum of an ICAP address. It can
29
+ also restore the Ethereum address from an ICAP address.
30
+ email: m.k@mk16.de
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - lib/icap.rb
36
+ homepage: https://rubygems.org/gems/ICAP
37
+ licenses:
38
+ - GPL-3.0-or-later
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 3.2.32
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Convert Ethereum addresses and Inter Exchange Client Address Protocol (ICAP)
59
+ addresses.
60
+ test_files: []