Created
November 11, 2019 07:36
-
-
Save ukstv/5bc79c4f6498cd9fa8fcae0d224ca68d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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