Class: CLPublicKey

Inherits:
CLValue show all
Includes:
CLValueBytesParsers::CLPublicKeyBytesParser, Utils::HashUtils
Defined in:
lib/types/cl_public_key.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::HashUtils

account_hash_from_byte_to_hex, byte_hash

Methods included from CLValueBytesParsers::CLPublicKeyBytesParser

#decode_base_16, #encode_base_16, #from_bytes, #to_bytes

Methods inherited from CLValue

#to_hash

Constructor Details

#initialize(raw_public_key, tag) ⇒ CLPublicKey

Returns a new instance of CLPublicKey.

Parameters:

  • raw_public_key (Array<Integer>)
  • tag (Integer)


35
36
37
38
39
40
# File 'lib/types/cl_public_key.rb', line 35

def initialize(raw_public_key, tag)
  super()
  @tag = tag
  @raw_public_key = raw_public_key
  raw_public_key_length_valid?(raw_public_key, tag)
end

Class Method Details

.from_ed25519(public_key) ⇒ Object



113
114
115
# File 'lib/types/cl_public_key.rb', line 113

def self.from_ed25519(public_key)
  CLPublicKey.new(public_key, CLPublicKeyTag[:ED25519])
end

.from_hex(public_key_hex) ⇒ Object

Raises:

  • (ArgumentError)


136
137
138
139
140
# File 'lib/types/cl_public_key.rb', line 136

def self.from_hex(public_key_hex)
  raise ArgumentError.new("Invalid public key") unless Utils::HexUtils.valid_public_key_format?(public_key_hex)
  public_key_hex_bytes = Utils::Base16.decode16(public_key_hex)
  CLPublicKey.new(public_key_hex_bytes.drop(1), public_key_hex_bytes[0])
end

.from_json(json) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/types/cl_public_key.rb', line 146

def self.from_json(json)
  parsed = JSON.parse(json).deep_symbolize_keys
  bytes = Utils::Base16.decode16(parsed[:bytes])
  tag = bytes[0]
  raw_public_key = bytes[1..]
  CLPublicKey.new(raw_public_key, tag)
end

.from_secp256k1(public_key) ⇒ Object



121
122
123
# File 'lib/types/cl_public_key.rb', line 121

def self.from_secp256k1(public_key)
  CLPublicKey.new(public_key, CLPublicKeyTag[:SECP256K1])
end

.to_json(public_key) ⇒ Object



142
143
144
# File 'lib/types/cl_public_key.rb', line 142

def self.to_json(public_key)
  json = {"bytes": public_key.to_hex, "cl_type": public_key.get_cl_type}.to_json
end

Instance Method Details

#ed25519?Boolean

Returns:



86
87
88
# File 'lib/types/cl_public_key.rb', line 86

def ed25519?
  @tag == CLPublicKeyTag[:ED25519]
end

#from_ed25519(public_key) ⇒ Object



109
110
111
# File 'lib/types/cl_public_key.rb', line 109

def from_ed25519(public_key)
  CLPublicKey.new(public_key, CLPublicKeyTag[:ED25519])
end

#from_hex(public_key_hex) ⇒ Object

Raises:

  • (ArgumentError)


130
131
132
133
134
# File 'lib/types/cl_public_key.rb', line 130

def from_hex(public_key_hex)
  raise ArgumentError.new("Invalid public key") unless Utils::HexUtils.valid_public_key_format?(public_key_hex)
  public_key_hex_bytes = Utils::Base16.decode16(public_key_hex)
  CLPublicKey.new(public_key_hex_bytes.drop(1), public_key_hex_bytes[0])
end

#from_secp256k1(public_key) ⇒ Object



117
118
119
# File 'lib/types/cl_public_key.rb', line 117

def from_secp256k1(public_key)
  CLPublicKey.new(public_key, CLPublicKeyTag[:SECP256K1])
end

#get_cl_public_key_tagObject



70
71
72
73
74
75
76
# File 'lib/types/cl_public_key.rb', line 70

def get_cl_public_key_tag
  if @tag == CLPublicKeyTag[:ED25519] || @tag == SignatureAlgorithm[:Ed25519]
    CLPublicKeyTag[:ED25519]
  else
    CLPublicKeyTag[:SECP256K1] 
  end
end

#get_cl_typeObject



60
61
62
63
# File 'lib/types/cl_public_key.rb', line 60

def get_cl_type
  @cl_type = CLPublicKeyType.new
  @cl_type.to_string
end

#get_signature_algorithmObject



78
79
80
81
82
83
84
# File 'lib/types/cl_public_key.rb', line 78

def get_signature_algorithm
  if @tag == CLPublicKeyTag[:ED25519] || @tag == SignatureAlgorithm[:Ed25519]
    SignatureAlgorithm[:Ed25519]
  else
    SignatureAlgorithm[:Secp256K1]
  end
end

#get_valueObject



66
67
68
# File 'lib/types/cl_public_key.rb', line 66

def get_value
  @raw_public_key  
end

#raw_public_key_length_valid?(raw_public_key, tag) ⇒ Boolean

Returns:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/types/cl_public_key.rb', line 42

def raw_public_key_length_valid?(raw_public_key, tag)
  if tag == CLPublicKeyTag[:ED25519] || tag == SignatureAlgorithm[:Ed25519]
    if raw_public_key.length != ED25519_LENGTH
      raise ArgumentError.new("Wrong length of ED25519 key. Expected #{ED25519_LENGTH}, but got #{raw_public_key.length}")
    end
    @raw_public_key = raw_public_key
    @tag = tag
  elsif tag == CLPublicKeyTag[:SECP256K1] || tag == SignatureAlgorithm[:Secp256K1]
    if raw_public_key.length != SECP256K1_LENGTH
      raise ArgumentError.new("Wrong length of SECP256K1 key. Expected #{SECP256K1_LENGTH}, but got #{raw_public_key.length}")
    end
    @raw_public_key = raw_public_key
    @tag = tag
  else
    raise ArgumentError.new("Unsupported type of public key")
  end
end

#secp256k1?Boolean

Returns:



90
91
92
# File 'lib/types/cl_public_key.rb', line 90

def secp256k1?
  @tag == CLPublicKeyTag[:SECP256K1]
end

#to_account_hash_byte_arrayArray<Integer>

Returns:

  • (Array<Integer>)


95
96
97
98
99
100
101
# File 'lib/types/cl_public_key.rb', line 95

def 
  key_name = CLPublicKeyTag.key(@tag).to_s
  prefix = key_name.downcase.unpack("C*") + [0]
  bytes = prefix + @raw_public_key
  result_array = Utils::HashUtils.byte_hash(bytes)
  @raw_public_key.length == 0 ? [] : result_array
end

#to_account_hash_hexString

Returns hex.

Returns:

  • (String)

    hex



104
105
106
107
# File 'lib/types/cl_public_key.rb', line 104

def 
  bytes = 
  hex = Utils::HashUtils.(bytes)
end

#to_hexObject



125
126
127
128
# File 'lib/types/cl_public_key.rb', line 125

def to_hex
  # "0#{@tag}#{Utils::Base16.encode16(@raw_public_key)}"
  "0#{@tag}#{CLValueBytesParsers::CLPublicKeyBytesParser.encode_base_16(@raw_public_key)}"
end