meta-log-db
Version:
Native database package for Meta-Log (ProLog, DataLog, R5RS)
164 lines • 6.23 kB
JavaScript
;
/**
* BIP44 Wallet Derivation Paths
*
* Implements BIP44 standard derivation paths for deterministic wallet generation
* Based on BIP44 specification: https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.StorageDerivationPaths = exports.StandardPaths = void 0;
exports.getDerivationPath = getDerivationPath;
exports.deriveStorageKey = deriveStorageKey;
exports.deriveStorageKeyFromPath = deriveStorageKeyFromPath;
/**
* BIP44 derivation path format: m / purpose' / coin_type' / account' / change / address_index
*
* @param purpose - Purpose (44 for BIP44)
* @param coin - Coin type (0 for Bitcoin, 60 for Ethereum)
* @param account - Account index
* @param change - Change chain (0 for external, 1 for internal)
* @param address - Address index
* @returns Derivation path string
*/
function getDerivationPath(purpose = 44, coin = 0, account = 0, change = 0, address = 0) {
return `m/${purpose}'/${coin}'/${account}'/${change}/${address}`;
}
/**
* Standard BIP44 derivation paths for common cryptocurrencies
*/
exports.StandardPaths = {
// Bitcoin
bitcoin: (account = 0, change = 0, address = 0) => getDerivationPath(44, 0, account, change, address),
// Ethereum
ethereum: (account = 0, change = 0, address = 0) => getDerivationPath(44, 60, account, change, address),
// Litecoin
litecoin: (account = 0, change = 0, address = 0) => getDerivationPath(44, 2, account, change, address),
// Custom coin type
custom: (coinType, account = 0, change = 0, address = 0) => getDerivationPath(44, coinType, account, change, address),
};
/**
* Storage key derivation paths for Meta-Log Database
*
* These are custom derivation paths for different storage purposes:
* - Local/private workspace keys
* - Published content root keys
* - Contributor signing keys
* - Ephemeral sharing keys
*/
exports.StorageDerivationPaths = {
/**
* Local/private workspace keys
* Path: m/44'/999'/0'/0/0
*/
LOCAL_PRIVATE: "m/44'/999'/0'/0/0",
/**
* Published content root keys
* Path: m/44'/999'/1'/0/0
*/
PUBLISHED_ROOT: "m/44'/999'/1'/0/0",
/**
* Contributor signing keys
* Path: m/44'/999'/2'/0/0
*/
CONTRIBUTOR_SIGNING: "m/44'/999'/2'/0/0",
/**
* Ephemeral sharing keys
* Path: m/44'/999'/3'/0/0
*/
EPHEMERAL_SHARING: "m/44'/999'/3'/0/0",
/**
* Helper: Published manifest key
* Path: m/44'/999'/1'/{topicIndex}'/{nodeIndex}'
*/
publishedManifest: (topicIndex, nodeIndex) => `m/44'/999'/1'/${topicIndex}'/${nodeIndex}'`,
/**
* Helper: Contributor key
* Path: m/44'/999'/2'/{contributorIndex}'
*/
contributorKey: (contributorIndex) => `m/44'/999'/2'/${contributorIndex}'`,
/**
* Helper: Sharing key
* Path: m/44'/999'/3'/{shareIndex}'/{sessionIndex}'
*/
sharingKey: (shareIndex, sessionIndex) => `m/44'/999'/3'/${shareIndex}'/${sessionIndex}'`,
};
/**
* Derive storage key from mnemonic and purpose
*
* @param mnemonic - BIP39 mnemonic phrase
* @param purpose - Storage purpose ('local' | 'published' | 'contributor' | 'ephemeral')
* @returns CryptoKey for encryption/decryption
*/
async function deriveStorageKey(mnemonic, purpose) {
const { mnemonicToSeed } = await Promise.resolve().then(() => __importStar(require('./bip39.js')));
const { deriveKey } = await Promise.resolve().then(() => __importStar(require('./bip32.js')));
// Convert mnemonic to seed
const seed = await mnemonicToSeed(mnemonic);
// Get derivation path based on purpose
let path;
switch (purpose) {
case 'local':
path = exports.StorageDerivationPaths.LOCAL_PRIVATE;
break;
case 'published':
path = exports.StorageDerivationPaths.PUBLISHED_ROOT;
break;
case 'contributor':
path = exports.StorageDerivationPaths.CONTRIBUTOR_SIGNING;
break;
case 'ephemeral':
path = exports.StorageDerivationPaths.EPHEMERAL_SHARING;
break;
default:
throw new Error(`Unknown purpose: ${purpose}`);
}
// Derive key from seed and path
return await deriveKey(seed, path);
}
/**
* Derive storage key with custom path
*
* @param mnemonic - BIP39 mnemonic phrase
* @param path - Custom derivation path
* @returns CryptoKey for encryption/decryption
*/
async function deriveStorageKeyFromPath(mnemonic, path) {
const { mnemonicToSeed } = await Promise.resolve().then(() => __importStar(require('./bip39.js')));
const { deriveKey } = await Promise.resolve().then(() => __importStar(require('./bip32.js')));
const seed = await mnemonicToSeed(mnemonic);
return await deriveKey(seed, path);
}
//# sourceMappingURL=bip44.js.map