Skip to content

Commit

Permalink
feat: add deriveAddress and stakeKey to the KeyManager
Browse files Browse the repository at this point in the history
- `NetworkId` to now a parameter of `createInMemoryKeyManager`
  • Loading branch information
rhyslbw committed Sep 24, 2021
1 parent 1ca4335 commit ca9d6ed
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/in-memory-key-manager/package.json
Expand Up @@ -22,6 +22,7 @@
"shx": "^0.3.3"
},
"dependencies": {
"@cardano-sdk/core": "0.1.0",
"@cardano-sdk/wallet": "0.1.0",
"bip39": "^3.0.4"
}
Expand Down
28 changes: 22 additions & 6 deletions packages/in-memory-key-manager/src/InMemoryKey.ts
Expand Up @@ -3,18 +3,21 @@ import * as bip39 from 'bip39';
import CardanoSerializationLib from '@emurgo/cardano-serialization-lib-nodejs';
import { KeyManagement } from '@cardano-sdk/wallet';
import { harden } from './util';
import { Cardano } from '@cardano-sdk/core';

/**
*
*/
export const createInMemoryKeyManager = ({
password,
accountIndex,
mnemonic
mnemonic,
networkId
}: {
password: string;
accountIndex?: number;
mnemonic: string;
networkId: Cardano.NetworkId;
}): KeyManagement.KeyManager => {
if (!accountIndex) {
accountIndex = 0;
Expand All @@ -24,18 +27,30 @@ export const createInMemoryKeyManager = ({
if (!validMnemonic) throw new KeyManagement.errors.InvalidMnemonic();

const entropy = bip39.mnemonicToEntropy(mnemonic);
const account = CardanoSerializationLib.Bip32PrivateKey.from_bip39_entropy(
const accountPrivateKey = CardanoSerializationLib.Bip32PrivateKey.from_bip39_entropy(
Buffer.from(entropy, 'hex'),
Buffer.from(password)
)
.derive(harden(1852))
.derive(harden(1815))
.derive(harden(accountIndex));

const privateParentKey = account.derive(0).derive(0);
const publicParentKey = privateParentKey.to_public().to_raw_key();
const privateParentKey = accountPrivateKey.derive(0).derive(0);
const publicParentKey = privateParentKey.to_public();
const publicKey = accountPrivateKey.to_public();
const stakeKey = publicKey.derive(2).derive(0);

return {
deriveAddress: (addressIndex, index) => {
const utxoPubKey = publicKey.derive(index).derive(addressIndex);
const baseAddr = CardanoSerializationLib.BaseAddress.new(
networkId,
CardanoSerializationLib.StakeCredential.from_keyhash(utxoPubKey.to_raw_key().hash()),
CardanoSerializationLib.StakeCredential.from_keyhash(stakeKey.to_raw_key().hash())
);

return baseAddr.to_address().to_bech32();
},
signMessage: async (_addressType, _signingIndex, message) => ({
publicKey: publicParentKey.toString(),
signature: `Signature for ${message} is not implemented yet`
Expand All @@ -48,7 +63,8 @@ export const createInMemoryKeyManager = ({
witnessSet.set_vkeys(vkeyWitnesses);
return witnessSet;
},
publicKey: account.to_public().to_raw_key(),
publicParentKey
stakeKey: stakeKey.to_raw_key(),
publicKey: publicKey.to_raw_key(),
publicParentKey: publicParentKey.to_raw_key()
};
};
@@ -1,6 +1,7 @@
import { createInMemoryKeyManager } from '@src/InMemoryKey';
import { util } from '@src/.';
import { loadCardanoSerializationLib } from '@cardano-sdk/cardano-serialization-lib';
import { Cardano } from '@cardano-sdk/core';
import { KeyManagement } from '@cardano-sdk/wallet';
import CardanoSerializationLib from '@emurgo/cardano-serialization-lib-nodejs';

Expand All @@ -13,6 +14,7 @@ describe('InMemoryKeyManager', () => {
const mnemonic = util.generateMnemonic();
keyManager = createInMemoryKeyManager({
mnemonic,
networkId: Cardano.NetworkId.testnet,
password: '123'
});
expect(keyManager.publicKey).toBeInstanceOf(CardanoSerializationLib.PublicKey);
Expand All @@ -23,6 +25,12 @@ describe('InMemoryKeyManager', () => {
expect(keyManager.publicParentKey).toBeDefined();
});

test('deriveAddress', async () => {
const address = await keyManager.deriveAddress(0, 0);
expect(address).toBeDefined();
expect(keyManager.publicParentKey).toBeDefined();
});

test('signTransaction', async () => {
const txHash = cardanoWasm.TransactionHash.from_bytes(
Buffer.from('8561258e210352fba2ac0488afed67b3427a27ccf1d41ec030c98a8199bc22ec', 'hex')
Expand Down
1 change: 1 addition & 0 deletions packages/in-memory-key-manager/test/tsconfig.json
Expand Up @@ -8,6 +8,7 @@
},
"references": [
{ "path": "../src" },
{ "path": "../../core/src" },
{ "path": "../../wallet/src" }
]
}
2 changes: 2 additions & 0 deletions packages/wallet/src/KeyManagement/KeyManager.ts
Expand Up @@ -2,6 +2,7 @@ import CardanoSerializationLib from '@emurgo/cardano-serialization-lib-nodejs';
import { Address } from '../';

export interface KeyManager {
deriveAddress: (addressIndex: number, index: 0 | 1) => string;
signMessage: (
addressType: Address.AddressType,
signingIndex: number,
Expand All @@ -12,4 +13,5 @@ export interface KeyManager {
signTransaction: (
txHash: CardanoSerializationLib.TransactionHash
) => Promise<CardanoSerializationLib.TransactionWitnessSet>;
stakeKey: CardanoSerializationLib.PublicKey;
}

0 comments on commit ca9d6ed

Please sign in to comment.