Class: NearApi::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/near_api/key.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(signer_id = ENV.fetch('NEAR_SIGNER_ID'), key_pair: ENV.fetch('NEAR_KEYPAIR', nil), public_key: ENV.fetch('NEAR_PUBLIC_KEY', nil)) ⇒ Key

Returns a new instance of Key.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/near_api/key.rb', line 8

def initialize(signer_id = ENV.fetch('NEAR_SIGNER_ID'),
               key_pair: ENV.fetch('NEAR_KEYPAIR', nil),
               public_key: ENV.fetch('NEAR_PUBLIC_KEY', nil))
  @signer_id = signer_id
  if key_pair.nil? && public_key.nil?
    raise ArgumentError, 'please specify key_pair or public_key'
  end

  unless key_pair.nil?
    key_value = if key_pair.include?(':')
                  key_pair.split(':').last
                else
                  key_pair
                end
    bytestring = NearApi::Base58.decode(key_value)
    @key_pair = Ed25519::SigningKey.from_keypair(bytestring)
    @public_key = @key_pair.verify_key.to_bytes
  end

  unless public_key.nil?
    key_value = if public_key.include?(':')
                  public_key.split(':').last
                else
                  public_key
                end
    bytestring = NearApi::Base58.decode(key_value)
    verify_key = Ed25519::VerifyKey.new(bytestring).to_bytes
    unless @public_key.nil?
      raise ArgumentError, 'public_key does not match keypair' if @public_key != verify_key
    end
    @public_key = verify_key
  end
end

Instance Attribute Details

#public_key ⇒ Object (readonly)

Returns the value of attribute public_key.



6
7
8
# File 'lib/near_api/key.rb', line 6

def public_key
  @public_key
end

#signer_id ⇒ Object (readonly)

Returns the value of attribute signer_id.



6
7
8
# File 'lib/near_api/key.rb', line 6

def signer_id
  @signer_id
end

Instance Method Details

#key_type ⇒ Object



42
43
44
# File 'lib/near_api/key.rb', line 42

def key_type
  0
end

#sign(msg) ⇒ Object



46
47
48
# File 'lib/near_api/key.rb', line 46

def sign(msg)
  key_pair.sign(msg)
end