src/sigs.js
// Transform eth_sign RSV result to a canonical DER signature
Transform eth_sign RSV result to a canonical DER signature
Transform eth_sign RSV result to a canonical DER signature
function rsvToDER(rsvStr) {
  let { r, s } = extractVRS(rsvStr);
  let sBigInt = utils.bytesToUint(s);
  if (sBigInt > CURVE_ORDER / BigInt(2)) {
    sBigInt = CURVE_ORDER - sBigInt;
    s = utils.deserializeHex((sBigInt.toString(16)));
  }
  while (s[0] === 0 && s[1] & 0x80 !== 0) {
    s = utils.safeSlice(s, 1);
  }
  while (r[0] === 0 && r[1] & 0x80 !== 0) {
    r = utils.safeSlice(r, 1);
  }
  const encR = utils.concatUint8Arrays(new Uint8Array([0x02, r.length]), r);
  const encS = utils.concatUint8Arrays(new Uint8Array([0x02, s.length]), s);
  return utils.concatUint8Arrays(
    new Uint8Array([0x30, encR.length + encS.length]),
    encR,
    encS
  );
}
function rsvToDER ( rsvStr ) { let { r , s } = extractVRS ( rsvStr ) ; let sBigInt = utils . bytesToUint ( s ) ; if ( sBigInt > CURVE_ORDER / BigInt ( 2 ) ) { sBigInt = CURVE_ORDER - sBigInt ; s = utils . deserializeHex ( ( sBigInt . toString ( 16 ) ) ) ; } while ( s [ 0 ] === 0 && s [ 1 ] & 0x80 !== 0 ) { s = utils . safeSlice ( s , 1 ) ; } while ( r [ 0 ] === 0 && r [ 1 ] & 0x80 !== 0 ) { r = utils . safeSlice ( r , 1 ) ; } const encR = utils . concatUint8Arrays ( new Uint8Array ( [ 0x02 , r . length ] ) , r ) ; const encS = utils . concatUint8Arrays ( new Uint8Array ( [ 0x02 , s . length ] ) , s ) ; return utils . concatUint8Arrays ( new Uint8Array ( [ 0x30 , encR . length + encS . length ] ) , encR , encS ) ; }
Transform eth_sign RSV result to a canonical DER signature
Transform eth_sign RSV result to a canonical DER signature
// If S is non-canoncial, lower it. Unsure if this is neccessary. Does // Ethereum use canonical low-S signatures? // DER-encoded integers use a minimal signed encoding. // If there is a leading 0 and the next bit is 0, trim the lead.