UNPKG

@julesl23/s5js

Version:

Enhanced TypeScript SDK for S5 decentralized storage with path-based API, media processing, and directory utilities

73 lines 3.47 kB
import * as msgpackr from 'msgpackr'; import { deriveHashInt } from '../util/derive_hash.js'; import { validatePhrase } from './seed_phrase/seed_phrase.js'; const authPayloadVersion1 = 0x01; const mainIdentityTweak = 0; // public const publicIdentityTweak = 1; const signingKeyPairTweak = 2; const encryptionKeyPairTweak = 3; const resolverLinksTweak = 4; const publicReservedTweak1 = 5; const publicReservedTweak2 = 6; // private const privateDataTweak = 64; const storageServiceAccountsTweak = 65; const hiddenDBTweak = 66; const fileSystemTweak = 67; const privateReservedTweak1 = 68; const privateReservedTweak2 = 69; const extensionTweak = 127; export class S5UserIdentity { seeds = new Map(); constructor(seeds) { this.seeds = seeds; } static unpack(bytes) { const seeds = new msgpackr.Unpackr({ useRecords: true, variableMapSize: true }).unpack(bytes); return new S5UserIdentity(seeds); } pack() { return new msgpackr.Packr({ useRecords: true, variableMapSize: true }).pack(this.seeds); } static async fromSeedPhrase(seedPhrase, crypto) { return new S5UserIdentity(await this.generateSeedMapFromSeedPhrase(seedPhrase, crypto)); } static async generateSeedMapFromSeedPhrase(seedPhrase, crypto) { const full = false; const seedEntropy = validatePhrase(seedPhrase, crypto); const seedBytes = crypto.hashBlake3Sync(seedEntropy[2]); const mainIdentitySeed = deriveHashInt(seedBytes, mainIdentityTweak, crypto); const publicIdentitySeed = deriveHashInt(mainIdentitySeed, publicIdentityTweak, crypto); const keyRotationIndex = 0; const publicSubSeed = deriveHashInt(publicIdentitySeed, keyRotationIndex, crypto); const privateDataSeed = deriveHashInt(mainIdentitySeed, privateDataTweak, crypto); const privateSubSeed = deriveHashInt(privateDataSeed, keyRotationIndex, crypto); const seeds = new Map(); seeds.set(signingKeyPairTweak, deriveHashInt(publicSubSeed, signingKeyPairTweak, crypto)); seeds.set(encryptionKeyPairTweak, deriveHashInt(publicSubSeed, encryptionKeyPairTweak, crypto)); seeds.set(resolverLinksTweak, deriveHashInt(publicSubSeed, resolverLinksTweak, crypto)); seeds.set(publicReservedTweak1, deriveHashInt(publicSubSeed, publicReservedTweak1, crypto)); seeds.set(publicReservedTweak2, deriveHashInt(publicSubSeed, publicReservedTweak2, crypto)); seeds.set(storageServiceAccountsTweak, deriveHashInt(privateSubSeed, storageServiceAccountsTweak, crypto)); seeds.set(hiddenDBTweak, deriveHashInt(privateSubSeed, hiddenDBTweak, crypto)); seeds.set(fileSystemTweak, deriveHashInt(privateSubSeed, fileSystemTweak, crypto)); seeds.set(privateReservedTweak1, deriveHashInt(privateSubSeed, privateReservedTweak1, crypto)); seeds.set(privateReservedTweak2, deriveHashInt(privateSubSeed, privateReservedTweak2, crypto)); seeds.set(extensionTweak, deriveHashInt(privateSubSeed, extensionTweak, crypto)); if (full) { seeds.set(publicIdentityTweak, publicIdentitySeed); } return seeds; } get fsRootKey() { return this.seeds.get(fileSystemTweak); } get hiddenDBKey() { return this.seeds.get(hiddenDBTweak); } get portalAccountSeed() { return this.seeds.get(storageServiceAccountsTweak); } } //# sourceMappingURL=identity.js.map