-
Notifications
You must be signed in to change notification settings - Fork 54
/
cip36KeyAgents.test.ts
69 lines (64 loc) · 3.15 KB
/
cip36KeyAgents.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { AddressType, Ed25519KeyPair, InMemoryKeyAgent, KeyAgent, util } from '@cardano-sdk/key-management';
import { Cardano } from '@cardano-sdk/core';
import { cip36 } from '../../src';
import { testKeyAgent } from '../../../key-management/test/mocks';
describe('cip36', () => {
// This is the KeyAgent used by the wallet, that has access to private stake key.
// Right now it will only work with InMemoryKeyAgent because HW implementations lack `signBlob`.
// However, it should be possible to implement `signBlob` for HW devices in the future.
let walletKeyAgent: KeyAgent;
beforeAll(async () => {
walletKeyAgent = await testKeyAgent();
});
describe('with some voting key chosen by the wallet', () => {
let votingKeyPair: Ed25519KeyPair;
beforeAll(async () => {
// This is the KeyAgent used to create voting key pair:
// - it can be the same key agent that the wallet uses,
// then voting key would be derived from the same seedphrase.
// - it can also be a separate key agent, then it would be using a different seedphrase,
// but right now it's the only way to support voting when stake key is controlled by a HW device.
const votingKeyAgent: InMemoryKeyAgent = (await testKeyAgent()) as unknown as InMemoryKeyAgent;
votingKeyPair = await util.toEd25519KeyPair(
await votingKeyAgent.exportExtendedKeyPair([
cip36.VotingKeyDerivationPath.PURPOSE,
cip36.VotingKeyDerivationPath.COIN_TYPE,
walletKeyAgent.accountIndex, // using same account index as wallet's key agent here
0, // chain as per cip36
0 // address_index as per cip36
]),
votingKeyAgent.cryptoProvider
);
});
it('can create cip36 voting registration metadata', async () => {
// Just ensuring we have some address. SingleAddressWallet already does this internally.
await walletKeyAgent.deriveAddress({ index: 0, type: AddressType.External });
// SingleAddressWallet uses a single reward account, so it can be taken from any GroupedAddress
const rewardAccount = walletKeyAgent.knownAddresses[0].rewardAccount;
// InMemoryKeyAgent uses this derivation path for stake key.
const stakeKey = await walletKeyAgent.derivePublicKey(util.STAKE_KEY_DERIVATION_PATH);
// "Delegating" voting power to your own voting key
const delegations: cip36.GovernanceKeyDelegation[] = [
{
votingKey: votingKeyPair.vkey,
weight: 1
}
];
const votingRegistrationMetadata: Cardano.TxMetadata = cip36.metadataBuilder.buildVotingRegistration({
delegations,
purpose: cip36.VotingPurpose.CATALYST,
rewardAccount,
stakeKey
});
const signedVotingRegistrationMetadata: Cardano.TxMetadata = await cip36.metadataBuilder.signVotingRegistration(
votingRegistrationMetadata,
{
// signing metadata with your wallet's stake key
signBlob: (blob) =>
walletKeyAgent.signBlob(util.STAKE_KEY_DERIVATION_PATH, blob).then(({ signature }) => signature)
}
);
expect(signedVotingRegistrationMetadata.size).toBe(2);
});
});
});