Exemplo n.º 1
0
Value verifymessage(const Array& params, bool fHelp)
{
    if (fHelp || params.size() != 3)
        throw runtime_error(
            "verifymessage <username> <signature> <message>\n"
            "Verify a signed message");

    string strUsername = params[0].get_str();
    string strSign     = params[1].get_str();
    string strMessage  = params[2].get_str();

    CPubKey pubkey;
    {
        CKeyID keyID;
        if( pwalletMain->GetKeyIdFromUsername(strUsername, keyID) ) {
            if( !pwalletMain->GetPubKey(keyID, pubkey) )
                throw JSONRPCError(RPC_WALLET_INVALID_ACCOUNT_NAME, "Error: failed to read pubkey from wallet");
        }
    }

    if( !pubkey.IsValid() ) {
        CTransaction txOut;
        uint256 hashBlock;
        if( !GetTransaction(strUsername, txOut, hashBlock) )
            throw JSONRPCError(RPC_WALLET_INVALID_ACCOUNT_NAME, "Error: this username does not exist in tx database");

        std::vector< std::vector<unsigned char> > vData;
        if( !txOut.pubKey.ExtractPushData(vData) || vData.size() < 1 )
            throw JSONRPCError(RPC_INTERNAL_ERROR, "Error: error extracting pubkey from tx");
        pubkey = CPubKey(vData[0]);
        if( !pubkey.IsValid() )
            throw JSONRPCError(RPC_INTERNAL_ERROR, "Error: invalid pubkey data from tx");
    }

    bool fInvalid = false;
    vector<unsigned char> vchSig = DecodeBase64(strSign.c_str(), &fInvalid);

    if (fInvalid)
        throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Malformed base64 encoding");

    CHashWriter ss(SER_GETHASH, 0);
    ss << strMessageMagic;
    ss << strMessage;

    CPubKey pubkeyRec;
    if (!pubkeyRec.RecoverCompact(ss.GetHash(), vchSig))
        return false;

    return (pubkeyRec.GetID() == pubkey.GetID());
}
Exemplo n.º 2
0
//*****************************************************************************
//*****************************************************************************
void AddressBookPage::onCopyPublicKeyAction()
{
    if (!ui->tableView->selectionModel())
    {
        QMessageBox::warning(this, "", trUtf8("No selected rows"));
        return;
    }

    QModelIndexList indexes = ui->tableView->selectionModel()->selectedRows(AddressTableModel::Address);
    if (indexes.isEmpty())
    {
        QMessageBox::warning(this, "", trUtf8("No selected rows"));
        return;
    }

    QModelIndex idx = indexes.at(0);
    QString address = idx.data().toString();

    CPubKey pubKey;

    StoredPubKeysDb & keysDb = StoredPubKeysDb::instance();

    // check stored key
    if (keysDb.load(address.toStdString(), pubKey))
    {
        // found
        QApplication::clipboard()->setText(QString::fromStdString(EncodeBase58(pubKey.Raw())));
        return;
    }

    // not found stored key, check address
    CBitcoinAddress addr(address.toStdString());
    if (!addr.IsValid())
    {
        QMessageBox::warning(this, "", trUtf8("Invalid bitcoin address"));
        return;
    }

    CKeyID id;
    CKey key;
    if (!addr.GetKeyID(id) || !pwalletMain->GetKey(id, key))
    {
        QMessageBox::information(this, "", trUtf8("Key not found"));
        return;
    }

    pubKey = key.GetPubKey();
    if (!pubKey.IsValid())
    {
        QMessageBox::information(this, "", trUtf8("Public key not found"));
        return;
    }

    QApplication::clipboard()->setText(QString::fromStdString(EncodeBase58(pubKey.Raw())));
}
Exemplo n.º 3
0
Value getdefaultuser(const Array& params, bool fHelp)
{
    if (fHelp || params.size() != 0)
        throw runtime_error(
            "getdefaultuser\n"
            "Get default user being used");

    EnsureWalletIsUnlocked();

    CPubKey vchPubKey = pwalletMain->vchDefaultKey;

    if( !vchPubKey.IsValid() )
        throw JSONRPCError(RPC_WALLET_ERROR, "Error: default user key is invalid");

    CKeyID keyID = vchPubKey.GetID();
    std::string username;
    if( !pwalletMain->GetUsernameFromKeyId(keyID, username) )
        throw JSONRPCError(RPC_WALLET_ERROR, "Error converting keyID to username");

    return username;
}
Exemplo n.º 4
0
Value smsglocalkeys(const Array& params, bool fHelp)
{
    if (fHelp || params.size() > 3)
        throw runtime_error(
            "smsglocalkeys [whitelist|all|wallet|recv <+/-> <address>|anon <+/-> <address>]\n"
            "List and manage keys.");
    
    if (!fSecMsgEnabled)
        throw runtime_error("Secure messaging is disabled.");
    
    Object result;
    
    std::string mode = "whitelist";
    if (params.size() > 0)
    {
        mode = params[0].get_str();
    };
    
    char cbuf[256];
    
    if (mode == "whitelist"
        || mode == "all")
    {
        uint32_t nKeys = 0;
        int all = mode == "all" ? 1 : 0;
        for (std::vector<SecMsgAddress>::iterator it = smsgAddresses.begin(); it != smsgAddresses.end(); ++it)
        {
            if (!all 
                && !it->fReceiveEnabled)
                continue;
            
            CBitcoinAddress coinAddress(it->sAddress);
            if (!coinAddress.IsValid())
                continue;
            
            std::string sPublicKey;
            
            CKeyID keyID;
            if (!coinAddress.GetKeyID(keyID))
                continue;
            
            CPubKey pubKey;
            if (!pwalletMain->GetPubKey(keyID, pubKey))
                continue;
            if (!pubKey.IsValid()
                || !pubKey.IsCompressed())
            {
                continue;
            };
            
            
            sPublicKey = EncodeBase58(pubKey.Raw());
            
            std::string sLabel = pwalletMain->mapAddressBook[keyID];
            std::string sInfo;
            if (all)
                sInfo = std::string("Receive ") + (it->fReceiveEnabled ? "on,  " : "off, ");
            sInfo += std::string("Anon ") + (it->fReceiveAnon ? "on" : "off");
            result.push_back(Pair("key", it->sAddress + " - " + sPublicKey + " " + sInfo + " - " + sLabel));
            
            nKeys++;
        };
        
        
        snprintf(cbuf, sizeof(cbuf), "%u keys listed.", nKeys);
        result.push_back(Pair("result", std::string(cbuf)));
        
    } else
    if (mode == "recv")
    {
        if (params.size() < 3)
        {
            result.push_back(Pair("result", "Too few parameters."));
            result.push_back(Pair("expected", "recv <+/-> <address>"));
            return result;
        };
        
        std::string op      = params[1].get_str();
        std::string addr    = params[2].get_str();
        
        std::vector<SecMsgAddress>::iterator it;
        for (it = smsgAddresses.begin(); it != smsgAddresses.end(); ++it)
        {
            if (addr != it->sAddress)
                continue;
            break;
        };
        
        if (it == smsgAddresses.end())
        {
            result.push_back(Pair("result", "Address not found."));
            return result;
        };
        
        if (op == "+" || op == "on"  || op == "add" || op == "a")
        {
            it->fReceiveEnabled = true;
        } else
        if (op == "-" || op == "off" || op == "rem" || op == "r")
        {
            it->fReceiveEnabled = false;
        } else
        {
            result.push_back(Pair("result", "Unknown operation."));
            return result;
        };
        
        std::string sInfo;
        sInfo = std::string("Receive ") + (it->fReceiveEnabled ? "on, " : "off,");
        sInfo += std::string("Anon ") + (it->fReceiveAnon ? "on" : "off");
        result.push_back(Pair("result", "Success."));
        result.push_back(Pair("key", it->sAddress + " " + sInfo));
        return result;
        
    } else
    if (mode == "anon")
    {
        if (params.size() < 3)
        {
            result.push_back(Pair("result", "Too few parameters."));
            result.push_back(Pair("expected", "anon <+/-> <address>"));
            return result;
        };
        
        std::string op      = params[1].get_str();
        std::string addr    = params[2].get_str();
        
        std::vector<SecMsgAddress>::iterator it;
        for (it = smsgAddresses.begin(); it != smsgAddresses.end(); ++it)
        {
            if (addr != it->sAddress)
                continue;
            break;
        };
        
        if (it == smsgAddresses.end())
        {
            result.push_back(Pair("result", "Address not found."));
            return result;
        };
        
        if (op == "+" || op == "on"  || op == "add" || op == "a")
        {
            it->fReceiveAnon = true;
        } else
        if (op == "-" || op == "off" || op == "rem" || op == "r")
        {
            it->fReceiveAnon = false;
        } else
        {
            result.push_back(Pair("result", "Unknown operation."));
            return result;
        };
        
        std::string sInfo;
        sInfo = std::string("Receive ") + (it->fReceiveEnabled ? "on, " : "off,");
        sInfo += std::string("Anon ") + (it->fReceiveAnon ? "on" : "off");
        result.push_back(Pair("result", "Success."));
        result.push_back(Pair("key", it->sAddress + " " + sInfo));
        return result;
        
    } else
    if (mode == "wallet")
    {
        uint32_t nKeys = 0;
        BOOST_FOREACH(const PAIRTYPE(CTxDestination, std::string)& entry, pwalletMain->mapAddressBook)
        {
            if (!IsMine(*pwalletMain, entry.first))
                continue;
            
            CBitcoinAddress coinAddress(entry.first);
            if (!coinAddress.IsValid())
                continue;
            
            std::string address;
            std::string sPublicKey;
            address = coinAddress.ToString();
            
            CKeyID keyID;
            if (!coinAddress.GetKeyID(keyID))
                continue;
            
            CPubKey pubKey;
            if (!pwalletMain->GetPubKey(keyID, pubKey))
                continue;
            if (!pubKey.IsValid()
                || !pubKey.IsCompressed())
            {
                continue;
            };
            
            sPublicKey = EncodeBase58(pubKey.Raw());
            
            result.push_back(Pair("key", address + " - " + sPublicKey + " - " + entry.second));
            nKeys++;
        };
        
        snprintf(cbuf, sizeof(cbuf), "%u keys listed from wallet.", nKeys);
        result.push_back(Pair("result", std::string(cbuf)));
    } else
Exemplo n.º 5
0
bool ExtractDestination(const CScript& _scriptPubKey, CTxDestination& addressRet, bool returnPubKey)
{
    vector<valtype> vSolutions;
    txnouttype whichType;
    CScript scriptPubKey = _scriptPubKey;

    // if this is a CLTV script, get the destination after CLTV
    if (scriptPubKey.IsCheckLockTimeVerify())
    {
        uint8_t pushOp = scriptPubKey[0];
        uint32_t scriptStart = pushOp + 3;

        // check post CLTV script
        scriptPubKey = CScript(scriptPubKey.size() > scriptStart ? scriptPubKey.begin() + scriptStart : scriptPubKey.end(), scriptPubKey.end());
    }

    if (!Solver(scriptPubKey, whichType, vSolutions))
        return false;

    if (whichType == TX_PUBKEY)
    {
        CPubKey pubKey(vSolutions[0]);
        if (!pubKey.IsValid())
        {
            //fprintf(stderr,"TX_PUBKEY invalid pubkey\n");
            return false;
        }

        if (returnPubKey)
            addressRet = pubKey;
        else
            addressRet = pubKey.GetID();
        return true;
    }

    else if (whichType == TX_PUBKEYHASH)
    {
        addressRet = CKeyID(uint160(vSolutions[0]));
        return true;
    }
    else if (whichType == TX_SCRIPTHASH)
    {
        addressRet = CScriptID(uint160(vSolutions[0]));
        return true;
    }
    
    else if (IsCryptoConditionsEnabled() != 0 && whichType == TX_CRYPTOCONDITION)
    {
        if (vSolutions.size() > 1)
        {
            CPubKey pk = CPubKey((vSolutions[1]));
            addressRet = pk;
            return pk.IsValid();
        }
        else
        {
            addressRet = CKeyID(uint160(vSolutions[0]));
        }
        return true;
    }
    // Multisig txns have more than one address...
    return false;
}