Skip to content

Instantly share code, notes, and snippets.

@ukstv
Created November 11, 2019 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ukstv/5bc79c4f6498cd9fa8fcae0d224ca68d to your computer and use it in GitHub Desktop.
Save ukstv/5bc79c4f6498cd9fa8fcae0d224ca68d to your computer and use it in GitHub Desktop.
import Web3 from 'web3'
import util from 'ethereumjs-util'
export function toSolSig(sig: string): string {
const {r, s, v} = util.fromRpcSig(sig)
// NOTE: with potential introduction of chainId this might need to be updated
if (v !== 27 && v !== 28) {
throw new Error('Invalid recovery id');
}
const parts = [util.setLengthLeft(r, 32) as Buffer, util.setLengthLeft(s, 32) as Buffer, util.toBuffer(v)]
return util.bufferToHex(Buffer.concat(parts));
}
export async function properSignature(web3: Web3, account: string, data: string): Promise<string> {
const improperSig = await web3.eth.sign(data, account)
return toSolSig(improperSig)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment