diff --git a/packages/hdwallet-native/src/crypto/isolation/adapters/solana.ts b/packages/hdwallet-native/src/crypto/isolation/adapters/solana.ts index 4d520a790..e4d5a320c 100644 --- a/packages/hdwallet-native/src/crypto/isolation/adapters/solana.ts +++ b/packages/hdwallet-native/src/crypto/isolation/adapters/solana.ts @@ -13,12 +13,10 @@ export class SolanaDirectAdapter { async getAddress(addressNList: core.BIP32Path): Promise { const nodeAdapter = await this.nodeAdapter.derivePath(core.addressNListToBIP32(addressNList)); const publicKeyBuffer = nodeAdapter.getPublicKey(); - // Convert the public key to Solana format by reversing the bytes, something something Little vs. Big Endian - const solanaFormat = Buffer.from(publicKeyBuffer).reverse(); - return new PublicKey(solanaFormat).toBase58(); + // PublicKey constructor in Solana expects the key in big-endian format + return new PublicKey(publicKeyBuffer).toBase58(); } - async signDirect(transaction: VersionedTransaction, addressNList: core.BIP32Path): Promise { const nodeAdapter = await this.nodeAdapter.derivePath(core.addressNListToBIP32(addressNList)); const pubkey = await this.getAddress(addressNList); diff --git a/packages/hdwallet-native/src/crypto/isolation/core/ed25519/index.ts b/packages/hdwallet-native/src/crypto/isolation/core/ed25519/index.ts index 57411c795..0d84d3d14 100644 --- a/packages/hdwallet-native/src/crypto/isolation/core/ed25519/index.ts +++ b/packages/hdwallet-native/src/crypto/isolation/core/ed25519/index.ts @@ -73,23 +73,24 @@ export class Ed25519Node extends Revocable(class {}) implements Ed25519Key { } async derive(index: number): Promise { + // Ensure hardened derivation + if (index < 0x80000000) { + index += 0x80000000; + } + const indexBuffer = Buffer.alloc(4); indexBuffer.writeUInt32BE(index, 0); - const data = Buffer.concat([ - Buffer.from([0x00]), // Hardened derivation prefix - this.#privateKey, // Private key - indexBuffer, // Index - ]); + const data = Buffer.concat([Buffer.from([0x00]), this.#privateKey, indexBuffer]); const hmac = createHmac("sha512", this.#chainCode); hmac.update(data); const I = hmac.digest(); - const IL = I.slice(0, 32); // Private key - const IR = I.slice(32); // Chain code + const IL = I.slice(0, 32); + const IR = I.slice(32); - // ED25519 key clamping, whatever that means + // Apply clamping as per RFC 8032 IL[0] &= 0xf8; IL[31] &= 0x7f; IL[31] |= 0x40;