bip66encode function

Uint8List bip66encode(
  1. dynamic r,
  2. dynamic s
)

Implementation

Uint8List bip66encode(r, s) {
  var lenR = r.length;
  var lenS = s.length;
  if (lenR == 0) throw new ArgumentError('R length is zero');
  if (lenS == 0) throw new ArgumentError('S length is zero');
  if (lenR > 33) throw new ArgumentError('R length is too long');
  if (lenS > 33) throw new ArgumentError('S length is too long');
  if (r[0] & 0x80 != 0) throw new ArgumentError('R value is negative');
  if (s[0] & 0x80 != 0) throw new ArgumentError('S value is negative');
  if (lenR > 1 && (r[0] == 0x00) && r[1] & 0x80 == 0)
    throw new ArgumentError('R value excessively padded');
  if (lenS > 1 && (s[0] == 0x00) && s[1] & 0x80 == 0)
    throw new ArgumentError('S value excessively padded');

  var signature = new Uint8List(6 + (lenR as int) + (lenS as int));

  // 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S]
  signature[0] = 0x30;
  signature[1] = signature.length - 2;
  signature[2] = 0x02;
  signature[3] = r.length;
  signature.setRange(4, 4 + lenR, r);
  signature[4 + lenR] = 0x02;
  signature[5 + lenR] = s.length;
  signature.setRange(6 + lenR, 6 + lenR + lenS, s);
  return signature;
}