-
Notifications
You must be signed in to change notification settings - Fork 55
/
certificates.ts
164 lines (154 loc) · 6.09 KB
/
certificates.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import * as Crypto from '@cardano-sdk/crypto';
import * as Trezor from '@trezor/connect';
import { BIP32Path } from '@cardano-sdk/crypto';
import { Cardano } from '@cardano-sdk/core';
import { GroupedAddress, util } from '@cardano-sdk/key-management';
import { InvalidArgumentError, Transform, areNumbersEqual, areStringsEqual } from '@cardano-sdk/util';
import { TrezorTxTransformerContext } from '../types';
type CertCredentialsType = {
scriptHash?: Crypto.Ed25519KeyHashHex;
keyHash?: Crypto.Ed25519KeyHashHex;
path?: BIP32Path;
};
const getCertCredentials = (
stakeKeyHash: Crypto.Ed25519KeyHashHex,
knownAddresses: GroupedAddress[] | undefined
): CertCredentialsType => {
const knownAddress = knownAddresses?.find((address) =>
areStringsEqual(Cardano.RewardAccount.toHash(address.rewardAccount), stakeKeyHash)
);
const rewardAddress = knownAddress ? Cardano.Address.fromBech32(knownAddress.rewardAccount)?.asReward() : null;
if (!!rewardAddress && areNumbersEqual(rewardAddress?.getPaymentCredential().type, Cardano.CredentialType.KeyHash)) {
const path = util.stakeKeyPathFromGroupedAddress(knownAddress);
return path ? { path } : { keyHash: stakeKeyHash };
}
return {
scriptHash: stakeKeyHash
};
};
const toPoolMetadata = (metadataJson: Cardano.PoolMetadataJson): Trezor.CardanoPoolMetadata => ({
hash: metadataJson.hash,
url: metadataJson.url
});
const getPoolOperatorKeyPath = (
operator: Cardano.RewardAccount,
context: TrezorTxTransformerContext
): BIP32Path | null => {
const knownAddress = context?.knownAddresses.find((address) => address.rewardAccount === operator);
return util.stakeKeyPathFromGroupedAddress(knownAddress);
};
export const getStakeAddressCertificate: Transform<
Cardano.StakeAddressCertificate,
Trezor.CardanoCertificate,
TrezorTxTransformerContext
> = (certificate, context) => {
const credentials = getCertCredentials(
certificate.stakeCredential.hash as unknown as Crypto.Ed25519KeyHashHex,
context?.knownAddresses
);
const certificateType =
certificate.__typename === Cardano.CertificateType.StakeRegistration
? Trezor.PROTO.CardanoCertificateType.STAKE_REGISTRATION
: Trezor.PROTO.CardanoCertificateType.STAKE_DEREGISTRATION;
return {
keyHash: credentials.keyHash,
path: credentials.path,
pool: undefined,
poolParameters: undefined,
scriptHash: credentials.scriptHash,
type: certificateType
};
};
export const getStakeDelegationCertificate: Transform<
Cardano.StakeDelegationCertificate,
Trezor.CardanoCertificate,
TrezorTxTransformerContext
> = (certificate, context) => {
const poolIdKeyHash = Cardano.PoolId.toKeyHash(certificate.poolId);
const credentials = getCertCredentials(
certificate.stakeCredential.hash as unknown as Crypto.Ed25519KeyHashHex,
context?.knownAddresses
);
return {
keyHash: credentials.keyHash,
path: credentials.path,
pool: poolIdKeyHash,
poolParameters: undefined,
scriptHash: credentials.scriptHash,
type: Trezor.PROTO.CardanoCertificateType.STAKE_DELEGATION
};
};
export const getPoolRegistrationCertificate: Transform<
Cardano.PoolRegistrationCertificate,
Trezor.CardanoCertificate,
TrezorTxTransformerContext
> = (certificate, context) => {
if (!certificate.poolParameters.metadataJson)
throw new InvalidArgumentError('certificate', 'Missing pool registration pool metadata.');
return {
keyHash: undefined,
path: undefined,
pool: undefined,
poolParameters: {
cost: certificate.poolParameters.cost.toString(),
margin: {
denominator: certificate.poolParameters.margin.denominator.toString(),
numerator: certificate.poolParameters.margin.numerator.toString()
},
metadata: toPoolMetadata(certificate.poolParameters.metadataJson),
owners: certificate.poolParameters.owners.map((owner) => {
const poolOwnerKeyPath = getPoolOperatorKeyPath(owner, context!);
const poolOwnerKeyHash = Cardano.RewardAccount.toHash(owner);
return poolOwnerKeyPath ? { stakingKeyPath: poolOwnerKeyPath } : { stakingKeyHash: poolOwnerKeyHash };
}),
pledge: certificate.poolParameters.pledge.toString(),
poolId: Cardano.PoolId.toKeyHash(certificate.poolParameters.id),
relays: certificate.poolParameters.relays.map((relay) => {
switch (relay.__typename) {
case 'RelayByAddress':
return {
ipv4Address: relay.ipv4,
ipv6Address: relay.ipv6,
port: relay.port,
type: Trezor.PROTO.CardanoPoolRelayType.SINGLE_HOST_IP
};
case 'RelayByName':
return {
hostName: relay.hostname,
port: relay.port,
type: Trezor.PROTO.CardanoPoolRelayType.SINGLE_HOST_NAME
};
case 'RelayByNameMultihost':
return {
hostName: relay.dnsName,
type: Trezor.PROTO.CardanoPoolRelayType.MULTIPLE_HOST_NAME
};
default:
throw new InvalidArgumentError('certificate', 'Unknown relay type.');
}
}),
rewardAccount: certificate.poolParameters.rewardAccount,
vrfKeyHash: certificate.poolParameters.vrf
},
scriptHash: undefined,
type: Trezor.PROTO.CardanoCertificateType.STAKE_POOL_REGISTRATION
};
};
const toCert = (cert: Cardano.Certificate, context: TrezorTxTransformerContext) => {
switch (cert.__typename) {
case Cardano.CertificateType.StakeRegistration:
return getStakeAddressCertificate(cert, context);
case Cardano.CertificateType.StakeDeregistration:
return getStakeAddressCertificate(cert, context);
case Cardano.CertificateType.StakeDelegation:
return getStakeDelegationCertificate(cert, context);
case Cardano.CertificateType.PoolRegistration:
return getPoolRegistrationCertificate(cert, context);
default:
throw new InvalidArgumentError('cert', `Certificate ${cert.__typename} not supported.`);
}
};
export const mapCerts = (
certs: Cardano.Certificate[],
context: TrezorTxTransformerContext
): Trezor.CardanoCertificate[] => certs.map((coreCert) => toCert(coreCert, context));