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

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
# File 'lib/types/cl_public_key.rb', line 35

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

Class Method Details

.from_ed25519(public_key) ⇒ Object



111
112
113
# File 'lib/types/cl_public_key.rb', line 111

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

.from_hex(public_key_hex) ⇒ Object

Raises:

  • (ArgumentError)


134
135
136
137
138
# File 'lib/types/cl_public_key.rb', line 134

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



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

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



119
120
121
# File 'lib/types/cl_public_key.rb', line 119

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

.to_json(public_key) ⇒ Object



140
141
142
# File 'lib/types/cl_public_key.rb', line 140

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:



84
85
86
# File 'lib/types/cl_public_key.rb', line 84

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

#from_ed25519(public_key) ⇒ Object



107
108
109
# File 'lib/types/cl_public_key.rb', line 107

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

#from_hex(public_key_hex) ⇒ Object

Raises:

  • (ArgumentError)


128
129
130
131
132
# File 'lib/types/cl_public_key.rb', line 128

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



115
116
117
# File 'lib/types/cl_public_key.rb', line 115

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

#get_cl_public_key_tagObject



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

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

#get_cl_typeObject



58
59
60
61
# File 'lib/types/cl_public_key.rb', line 58

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

#get_signature_algorithmObject



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

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

#get_valueObject



64
65
66
# File 'lib/types/cl_public_key.rb', line 64

def get_value
  @raw_public_key  
end

#raw_public_key_length_valid?(raw_public_key, tag) ⇒ Boolean

Returns:



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

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:



88
89
90
# File 'lib/types/cl_public_key.rb', line 88

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

#to_account_hash_byte_arrayArray<Integer>

Returns:

  • (Array<Integer>)


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

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



102
103
104
105
# File 'lib/types/cl_public_key.rb', line 102

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

#to_hexObject



123
124
125
126
# File 'lib/types/cl_public_key.rb', line 123

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