Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure derivationPath is spreadable #118

Merged
merged 3 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
mvayngrib marked this conversation as resolved.
Show resolved Hide resolved
})

// 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
Loading