Skip to content

Instantly share code, notes, and snippets.

@danfinlay
Created March 9, 2020 19:17
Show Gist options
  • Save danfinlay/33afe20a4ddb5c40bc899b5ddb99ff78 to your computer and use it in GitHub Desktop.
Save danfinlay/33afe20a4ddb5c40bc899b5ddb99ff78 to your computer and use it in GitHub Desktop.
Low level signing method roadmap
  • low-level signing method
@prestwich
Copy link

prestwich commented Mar 15, 2020

placing this here for future reference. uses utils from bitcoin-spv

function srvToDER(srvStr) {
  let srv = '';
  if (srvStr.slice(0, 2) === '0x') {
    srv = srvStr.slice(2);
  } else {
    srv = srvStr;
  }
  let s = deserializeHex(safeSlice(srv, 0, 64));
  const r = deserializeHex(safeSlice(srv, 64, 128));

  // Trim to minimal encoding.
  // If there is a leading 0 and the next bit is 0, trim the lead.
  while (s[0] === 0 && s[1] & 0x80 !== 0) {
    s = safeSlice(s, 1);
  }
  while (r[0] === 0 && r[1] & 0x80 !== 0) {
    r = safeSlice(r, 1);
  }

  const encR = concatUint8Arrays(new Uint8Array([0x02, r.length]), r);
  const encS = concatUint8Arrays(new Uint8Array([0x02, s.length]), s)

  return concatUint8Arrays(
    new Uint8Array([0x30, encR.length + encS.length]),
    encR,
    encS
  );
}

@tynes
Copy link

tynes commented Mar 15, 2020

@prestwich
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment