1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-24 00:18:26 +00:00
trezor-firmware/emscripten/post.js

115 lines
2.9 KiB
JavaScript
Raw Normal View History

/*
typedef struct {
uint32_t depth;
uint32_t fingerprint;
uint32_t child_num;
uint8_t chain_code[32];
uint8_t private_key[32];
uint8_t public_key[33];
} HDNode;
*/
var HEAPU8 = Module['HEAPU8'];
var _malloc = Module['_malloc'];
2016-05-12 17:29:24 +00:00
var _hdnode_public_ckd_address_optimized = Module['_hdnode_public_ckd_address_optimized'];
var _ecdsa_read_pubkey = Module['_ecdsa_read_pubkey'];
var Pointer_stringify = Module['Pointer_stringify'];
2016-05-12 17:29:24 +00:00
// HDNode structs global
var PUBPOINT_SIZE = 2 * 9 * 4; // (2 * bignum256 (= 9 * uint32_t))
var _pubpoint = _malloc(PUBPOINT_SIZE);
var PUBKEY_SIZE = 33;
var _pubkey = _malloc(PUBKEY_SIZE);
var CHAINCODE_SIZE = 32;
var _chaincode = _malloc(CHAINCODE_SIZE);
// address string global
2017-03-30 15:53:09 +00:00
var ADDRESS_SIZE = 60; // maximum size
var _address = _malloc(ADDRESS_SIZE);
/*
* public library interface
*/
/**
2015-11-06 13:52:25 +00:00
* @param {HDNode} node HDNode struct, see the definition above
*/
function serializeNode(node) {
2016-05-12 17:29:24 +00:00
var u8_pubkey = new Uint8Array(33);
u8_pubkey.set(node['public_key'], 0);
HEAPU8.set(u8_pubkey, _pubkey);
2016-05-12 17:29:24 +00:00
var u8_chaincode = new Uint8Array(32);
u8_chaincode.set(node['chain_code'], 0);
HEAPU8.set(u8_chaincode, _chaincode);
2016-05-12 17:29:24 +00:00
_ecdsa_read_pubkey(0, _pubkey, _pubpoint);
}
/**
2015-11-06 13:52:25 +00:00
* @param {Number} index BIP32 index of the address
* @param {Number} version address version byte
* @return {String}
*/
2016-05-12 17:29:24 +00:00
function deriveAddress(index, version) {
2017-03-30 15:53:09 +00:00
_hdnode_public_ckd_address_optimized(_pubpoint, _chaincode, index, version, _address, ADDRESS_SIZE);
return Pointer_stringify(_address);
}
/**
2015-11-06 13:52:25 +00:00
* @param {HDNode} node HDNode struct, see the definition above
* @param {Number} firstIndex index of the first address
* @param {Number} lastIndex index of the last address
* @param {Number} version address version byte
* @return {Array<String>}
*/
2015-11-06 13:52:25 +00:00
function deriveAddressRange(node, firstIndex, lastIndex, version) {
var addresses = [];
2016-05-12 17:29:24 +00:00
serializeNode(node);
var i;
2015-11-06 13:52:25 +00:00
for (i = firstIndex; i <= lastIndex; i++) {
2016-05-12 17:29:24 +00:00
addresses.push(deriveAddress(i, version));
}
return addresses;
}
if (typeof module !== 'undefined') {
module['exports'] = {
'serializeNode': serializeNode,
'deriveAddress': deriveAddress,
'deriveAddressRange': deriveAddressRange
};
}
/*
* Web worker processing
*/
function processMessage(event) {
var data = event['data'];
var type = data['type'];
switch (type) {
case 'deriveAddressRange':
2015-11-06 13:52:25 +00:00
var addresses = deriveAddressRange(
data['node'],
2015-11-06 13:52:25 +00:00
data['firstIndex'],
data['lastIndex'],
data['version']
);
2015-11-06 13:52:25 +00:00
postMessage({
'addresses': addresses,
'firstIndex': data['firstIndex'],
'lastIndex': data['lastIndex']
});
break;
default:
throw new Error('Unknown message type: ' + type);
}
}
if (ENVIRONMENT_IS_WORKER) {
this['onmessage'] = processMessage;
}