crypto_commons 0.0.0 → 0.0.1

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: daa051041dee70a7c016e1e6e26756b303f303dd3601ca8acbd243e957c22783
4
- data.tar.gz: d03048e497d66869cd6d4df3ecce4cadfc1fc2e7703ed6192ccc2bfe8e36c352
3
+ metadata.gz: 04d6d3313a8b2638f812b43efc8f95832f36a8e490ceaf80045fdca6cdcae958
4
+ data.tar.gz: 235cf0090ecdce0b4cf5f4753f28864b1411e3b097dc7bcded196c9f87fe49e8
5
5
  SHA512:
6
- metadata.gz: ef4718db8ab346d45100a8d743eb9344c5f29a796d286bc961932fe5fab5f1ba7b530351bc72b3975026b8bfe08d7240a9fac63e5556db8811b82c4ed9b7193d
7
- data.tar.gz: fed6f3292381d3a2d4bb71e29856f810010eade01c5e254235933d605e0aa02b05b9e751a4447b9916ee3fc4820c29c5f0d9c5b4edd2cea5cff517e727e891a3
6
+ metadata.gz: bf377e4b133044f326d68e5691dffd8508ff0e8948310671c85a1d40c47c168432f5edc2f15faa9418fe17166f32ad6e6b4f6bd9660e96c2483229d9e18f2bd8
7
+ data.tar.gz: 74b8920525534fefc0dc5fad45b502d9c1fd15fc773a6ca96ad0157b5346fb829f47ff58f73139d4210dbb6c4aaa4d9984df05d4e50e6870cf8e2bee4c0f47ef
data/CHANGELOG.md CHANGED
@@ -1,5 +1,5 @@
1
1
  ## [Unreleased]
2
2
 
3
- ## [0.0.0] - 2021-12-21
3
+ ## [0.0.1] - 2021-12-21
4
4
 
5
- - Initial release
5
+ - Added some util methods to get publickey, address, wif of a given private key.
data/lib/base58.rb ADDED
@@ -0,0 +1,23 @@
1
+ module Base58
2
+ #Base58base58_encode('000000000000000000000000000000000000000000000001a838b13505b26867')
3
+ def self.base58_encode(input)
4
+ @chars = %w[
5
+ 1 2 3 4 5 6 7 8 9
6
+ A B C D E F G H J K L M N P Q R S T U V W X Y Z
7
+ a b c d e f g h i j k m n o p q r s t u v w x y z
8
+ ]
9
+ @base = @chars.length
10
+
11
+ i = input.to_i(16)
12
+ buffer = String.new
13
+
14
+ while i > 0
15
+ remainder = i % @base
16
+ i = i / @base
17
+ buffer = @chars[remainder] + buffer
18
+ end
19
+
20
+ leading_zero_bytes = (input.match(/^([0]+)/) ? $1 : '').size / 2
21
+ ("1"*leading_zero_bytes) + buffer
22
+ end
23
+ end
data/lib/checksum.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'digest'
2
+
3
+ module CheckSum
4
+ def self.double_hash256(hex)
5
+ binary = [hex].pack("H*")
6
+ hash1 = Digest::SHA256.digest(binary)
7
+ hash2 = Digest::SHA256.digest(hash1)
8
+ result = hash2.unpack("H*")[0]
9
+ return result
10
+ end
11
+
12
+ def self.checksum(hex)
13
+ hash = double_hash256(hex) # Hash the data through SHA256 twice
14
+ return hash[0...8] # Return the first 4 bytes (8 characters)
15
+ end
16
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CryptoCommons
4
- VERSION = "0.0.0"
4
+ VERSION = "0.0.1"
5
5
  end
@@ -1,9 +1,55 @@
1
1
  # frozen_string_literal: true
2
+ require 'digest'
2
3
 
3
4
  require_relative "crypto_commons/version"
5
+ require_relative "./base58"
6
+ require_relative "./checksum"
7
+ require_relative "./elliptic_curve"
4
8
 
5
9
  module CryptoCommons
6
- def self.hello
7
- return "Hello World"
10
+ #Private key to Wallet Import Format, takes a Hex input
11
+ def self.wif(private_key)
12
+ extended = "80" + private_key + "01"
13
+ extendedchecksum = extended + CheckSum.checksum(extended)
14
+ wif = Base58.base58_encode(extendedchecksum)
15
+ end
16
+
17
+ #Private Key To Public Key, takes an Integer as input
18
+ #CryptoCommons.to_public_key(76)
19
+ def self.public_key(private_key)
20
+ point = EllipticCurve.multiply(private_key, $g)
21
+
22
+ # convert to hexadecimal
23
+ x = point[:x].to_s(16).rjust(64, "0")
24
+ y = point[:y].to_s(16).rjust(64, "0")
25
+
26
+ # compressed public key format
27
+ if (point[:y] % 2 == 0)
28
+ prefix = "02"
29
+ else
30
+ prefix = "03"
31
+ end
32
+
33
+ prefix + x
34
+ end
35
+
36
+ #Private Key To Public Key Hash, takes an Integer as input
37
+ #CryptoCommons.to_public_key_hash160(76)
38
+ def self.public_key_hash160(private_key)
39
+ publickey = public_key(private_key)
40
+ binary = [publickey].pack("H*")
41
+ sha256 = Digest::SHA256.digest(binary)
42
+ ripemd160 = Digest::RMD160.digest(sha256)
43
+ return ripemd160.unpack("H*")[0]
44
+ end
45
+
46
+ def self.address(private_key)
47
+ hash160 = public_key_hash160(private_key)
48
+
49
+ prefix = "00"
50
+ checksum = CheckSum.checksum(prefix + hash160)
51
+ address = Base58.base58_encode(prefix + hash160 + checksum)
52
+
53
+ return address
8
54
  end
9
55
  end
@@ -0,0 +1,80 @@
1
+ module EllipticCurve
2
+ #copied
3
+ #y^2 = x^3 + ax + b
4
+ $a = 0
5
+ $b = 7
6
+
7
+ #prime modulus
8
+ $p = 2 ** 256 - 2 ** 32 - 2 ** 9 - 2 ** 8 - 2 ** 7 - 2 ** 6 - 2 ** 4 - 1
9
+
10
+ #number of points on the curve
11
+ $n = 115792089237316195423570985008687907852837564279074904382605163141518161494337
12
+
13
+ #generator point (the starting point on the curve used for all calculations)
14
+ $g = {
15
+ x: 55066263022277343669578718895168534326250603453777594175500187360389116729240,
16
+ y: 32670510020758816978083085130507043184471273380659243275938904335757337482424,
17
+ }
18
+
19
+ #Modular Inverse.
20
+ def self.modinv(a, m = $p)
21
+ a = a % m if a < 0
22
+ prevy, y = 0, 1
23
+ while a > 1
24
+ q = m / a
25
+ y, prevy = prevy - q * y, y
26
+ a, m = m % a, a
27
+ end
28
+ return y
29
+ end
30
+
31
+ #Double - Add a point on the curve to itself.
32
+ def self.double(point)
33
+ #slope = (3x^2 + a) / 2y
34
+ slope = ((3 * point[:x] ** 2) * modinv((2 * point[:y]))) % $p # using modular inverse to perform "division"
35
+
36
+ #new x = slope^2 - 2x
37
+ x = (slope ** 2 - (2 * point[:x])) % $p
38
+ #new y = slope * (x - new x) * y
39
+ y = (slope * (point[:x] - x) - point[:y]) % $p
40
+
41
+ return { x: x, y: y }
42
+ end
43
+
44
+ #Add - Add two points together.
45
+ def self.add(point1, point2)
46
+ #double if both points are the same
47
+ return double(point1) if point1 == point2
48
+
49
+ #slope = (y1 - y2) / (x1 - x2)
50
+ slope = ((point1[:y] - point2[:y]) * modinv(point1[:x] - point2[:x])) % $p
51
+ #new x = slope^2 - x1 - x2
52
+ x = (slope ** 2 - point1[:x] - point2[:x]) % $p
53
+ #new y = slope * (x1 - new x) - y1
54
+ y = ((slope * (point1[:x] - x)) - point1[:y]) % $p
55
+
56
+ return { x: x, y: y }
57
+ end
58
+
59
+ #Multiply - Use the double and add operations to multiply a point by an integer.
60
+ def self.multiply(k, point = $g)
61
+ # create a copy the initial starting point (for use in addition later on)
62
+ current = point
63
+
64
+ # convert integer to binary representation (for use in the double and add algorithm)
65
+ binary = k.to_s(2)
66
+
67
+ # double and add algorithm for fast multiplication
68
+ binary.split("").drop(1).each do |char| # ignore first binary character
69
+ # 0 = double
70
+ current = double(current)
71
+
72
+ # 1 = double and add
73
+ if char == "1"
74
+ current = add(current, point)
75
+ end
76
+ end
77
+
78
+ return current
79
+ end
80
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crypto_commons
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - spmarisa
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-12-20 00:00:00.000000000 Z
11
+ date: 2021-12-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: crypto_commons is a handy tool in managing crypto assets.
14
14
  email:
@@ -28,8 +28,11 @@ files:
28
28
  - bin/console
29
29
  - bin/setup
30
30
  - crypto_commons.gemspec
31
+ - lib/base58.rb
32
+ - lib/checksum.rb
31
33
  - lib/crypto_commons.rb
32
34
  - lib/crypto_commons/version.rb
35
+ - lib/elliptic_curve.rb
33
36
  - sig/crypto_commons.rbs
34
37
  homepage: https://github.com/spmarisa/crypto_commons
35
38
  licenses: