Skip to content

Commit

Permalink
Merge pull request #4292 from BitGo/WP-1465
Browse files Browse the repository at this point in the history
fix(sdk-core): deprecate derivedHardened for eddsa KeyPair
  • Loading branch information
alebusse authored Feb 20, 2024
2 parents 2f44e86 + d3cb900 commit d96ca18
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 48 deletions.
17 changes: 0 additions & 17 deletions modules/sdk-coin-dot/test/unit/keypair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,6 @@ describe('Dot KeyPair', () => {
const keyPair = new KeyPair({ pub: bs58Account.publicKey });
should.equal(keyPair.getKeys().pub, publicKeyHexString);
});

it('should be able to derive keypair with hardened derivation', () => {
// using ed25519 (polkadot.js uses sr25519)
const keyPair = new KeyPair({
prv: account1.secretKey,
});
const derivationIndex = 0;
const derived = keyPair.deriveHardened(`m/0'/0'/0'/${derivationIndex}'`);
const derivedKeyPair = new KeyPair({
prv: derived.prv || '',
});
should.exists(derivedKeyPair.getAddress(DotAddressFormat.substrate));
should.exists(derivedKeyPair.getKeys().prv);
should.exists(derivedKeyPair.getKeys().pub);
should.equal(derivedKeyPair.getKeys().prv?.length, 64);
should.equal(derivedKeyPair.getKeys().pub?.length, 64);
});
});

describe('KeyPair validation', () => {
Expand Down
32 changes: 1 addition & 31 deletions modules/sdk-coin-sol/test/unit/keyPair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from 'assert';
import { KeyPair } from '../../src';
import should from 'should';
import * as testData from '../resources/sol';
import { isValidPublicKey, isValidAddress, isValidPrivateKey } from '../../src/lib/utils';
import { isValidPublicKey, isValidAddress } from '../../src/lib/utils';

describe('Sol KeyPair', function () {
const defaultSeed = { seed: testData.accountWithSeed.seed };
Expand Down Expand Up @@ -158,34 +158,4 @@ describe('Sol KeyPair', function () {
keyPair.verifySignature(message, signature).should.equal(false);
});
});

describe('deriveHardened', () => {
it('should derive child key pairs', () => {
const rootKeyPair = new KeyPair();
for (let i = 0; i < 50; i++) {
const path = `m/0'/0'/0'/${i}'`;
const derived = new KeyPair(rootKeyPair.deriveHardened(path));

isValidPublicKey(derived.getKeys().pub).should.be.true();
isValidAddress(derived.getAddress()).should.be.true();

const derivedPrv = derived.getKeys().prv;
should.exist(derivedPrv);
isValidPrivateKey(derivedPrv as string | Uint8Array).should.be.true();

const rederived = new KeyPair(rootKeyPair.deriveHardened(path));
rederived.getKeys().should.deepEqual(derived.getKeys());
}
});

it('should not be able to derive without private key', () => {
const rootKeyPair = new KeyPair({ pub: testData.accountWithSeed.publicKey });
assert.throws(() => rootKeyPair.deriveHardened("m/0'/0'/0'/0'"), /need private key to derive hardened keypair/);
});

it('should throw error for non-hardened path', () => {
const rootKeyPair = new KeyPair();
assert.throws(() => rootKeyPair.deriveHardened('m/0/0/0/0'), /Invalid derivation path/);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export abstract class Ed25519KeyPair implements BaseKeyPair {
* as the seed.
*
* @param path derivation path
* @deprecated
*/
deriveHardened(path: string): DefaultKeys {
if (!this.keyPair?.prv) {
Expand Down
2 changes: 2 additions & 0 deletions modules/sdk-core/src/account-lib/util/ed25519KeyDeriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface HdKeypair {
*
* https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0005.md
* https://github.com/satoshilabs/slips/blob/master/slip-0010.md
*
* @deprecated
*/
export class Ed25519KeyDeriver {
/**
Expand Down

0 comments on commit d96ca18

Please sign in to comment.