// NEEDS TO BE TESTED void CoinQKeychainSqlite3::insertKey(const uchar_vector& privKey, int type, int status, const std::string& label, bool bCompressed, int minHeight, int maxHeight) { if (type < 1 || type > 2) { throw CoinQ::Exception("Invalid type.", CoinQ::Exception::INVALID_PARAMETERS); } if (status < 1 || status > 2) { throw CoinQ::Exception("Invalid status.", CoinQ::Exception::INVALID_PARAMETERS); } CoinKey key; key.setCompressed(bCompressed); key.setPrivateKey(privKey); uchar_vector pubkey = key.getPublicKey(); std::string pubkeyhash = ripemd160(sha256(pubkey)).getHex(); std::string pubkeyhex = pubkey.getHex(); std::string privkeyhex = privKey.getHex(); SQLite3Stmt stmt; stmt.prepare(db, "INSERT INTO `keys` (`hash`, `pubkey`, `privkey`, `type`, `status`, `minheight`, `maxheight`, `label`) VALUES (?,?,?,?,?,?,?,?)"); stmt.bindText(1, pubkeyhash); stmt.bindText(2, pubkeyhex); stmt.bindText(3, privkeyhex); stmt.bindInt (4, type); stmt.bindInt (5, status); stmt.bindInt (6, minHeight); stmt.bindInt (7, maxHeight); stmt.bindText(8, label); stmt.step(); stmt.finalize(); }
std::string signtransaction(bool bHelp, params_t& params) { if (bHelp || params.size() < 6) { return "signtransaction <outhash> <outindex> <redeemscript> <toaddress> <value> <privkey1> [<privkey2> <privkey3> ...] - creates and signs a transaction claiming a multisignature input."; } uchar_vector outHash = params[0]; uint outIndex = strtoul(params[1].c_str(), NULL, 10); uchar_vector redeemScript = params[2]; std::string toAddress = params[3]; uint64_t value = strtoull(params[4].c_str(), NULL, 10); std::vector<std::string> privKeys; for (uint i = 5; i < params.size(); i++) { privKeys.push_back(params[i]); } StandardTxOut txOut; txOut.set(toAddress, value); MultiSigRedeemScript multiSig; multiSig.parseRedeemScript(redeemScript); P2SHTxIn txIn(outHash, outIndex, multiSig.getRedeemScript()); txIn.setScriptSig(SCRIPT_SIG_SIGN); Transaction tx; tx.addOutput(txOut); tx.addInput(txIn); uchar_vector hashToSign = tx.getHashWithAppendedCode(1); // SIGHASH_ALL // TODO: make sure to wipe all key data if there's any failure CoinKey key; for (uint i = 0; i < privKeys.size(); i++) { if (!key.setWalletImport(privKeys[i])) { std::stringstream ss; ss << "Private key " << i+1 << " is invalid."; throw std::runtime_error(ss.str()); } uchar_vector sig; if (!key.sign(hashToSign, sig)) { std::stringstream ss; ss << "Error signing with key " << i+1 << "."; throw std::runtime_error(ss.str()); } txIn.addSig(uchar_vector(), sig); } if (privKeys.size() < multiSig.getMinSigs()) { txIn.setScriptSig(SCRIPT_SIG_EDIT); } else { txIn.setScriptSig(SCRIPT_SIG_BROADCAST); } tx.clearInputs(); tx.addInput(txIn); return tx.getSerialized().getHex(); }
int main() { CoinKey coinKey; coinKey.generateNewKey(); string receivingAddress = coinKey.getAddress(); string walletImport = coinKey.getWalletImport(); cout << endl << "Address: " << receivingAddress << endl << "Wallet Import: " << walletImport << endl << endl; return 0; }
void CoinQKeychainSqlite3::generateNewKeys(int count, int type, bool bCompressed) { SQLite3Stmt stmt; CoinKey key; key.setCompressed(bCompressed); stmt.prepare(db, "INSERT INTO `keys` (`hash`, `pubkey`, `privkey`, `type`, `status`, `minheight`, `maxheight`) VALUES (?,?,?,?,?,-1,-1)"); for (int i = 0; i < count; i++) { key.generateNewKey(); uchar_vector pubkey = key.getPublicKey(); std::string pubkeyhash = ripemd160(sha256(pubkey)).getHex(); std::string pubkeyhex = pubkey.getHex(); uchar_vector privkey = key.getPrivateKey(); std::string privkeyhex = privkey.getHex(); stmt.reset(); stmt.bindText(1, pubkeyhash); stmt.bindText(2, pubkeyhex); stmt.bindText(3, privkeyhex); stmt.bindInt (4, type); stmt.bindInt (5, Status::POOL); stmt.step(); } }