dp-contract-proxy-kit
Version:
Enable batched transactions and contract account interactions using a unique deterministic Gnosis Safe.
58 lines • 2.69 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import BigNumber from 'bignumber.js';
import { bufferToHex, ecrecover, pubToAddress } from 'ethereumjs-util';
export const getTransactionHashSignature = (ethLibAdapter, ownerAccount, txHash) => __awaiter(void 0, void 0, void 0, function* () {
let signature = yield ethLibAdapter.signMessage(txHash, ownerAccount);
const hasPrefix = isTxHashSignedWithPrefix(txHash, signature, ownerAccount);
let signatureV = parseInt(signature.slice(-2), 16);
switch (signatureV) {
case 0:
case 1:
signatureV += 31;
break;
case 27:
case 28:
if (hasPrefix) {
signatureV += 4;
}
break;
default:
throw new Error('Invalid signature');
}
signature = signature.slice(0, -2) + signatureV.toString(16);
return signature;
});
export const getTransactionHashSignatureRSV = (ethLibAdapter, ownerAccount, txHash) => __awaiter(void 0, void 0, void 0, function* () {
const signature = yield getTransactionHashSignature(ethLibAdapter, ownerAccount, txHash);
return {
r: new BigNumber('0x' + signature.slice(2, 66)).toString(10),
s: new BigNumber('0x' + signature.slice(66, 130)).toString(10),
v: new BigNumber('0x' + signature.slice(130, 132)).toString(10)
};
});
const isTxHashSignedWithPrefix = (txHash, signature, ownerAccount) => {
let hasPrefix;
try {
const rsvSig = {
r: Buffer.from(signature.slice(2, 66), 'hex'),
s: Buffer.from(signature.slice(66, 130), 'hex'),
v: parseInt(signature.slice(130, 132), 16)
};
const recoveredData = ecrecover(Buffer.from(txHash.slice(2), 'hex'), rsvSig.v, rsvSig.r, rsvSig.s);
const recoveredAccount = bufferToHex(pubToAddress(recoveredData));
hasPrefix = recoveredAccount.toLowerCase() !== ownerAccount.toLowerCase();
}
catch (e) {
hasPrefix = true;
}
return hasPrefix;
};
//# sourceMappingURL=utils.js.map