Skip to content

Commit

Permalink
fix: ensure derivationPath is spreadable (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
sparten11740 authored Jul 15, 2024
1 parent b49dbaa commit a27ee3c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
25 changes: 25 additions & 0 deletions libraries/key-identifier/__tests__/key-identifier.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,31 @@ describe('KeyIdentifier', () => {
expect(keyId.derivationPath).toBe("m/44'/60'/0'/0/0")
})

test('spreads all properties', () => {
const properties = {
assetName: 'wayne-coin',
derivationAlgorithm: 'BIP32',
derivationPath: "m/44'/60'/0'",
keyType: 'secp256k1',
}

const keyId = new KeyIdentifier(properties)

expect({ ...keyId }).toEqual(properties)
})

test('cannot write derivationPath', () => {
const keyId = new KeyIdentifier({
derivationAlgorithm: 'BIP32',
assetName: 'ethereum',
derivationPath: ['m', "44'", "60'", "0'", '0', '0'],
})

expect(() => {
keyId.derivationPath = 'm/44/60/0/0/0'
}).toThrow()
})

describe('.extend()', () => {
test('extends derivation path', () => {
const keyId = new KeyIdentifier({
Expand Down
2 changes: 1 addition & 1 deletion libraries/key-identifier/src/key-identifier.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class KeyIdentifier {

constructor(params: ConstructorParams)

get derivationPath(): string
readonly derivationPath: string

/**
* Returns a new KeyIdentifier instance that has an updated derivation path extended with
Expand Down
9 changes: 5 additions & 4 deletions libraries/key-identifier/src/key-identifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ export default class KeyIdentifier {
? derivationPath
: DerivationPath.from(derivationPath)

Object.defineProperty(this, 'derivationPath', {
value: this.#derivationPath.toString(),
enumerable: true,
})

// Freeze the object on construction, disallow tampering with derivation path.
// Ensures immutability of key identifiers passed to keychain.
Object.freeze(this)
}

get derivationPath() {
return this.#derivationPath.toString()
}

derive(pathLike) {
return new KeyIdentifier({
...this,
Expand Down

0 comments on commit a27ee3c

Please sign in to comment.