@avalabs/hw-app-avalanche
Version:
Node API for Avalanche App (Ledger Nano S/X/S+)
95 lines (92 loc) • 3.18 kB
JavaScript
import bs58 from 'bs58';
const HARDENED = 2147483648;
function pathCoinType(path) {
if (!path.startsWith("m")) {
throw new Error(`Path should start with "m" (e.g "m/44'/5757'/5'/0/3")`);
}
const pathArray = path.split("/").slice(1);
const maybe44 = Number(pathArray[0].slice(0, -1));
if (maybe44 != 44) {
throw new Error(`Path's first element should be "44", got ${maybe44} (e.g "m/44'/5757'/5'/0/3")`);
}
return pathArray[1];
}
function serializePath(path) {
if (!path.startsWith("m")) {
throw new Error(`Path should start with "m" (e.g "m/44'/5757'/5'/0/3")`);
}
const pathArray = path.split("/");
if (pathArray.length !== 6 && pathArray.length !== 5 && pathArray.length !== 4) {
throw new Error(`Invalid path. (e.g "m/44'/5757'/5'/0/3")`);
}
const buf = Buffer.alloc(1 + (pathArray.length - 1) * 4);
buf.writeUInt8(pathArray.length - 1);
for (let i = 1; i < pathArray.length; i += 1) {
let value = 0;
let child = pathArray[i];
if (child.endsWith("'")) {
value += HARDENED;
child = child.slice(0, -1);
}
const childNumber = Number(child);
if (Number.isNaN(childNumber)) {
throw new Error(`Invalid path : ${child} is not a number. (e.g "m/44'/461'/5'/0/3")`);
}
if (childNumber >= HARDENED) {
throw new Error("Incorrect child value (bigger or equal to 0x80000000)");
}
value += childNumber;
buf.writeUInt32BE(value, 1 + 4 * (i - 1));
}
return buf;
}
function serializePathSuffix(path) {
if (path.startsWith("m")) {
throw new Error('Path suffix do not start with "m" (e.g "0/3")');
}
const pathArray = path.split("/");
if (pathArray.length !== 2) {
throw new Error('Invalid path suffix. (e.g "0/3")');
}
const buf = Buffer.alloc(1 + pathArray.length * 4);
buf.writeUInt8(pathArray.length);
for (let i = 0; i < pathArray.length; i += 1) {
let value = 0;
const child = pathArray[i];
if (child.endsWith("'")) {
throw new Error('Invalid hardened path suffix. (e.g "0/3")');
}
const childNumber = Number(child);
if (Number.isNaN(childNumber)) {
throw new Error(`Invalid path : ${child} is not a number. (e.g "0/3")`);
}
if (childNumber >= HARDENED) {
throw new Error("Incorrect child value (bigger or equal to 0x80000000)");
}
value += childNumber;
buf.writeUInt32BE(value, 1 + 4 * i);
}
return buf;
}
function serializeHrp(hrp) {
if (hrp) {
const bufHrp = Buffer.from(hrp, "ascii");
return Buffer.concat([new Uint8Array(Buffer.alloc(1, bufHrp.length)), new Uint8Array(bufHrp)]);
} else {
return Buffer.alloc(1, 0);
}
}
function serializeChainID(chainid) {
if (chainid) {
let decoded = bs58.decode(chainid);
if (decoded.length == 36) {
decoded = decoded.slice(0, 32);
} else if (decoded.length != 32) {
throw Error("ChainID was not 32 bytes long (encoded with base58)");
}
return Buffer.concat([new Uint8Array(Buffer.alloc(1, decoded.length)), new Uint8Array(Buffer.from(decoded))]);
} else {
return Buffer.alloc(1, 0);
}
}
export { pathCoinType, serializeChainID, serializeHrp, serializePath, serializePathSuffix };