rddl-network / Tasmota

Alternative firmware for ESP8266 and ESP32 based devices with easy configuration using webUI, OTA updates, automation using timers or rules, expandability and entirely local control over MQTT, HTTP, Serial or KNX. Full documentation at
https://tasmota.github.io/docs
GNU General Public License v3.0
1 stars 2 forks source link

Tasmota Node - create two derivation path (liquid, planetmint; not one) #3

Closed eckelj closed 1 year ago

eckelj commented 1 year ago

Create two derivations:

Sign challenges twice, one signature for liquid, the other for planetmint. At this point, one planetmint derivation is used (8680). We need to use another derivation to serialize the liquid public key and sign challenges (1776).

Cybnon commented 1 year ago

The feature is merged maybe we can optimize the code to create a method that takes the id of liquid ord planetmint. Instead of duplicating the code. But due to Plattformio-specific errors, I got when extracting the method, I went with the easiest approach for now. current code:

void getPlntmntKeys(){
  readSeed();
  HDNode node_planetmint;
  hdnode_from_seed( secret_seed, SEED_SIZE, SECP256K1_NAME, &node_planetmint);
  hdnode_private_ckd_prime(&node_planetmint, 44);
  hdnode_private_ckd_prime(&node_planetmint, 8680);
  hdnode_private_ckd_prime(&node_planetmint, 0);
  hdnode_private_ckd(&node_planetmint, 0);
  hdnode_private_ckd(&node_planetmint, 0);
  hdnode_fill_public_key(&node_planetmint);
  memcpy(g_priv_key, node_planetmint.private_key, 32);
  memcpy(g_pub_key, node_planetmint.public_key, PUB_KEY_SIZE);

  HDNode node_rddl;
  hdnode_from_seed( secret_seed, SEED_SIZE, SECP256K1_NAME, &node_rddl);
  hdnode_private_ckd_prime(&node_rddl, 44);
  hdnode_private_ckd_prime(&node_rddl, 1776);
  hdnode_private_ckd_prime(&node_rddl, 0);
  hdnode_private_ckd(&node_rddl, 0);
  hdnode_private_ckd(&node_rddl, 0);
  hdnode_fill_public_key(&node_rddl);
  memcpy(g_priv_key, node_rddl.private_key, 32);
  memcpy(g_pub_key, node_rddl.public_key, PUB_KEY_SIZE);

  uint8_t address_bytes[ADDRESS_TAIL] = {0};
  pubkey2address( g_pub_key, PUB_KEY_SIZE, address_bytes );
  getAddressString( address_bytes, g_address);
  uint32_t fingerprint = hdnode_fingerprint(&node_planetmint);
  int ret = hdnode_serialize_public( &node_planetmint, fingerprint, PLANETMINT_PMPB, g_ext_pub_key_planetmint, EXT_PUB_KEY_SIZE);
  int ret2 = hdnode_serialize_public( &node_rddl, fingerprint, VERSION_PUBLIC, g_ext_pub_key_liquid, EXT_PUB_KEY_SIZE);
}

the refactored code can look like this:

void deriveHDNode(HDNode* node, uint32_t prime1, uint32_t prime2) {
    hdnode_from_seed(secret_seed, SEED_SIZE, SECP256K1_NAME, node);
    hdnode_private_ckd_prime(node, 44);
    hdnode_private_ckd_prime(node, prime1);
    hdnode_private_ckd_prime(node, prime2);
    hdnode_private_ckd(node, 0);
    hdnode_private_ckd(node, 0);
    hdnode_fill_public_key(node);
}

void getPlntmntKeys() {
    readSeed();
    HDNode node_planetmint;
    HDNode node_rddl;

    // Derive node_planetmint
    deriveHDNode(&node_planetmint, 8680, 0);
    memcpy(g_priv_key, node_planetmint.private_key, 32);
    memcpy(g_pub_key, node_planetmint.public_key, PUB_KEY_SIZE);
    uint32_t fingerprint = hdnode_fingerprint(&node_planetmint);
    hdnode_serialize_public(&node_planetmint, fingerprint, PLANETMINT_PMPB, g_ext_pub_key_planetmint, EXT_PUB_KEY_SIZE);

    // Derive node_rddl
    deriveHDNode(&node_rddl, 1776, 0);
    memcpy(g_priv_key, node_rddl.private_key, 32);
    memcpy(g_pub_key, node_rddl.public_key, PUB_KEY_SIZE);
    hdnode_serialize_public(&node_rddl, fingerprint, VERSION_PUBLIC, g_ext_pub_key_liquid, EXT_PUB_KEY_SIZE);

    uint8_t address_bytes[ADDRESS_TAIL] = {0};
    pubkey2address(g_pub_key, PUB_KEY_SIZE, address_bytes);
    getAddressString(address_bytes, g_address);
}
jmastr commented 1 year ago

https://github.com/rddl-network/Tasmota/pull/5