Skip to content

Commit

Permalink
#1263 added methods to wasm keykeeper to extract sbbs address
Browse files Browse the repository at this point in the history
  • Loading branch information
anatolse committed Feb 20, 2020
1 parent 2876294 commit cc30908
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 5 deletions.
125 changes: 125 additions & 0 deletions keykeeper/wasm_key_keeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "local_private_key_keeper.h"
#include "mnemonic/mnemonic.h"
#include "wasm_key_keeper.h"
#include "wallet/core/wallet_db.h"

#include <boost/algorithm/string.hpp>
#include "utility/string_helpers.cpp"
Expand All @@ -28,6 +29,29 @@ using namespace beam::wallet;
using json = nlohmann::json;
// #define PRINT_TEST_DATA 1

namespace beam
{
char* to_hex(char* dst, const void* bytes, size_t size) {
static const char digits[] = "0123456789abcdef";
char* d = dst;

const uint8_t* ptr = (const uint8_t*)bytes;
const uint8_t* end = ptr + size;
while (ptr < end) {
uint8_t c = *ptr++;
*d++ = digits[c >> 4];
*d++ = digits[c & 0xF];
}
*d = '\0';
return dst;
}

std::string to_hex(const void* bytes, size_t size) {
char* buf = (char*)alloca(2 * size + 1);
return std::string(to_hex(buf, bytes, size));
}
}

struct KeyKeeper
{
KeyKeeper(const std::string& phrase)
Expand Down Expand Up @@ -57,6 +81,16 @@ struct KeyKeeper
return _impl2.GetWalletID();
}

std::string GetSbbsAddress(int ownID)
{
return _impl2.GetSbbsAddress(uint64_t(ownID));
}

std::string GetSbbsAddressPrivate(int ownID)
{
return _impl2.GetSbbsAddressPrivate(uint64_t(ownID));
}

std::string get_Kdf(bool root, Key::Index keyIndex)
{
IPrivateKeyKeeper2::Method::get_Kdf method;
Expand Down Expand Up @@ -253,6 +287,7 @@ struct KeyKeeper
}

private:

struct MyKeeKeeper
: public LocalPrivateKeyKeeperStd
{
Expand All @@ -271,6 +306,94 @@ struct KeyKeeper
pid.FromSk(sk);
return pid.str();
}

std::string GetSbbsAddress(uint64_t ownID)
{
WalletID walletID;
get_SbbsWalletID(walletID, ownID);
return std::to_string(walletID);
}

std::string GetSbbsAddressPrivate(uint64_t ownID)
{
ECC::Scalar::Native sk;
WalletID walletID;
get_SbbsWalletID(sk, walletID, ownID);
return ECC::Scalar(sk).m_Value.str();
}

Key::IKdf::Ptr CreateSbbsKdf()
{
IPrivateKeyKeeper2::Method::get_Kdf m;
// trustless mode. create SBBS Kdf from a child PKdf. It won't be directly accessible from the owner key
m.m_Root = false;
m.m_iChild = Key::Index(-1); // definitely won't collude with a coin child Kdf (for coins high byte is reserved for scheme)

InvokeSync(m);

ECC::Scalar::Native sk;
m.m_pPKdf->DerivePKey(sk, Zero);

ECC::NoLeak<ECC::Scalar> s;
s.V = sk;

Key::IKdf::Ptr kdf;
ECC::HKdf::Create(kdf, s.V.m_Value);
return kdf;
}

// copied from wallet_db.cpp
// TODO move to common place
Key::IKdf::Ptr get_SbbsKdf()// const
{
if (!m_pKdfSbbs)
{
m_pKdfSbbs = CreateSbbsKdf();
}
return m_pKdfSbbs;
}

void get_SbbsPeerID(ECC::Scalar::Native& sk, PeerID& pid, uint64_t ownID)
{
Key::IKdf::Ptr pKdfSbbs = get_SbbsKdf();
// if (!pKdfSbbs)
// throw CannotGenerateSecretException();

ECC::Hash::Value hv;
Key::ID(ownID, Key::Type::Bbs).get_Hash(hv);

pKdfSbbs->DeriveKey(sk, hv);
pid.FromSk(sk);
}

void get_SbbsWalletID(ECC::Scalar::Native& sk, WalletID& wid, uint64_t ownID)
{
get_SbbsPeerID(sk, wid.m_Pk, ownID);

// derive the channel from the address
BbsChannel ch;
wid.m_Pk.ExportWord<0>(ch);
ch %= proto::Bbs::s_MaxWalletChannels;

wid.m_Channel = ch;
}

void get_SbbsWalletID(WalletID& wid, uint64_t ownID)
{
ECC::Scalar::Native sk;
get_SbbsWalletID(sk, wid, ownID);
}

//void get_Identity(PeerID& pid, uint64_t ownID)// const
//{
// ECC::Hash::Value hv;
// Key::ID(ownID, Key::Type::WalletID).get_Hash(hv);
// ECC::Point::Native pt;
// get_OwnerKdf()->DerivePKeyG(pt, hv);
// pid = ECC::Point(pt).m_X;
//}

/*mutable*/ ECC::Key::IKdf::Ptr m_pKdfSbbs;
};
MyKeeKeeper _impl2;
};
Expand All @@ -282,6 +405,8 @@ EMSCRIPTEN_BINDINGS()
.constructor<const std::string&>()
.function("getOwnerKey", &KeyKeeper::GetOwnerKey)
.function("getWalletID", &KeyKeeper::GetWalletID)
.function("getSbbsAddress", &KeyKeeper::GetSbbsAddress)
.function("getSbbsAddressPrivate", &KeyKeeper::GetSbbsAddressPrivate)
#define THE_MACRO(method) \
.function(#method, &KeyKeeper::method)\

Expand Down
39 changes: 35 additions & 4 deletions wallet/service/client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ <h3>UTXO</h3>
Module().then(function(Module)
{
var connection = null;
var keykeeper = null
var keykeeper = null;
var walletID = null;

function showPanel(id)
{
Expand Down Expand Up @@ -212,9 +213,23 @@ <h3>UTXO</h3>
}
}

xhr.send(JSON.stringify({
WalletID: walletGUID
}))

if (keykeeper) {
var data = JSON.stringify(
{
WalletID: keykeeper.getWalletID(),
SbbsAddress: keykeeper.getSbbsAddress(8),
SbbsAddressPrivate: keykeeper.getSbbsAddressPrivate(8)
})
console.log(data)
xhr.send(data)
}
else {
xhr.send(JSON.stringify({
WalletID: walletGUID
}))
}

}

function startAliveTimer() {
Expand Down Expand Up @@ -270,6 +285,22 @@ <h3>UTXO</h3>
}
}

if (localStorage.id)
{
console.log(`walletID:${localStorage.id}`)
console.log(`Creating key keeper from stored seed ${localStorage.seed}`)
keykeeper = initKeyKeeper(localStorage.seed)
console.log(`done`)
}
else
{
console.log(`Creating key keeper from new seed`)
var seed = $('#create-wallet-panel .seed-phrase-input').val()
keykeeper = initKeyKeeper(seed)
}



login()

function initKeyKeeper(seed)
Expand Down
2 changes: 1 addition & 1 deletion wallet/service/monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ int main(int argc, char* argv[])
nnet.m_Cfg.m_vNodes.push_back(nodeAddress);
nnet.Connect();

for (BbsChannel c = 0; c < 1024; ++c)
for (BbsChannel c = 0; c < proto::Bbs::s_MaxWalletChannels; ++c)
{
nnet.BbsSubscribe(c, getTimestamp(), &monitor);
}
Expand Down

0 comments on commit cc30908

Please sign in to comment.