aboutsummaryrefslogblamecommitdiffstats
path: root/src/utils/signature_utils.ts
blob: b312b555495fa48190bc1d208379e36317a067b1 (plain) (tree)




























                                                               
import * as ethUtil from 'ethereumjs-util';
import {ECSignature} from '../types';

export const signatureUtils = {
    parseSignatureHexAsVRS(signatureHex: string): ECSignature {
        const signatureBuffer = ethUtil.toBuffer(signatureHex);
        let v = signatureBuffer[0];
        if (v < 27) {
            v += 27;
        }
        const r = signatureBuffer.slice(1, 33);
        const s = signatureBuffer.slice(33, 65);
        const ecSignature: ECSignature = {
            v,
            r: ethUtil.bufferToHex(r),
            s: ethUtil.bufferToHex(s),
        };
        return ecSignature;
    },
    parseSignatureHexAsRSV(signatureHex: string): ECSignature {
        const {v, r, s} = ethUtil.fromRpcSig(signatureHex);
        const ecSignature: ECSignature = {
            v,
            r: ethUtil.bufferToHex(r),
            s: ethUtil.bufferToHex(s),
        };
        return ecSignature;
    },
};