Module: Solace::Utils::Codecs

Extended by:
Codecs
Included in:
Codecs
Defined in:
lib/solace/zar_trustless_escrow/codecs_extensions.rb

Overview

Extensions to Solace::Utils::Codecs for the Borsh/Anchor types used by the ZAR Trustless Escrow program that the base solace gem does not provide.

The base gem (>= 0.1.5) already provides encode_le_u64 / decode_le_u64 and the base58 helpers as module (self.) methods; this module reopens Codecs and uses ‘extend self` so the helpers below are callable both as instance methods (within composed encoders) and as Solace::Utils::Codecs.<method>.

Instance Method Summary collapse

Instance Method Details

#decode_le_i64(stream) ⇒ Integer

Decodes an i64 from 8 little-endian bytes (two’s complement).

Parameters:

  • stream (IO, StringIO)

    The stream to read from.

Returns:

  • (Integer)

    Value in range -2**63..2**63-1.



66
67
68
# File 'lib/solace/zar_trustless_escrow/codecs_extensions.rb', line 66

def decode_le_i64(stream)
  stream.read(8).unpack1('q<')
end

#decode_option_i64(stream) ⇒ Integer?

Decodes an Option<i64> in Borsh format. None → nil, Some(i64) → integer.

Parameters:

  • stream (IO, StringIO)

    The stream to read from.

Returns:

  • (Integer, nil)


94
95
96
97
98
# File 'lib/solace/zar_trustless_escrow/codecs_extensions.rb', line 94

def decode_option_i64(stream)
  return nil if decode_u8(stream).zero?

  decode_le_i64(stream)
end

#decode_option_pubkey(stream) ⇒ String?

Decodes an Option<publicKey> in Borsh format. None → nil, Some(key) → base58 pubkey.

Parameters:

  • stream (IO, StringIO)

    The stream to read from.

Returns:

  • (String, nil)

    Base58 public key or nil.



83
84
85
86
87
# File 'lib/solace/zar_trustless_escrow/codecs_extensions.rb', line 83

def decode_option_pubkey(stream)
  return nil if decode_u8(stream).zero?

  decode_pubkey(stream)
end

#decode_pubkey(stream) ⇒ String

Decodes a public key from 32 bytes.

Parameters:

  • stream (IO, StringIO)

    The stream to read from.

Returns:

  • (String)

    Base58 public key.



74
75
76
# File 'lib/solace/zar_trustless_escrow/codecs_extensions.rb', line 74

def decode_pubkey(stream)
  Solace::Utils::Codecs.bytes_to_base58(stream.read(32).bytes)
end

#decode_u8(stream) ⇒ Integer

Decodes a u8 from 1 byte.

Parameters:

  • stream (IO, StringIO)

    The stream to read from.

Returns:

  • (Integer)

    Value in range 0..255.



58
59
60
# File 'lib/solace/zar_trustless_escrow/codecs_extensions.rb', line 58

def decode_u8(stream)
  stream.read(1).unpack1('C')
end

#encode_le_i64(i64) ⇒ String

Encodes an i64 as 8 little-endian bytes (two’s complement).

Parameters:

  • i64 (Integer)

    Value in range -2**63..2**63-1.

Returns:

  • (String)

    8-byte little-endian binary string.



19
20
21
# File 'lib/solace/zar_trustless_escrow/codecs_extensions.rb', line 19

def encode_le_i64(i64)
  [i64].pack('q<')
end

#encode_option_i64(i64) ⇒ Array<Integer>

Encodes an Option<i64> in Borsh format. None → [0], Some(i64) → [1] + 8 little-endian bytes.

Parameters:

  • i64 (Integer, nil)

Returns:

  • (Array<Integer>)


48
49
50
51
52
# File 'lib/solace/zar_trustless_escrow/codecs_extensions.rb', line 48

def encode_option_i64(i64)
  return [0] if i64.nil?

  [1] + encode_le_i64(i64).bytes
end

#encode_option_pubkey(pubkey) ⇒ Array<Integer>

Encodes an Option<publicKey> in Borsh format. None → [0], Some(key) → [1] + 32 bytes.

Parameters:

  • pubkey (#to_s, nil)

    Base58 public key or nil.

Returns:

  • (Array<Integer>)


37
38
39
40
41
# File 'lib/solace/zar_trustless_escrow/codecs_extensions.rb', line 37

def encode_option_pubkey(pubkey)
  return [0] if pubkey.nil?

  [1] + encode_pubkey(pubkey)
end

#encode_pubkey(pubkey) ⇒ Array<Integer>

Encodes a public key as 32 bytes. Accepts any representation that resolves to a base58 string via #to_s (String, Keypair, PublicKey).

Parameters:

  • pubkey (#to_s)

    The public key in any representation.

Returns:

  • (Array<Integer>)

    32 bytes.



28
29
30
# File 'lib/solace/zar_trustless_escrow/codecs_extensions.rb', line 28

def encode_pubkey(pubkey)
  Solace::Utils::Codecs.base58_to_bytes(pubkey.to_s)
end