Skip to content

Commit

Permalink
feedback from PR bitpay#3692
Browse files Browse the repository at this point in the history
  • Loading branch information
leolambo committed Dec 9, 2024
1 parent 432e40f commit d118eba
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
6 changes: 3 additions & 3 deletions packages/crypto-wallet-core/src/derivation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ export interface Key {
}

export interface IDeriver {
deriveAddress(network: string, xPub: string, addressIndex: number, isChange: boolean, addressType?: string): string | void;
deriveAddress(network: string, xPub: string, addressIndex: number, isChange: boolean, addressType?: string): string;

derivePrivateKey(network: string, xPriv: string, addressIndex: number, isChange: boolean, addressType?: string): Key;

deriveAddressWithPath(network: string, xpubKey: string, path: string, addressType: string): string | void;
deriveAddressWithPath(network: string, xpubKey: string, path: string, addressType: string): string;

derivePrivateKeyWithPath(network, xprivKey: string, path: string, addressType: string): Key;

Expand All @@ -39,7 +39,7 @@ const derivers: { [chain: string]: IDeriver } = {
MATIC: new MaticDeriver(),
ARB: new ArbDeriver(),
BASE: new BaseDeriver(),
OP: new OpDeriver(),,
OP: new OpDeriver(),
SOL: new SolDeriver()
};

Expand Down
23 changes: 14 additions & 9 deletions packages/crypto-wallet-core/src/derivation/sol/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import * as web3 from '@solana/web3.js';
import { IDeriver } from '..';

import { derivePath, getPublicKey } from 'ed25519-hd-key';
import { derivePath } from 'ed25519-hd-key';

const BitcoreLib = require('bitcore-lib');
const Base58 = BitcoreLib.encoding.Base58;

export class SolDeriver implements IDeriver {
deriveAddress(network, xpubkey, addressIndex, isChange) {
throw new Error('Cannot derive solona addresses from just xpubkey, need to use derivePrivateKeyWithPath');
deriveAddress(_network, _xpubkey, _addressIndex, _isChange) {
if (true) {
throw new Error('Cannot derive solona addresses from just xpubkey, need to use derivePrivateKeyWithPath');
}
return '';
}

deriveAddressWithPath(network: string, xpubKey: string, path: string) {
throw new Error('Cannot derive solona addresses from just xpubkey, need to use derivePrivateKeyWithPath');
deriveAddressWithPath(_network: string, _xpubKey: string, _path: string) {
if (true) {
throw new Error('Cannot derive solona addresses from just xpubkey, need to use derivePrivateKeyWithPath');
}
return '';
}

getAddress(network: string, pubKey) {
Expand All @@ -21,16 +26,16 @@ export class SolDeriver implements IDeriver {
}

addressFromPublicKeyBuffer(pubKey: Buffer): string {
return Base58.fromBuffer(pubKey).toString();
return new web3.PublicKey(pubKey).toString();
}

derivePrivateKey(network, xPriv, addressIndex, isChange) {
const changeNum = isChange ? 1 : 0;
const path = `m/${changeNum}'/${addressIndex}'`;
const path = `m/${addressIndex}'/${changeNum}'`;
return this.derivePrivateKeyWithPath(network, xPriv, path);
}

derivePrivateKeyWithPath(network: string, seed: string, path: string) {
derivePrivateKeyWithPath(_network: string, seed: string, path: string) {
const keypair = web3.Keypair.fromSeed(derivePath(path, seed).key);
const privKey = keypair.secretKey.toString();
const pubKey = keypair.publicKey.toBase58();
Expand Down

0 comments on commit d118eba

Please sign in to comment.