Class: Ciri::Crypto::Signature

Inherits:
Object
  • Object
show all
Defined in:
lib/ciri/crypto/signature.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(signature: nil, vrs: nil) ⇒ Signature

Returns a new instance of Signature.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ciri/crypto/signature.rb', line 34

def initialize(signature: nil, vrs: nil)
  if !!signature == !!vrs
    raise ArgumentError.new("should pass signature_bytes or vrs, but can't provide both together")
  end

  if signature
    unless signature.size == 65
      raise ECDSASignatureError.new("signature size should be 65, got: #{signature.size}")
    end

    @r = Utils.big_endian_decode(signature[0...32])
    @s = Utils.big_endian_decode(signature[32...64])
    @v = Utils.big_endian_decode(signature[64])
  else
    @v, @r, @s = vrs

    unless self.signature.size == 65
      raise ECDSASignatureError.new("vrs is incorrect")
    end
  end
end

Instance Attribute Details

#rObject (readonly)

Returns the value of attribute r.



32
33
34
# File 'lib/ciri/crypto/signature.rb', line 32

def r
  @r
end

#sObject (readonly)

Returns the value of attribute s.



32
33
34
# File 'lib/ciri/crypto/signature.rb', line 32

def s
  @s
end

#vObject (readonly)

Returns the value of attribute v.



32
33
34
# File 'lib/ciri/crypto/signature.rb', line 32

def v
  @v
end

Instance Method Details

#low_s?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/ciri/crypto/signature.rb', line 70

def low_s?
  s < (SECP256K1N / 2)
end

#signatureObject Also known as: to_s



56
57
58
59
60
# File 'lib/ciri/crypto/signature.rb', line 56

def signature
  @signature ||= Utils.big_endian_encode(@r, "\x00".b, size: 32) +
    Utils.big_endian_encode(@s, "\x00".b, size: 32) +
    Utils.big_endian_encode(@v, "\x00".b)
end

#valid?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
# File 'lib/ciri/crypto/signature.rb', line 64

def valid?
  v <= 1 &&
    r < SECP256K1N && r >= 1 &&
    s < SECP256K1N && s >= 1
end