Class: Fireblocks::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/fireblocks/api/token.rb

Overview

This class will issue a new Fireblocks token

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, uri) ⇒ Token

Returns a new instance of Token.



18
19
20
21
# File 'lib/fireblocks/api/token.rb', line 18

def initialize(body, uri)
  @body = body
  @uri = uri
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



16
17
18
# File 'lib/fireblocks/api/token.rb', line 16

def body
  @body
end

#uriObject

Returns the value of attribute uri.



16
17
18
# File 'lib/fireblocks/api/token.rb', line 16

def uri
  @uri
end

Class Method Details

.call(body, uri) ⇒ Object



11
12
13
# File 'lib/fireblocks/api/token.rb', line 11

def call(body, uri)
  new(body, uri).call
end

Instance Method Details

#body_hashObject



35
36
37
# File 'lib/fireblocks/api/token.rb', line 35

def body_hash
  Digest::SHA256.hexdigest(body.to_json)
end

#callObject



31
32
33
# File 'lib/fireblocks/api/token.rb', line 31

def call
  JWT.encode jwt_headers, rsa_private, 'RS256', typ: 'JWT'
end

#created_atObject



23
24
25
# File 'lib/fireblocks/api/token.rb', line 23

def created_at
  Time.now.to_i
end

#decode_tokenObject



39
40
41
# File 'lib/fireblocks/api/token.rb', line 39

def decode_token
  JWT.decode token, rsa_private.public_key, true, algorithm: 'RS256'
end

#expire_atObject



27
28
29
# File 'lib/fireblocks/api/token.rb', line 27

def expire_at
  Time.now.to_i + 55
end

#jwt_headersObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/fireblocks/api/token.rb', line 43

def jwt_headers
  {
    uri: uri.request_uri,
    nonce: nonce,
    iat: created_at,
    exp: expire_at,
    sub: Fireblocks.configuration.api_key,
    bodyHash: body_hash
  }
end

#nonceObject



54
55
56
# File 'lib/fireblocks/api/token.rb', line 54

def nonce
  "#{Time.now.to_i}#{Time.now.nsec}".to_i
end

#rsa_privateObject



58
59
60
# File 'lib/fireblocks/api/token.rb', line 58

def rsa_private
  OpenSSL::PKey::RSA.new(Fireblocks.configuration.private_key)
end