Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/utils/sign-vote.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { crypto } from "bitcoinjs-lib";
import { ECPairFactory } from "ecpair";
import { Buffer } from "buffer";
import { Int64LE } from "int64-buffer";
import secp256k1 from "secp256k1";
import { swapEndiannessInPlace, swapEndianness } from "./buffer-math";
import { syscoinNetworks } from "./networks";
import * as secp from "@bitcoinerlab/secp256k1";

const ECPair = ECPairFactory(secp);


/**
* This function returns an object that the api must receive to make the vote through the mn, collecting the data for the vote and making the signature
Expand All @@ -15,6 +20,10 @@ const signVote = (obj) => {
// eslint-disable-next-line
try {
const { mnPrivateKey, vinMasternode, gObjectHash, voteOutcome } = obj;
const network =
process.env.REACT_APP_CHAIN_NETWORK === "main"
? syscoinNetworks.mainnet
: syscoinNetworks.testnet;
const time = Math.floor(Date.now() / 1000);
const gObjectHashBuffer = Buffer.from(gObjectHash, "hex");
const voteSignalNum = 1; // 'funding'
Expand Down Expand Up @@ -45,10 +54,24 @@ const signVote = (obj) => {
const hash = crypto.hash256(message);

let signObj = null;
// mnPrivateKey is expected to be a hex-encoded private key from the descriptor derivation
const privateKeyBuffer = Buffer.from(mnPrivateKey, "hex");
let privateKeyBuffer;
let isCompressed = true;
if (/^[0-9a-fA-F]{64}$/.test(mnPrivateKey)) {
privateKeyBuffer = Buffer.from(mnPrivateKey, "hex");
} else {
const keyPair = ECPair.fromWIF(`${mnPrivateKey}`, network);
if (!keyPair.privateKey) {
throw new Error("mnPrivateKey WIF does not contain a private key");
}
privateKeyBuffer = Buffer.from(keyPair.privateKey);
isCompressed = keyPair.compressed;
}

if (privateKeyBuffer.length !== 32) {
throw new Error("mnPrivateKey must be 32 bytes");
}

signObj = secp256k1.sign(hash, privateKeyBuffer);
const isCompressed = true;
const recId = 27 + signObj.recovery + (isCompressed ? 4 : 0);

const recIdBuffer = Buffer.allocUnsafe(1);
Expand Down