Skip to content

Commit

Permalink
fix(sdk-coin-xlm): deprecate eddsa key deriver for xlm
Browse files Browse the repository at this point in the history
deprecated eddsa key deriver for xlm

WP-1401

TICKET: WP-1401
  • Loading branch information
alebusse committed Feb 20, 2024
1 parent 7a24905 commit b0f9b6f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 44 deletions.
1 change: 0 additions & 1 deletion modules/sdk-coin-xlm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"dependencies": {
"@bitgo/sdk-core": "^25.1.0",
"@bitgo/statics": "^46.1.0",
"@bitgo/utxo-lib": "^9.34.0",
"bignumber.js": "^9.1.1",
"lodash": "^4.17.14",
"stellar-sdk": "^10.0.1",
Expand Down
37 changes: 6 additions & 31 deletions modules/sdk-coin-xlm/src/xlm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import assert from 'assert';
import * as _ from 'lodash';
import * as utxolib from '@bitgo/utxo-lib';
import * as querystring from 'querystring';
import * as url from 'url';
import * as request from 'superagent';
Expand All @@ -12,7 +11,6 @@ import {
BitGoBase,
checkKrsProvider,
common,
Ed25519KeyDeriver,
ExtraPrebuildParamsOptions,
InvalidAddressError,
InvalidMemoIdError,
Expand All @@ -33,6 +31,7 @@ import {
VerifyAddressOptions as BaseVerifyAddressOptions,
VerifyTransactionOptions as BaseVerifyTransactionOptions,
Wallet,
NotSupported,
} from '@bitgo/sdk-core';
import { toBitgoRequest } from '@bitgo/sdk-api';
import { getStellarKeys } from './getStellarKeys';
Expand Down Expand Up @@ -209,12 +208,8 @@ export class Xlm extends BaseCoin {
return 'https://horizon.stellar.org';
}

/**
* Generate a new key pair on the ed25519 curve
* @param seed
* @returns generated pub and prv
*/
generateKeyPair(seed: Buffer): KeyPair {
/** inheritdoc */
generateKeyPair(seed?: Buffer): KeyPair {
const pair = seed ? stellar.Keypair.fromRawEd25519Seed(seed) : stellar.Keypair.random();
return {
pub: pair.publicKey(),
Expand Down Expand Up @@ -1112,29 +1107,9 @@ export class Xlm extends BaseCoin {
return true;
}

/**
* Derive a hardened child public key from a master key seed using an additional seed for randomness.
*
* Due to technical differences between keypairs on the ed25519 curve and the secp256k1 curve,
* only hardened private key derivation is supported.
*
* @param key seed for the master key. Note: Not the public key or encoded private key. This is the raw seed.
* @param entropySeed random seed which is hashed to generate the derivation path
*/
deriveKeyWithSeed({ key, seed }: { key: string; seed: string }): { derivationPath: string; key: string } {
const derivationPathInput = utxolib.crypto.hash256(Buffer.from(seed, 'utf8')).toString('hex');
const derivationPathParts = [
999999,
parseInt(derivationPathInput.slice(0, 7), 16),
parseInt(derivationPathInput.slice(7, 14), 16),
];
const derivationPath = 'm/' + derivationPathParts.map((part) => `${part}'`).join('/');
const derivedKey = Ed25519KeyDeriver.derivePath(derivationPath, key).key;
const keypair = stellar.Keypair.fromRawEd25519Seed(derivedKey);
return {
key: keypair.publicKey(),
derivationPath,
};
/** inheritdoc */
deriveKeyWithSeed(): { derivationPath: string; key: string } {
throw new NotSupported('method deriveKeyWithSeed not supported for eddsa curve');
}

/**
Expand Down
20 changes: 8 additions & 12 deletions modules/sdk-coin-xlm/test/unit/xlm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ describe('XLM:', function () {
walletParams.rootPrivateKey.should.equal(rootPrivateKey);
});

describe('deriveKeyWithSeed', function () {
it('should derive key with seed', function () {
(() => {
basecoin.deriveKeyWithSeed('test');
}).should.throw('method deriveKeyWithSeed not supported for eddsa curve');
});
});

describe('Transaction Verification', function () {
let basecoin;
let wallet;
Expand Down Expand Up @@ -891,18 +899,6 @@ describe('XLM:', function () {
stellar.StrKey.encodeEd25519SecretSeed(seed).should.equal(secret);
});

it('should deterministically derive a child key from master seed and entropy seed', () => {
const seed = Buffer.alloc(32).fill(0).toString('hex');
const masterSeed = '0x01020304050607080910111213141516171819202122232425262728293031';

const derivedKey = basecoin.deriveKeyWithSeed({ key: masterSeed, seed });

derivedKey.should.have.properties({
key: 'GCJR3ORBWOKGFA3FTGYDDQVFEEMCYXFHY6KAUOTU4MQMFHK4LLSWWGLW',
derivationPath: "m/999999'/230673453'/206129755'",
});
});

it('should validate pub key', () => {
const { pub } = basecoin.keychains().create();
basecoin.isValidPub(pub).should.equal(true);
Expand Down

0 comments on commit b0f9b6f

Please sign in to comment.