@@ -3,46 +3,15 @@ import * as Trezor from '@trezor/connect';
3
3
import { BIP32Path } from '@cardano-sdk/crypto' ;
4
4
import { Cardano } from '@cardano-sdk/core' ;
5
5
import { GroupedAddress , util } from '@cardano-sdk/key-management' ;
6
- import { InvalidArgumentError /* , Transform*/ } from '@cardano-sdk/util' ;
6
+ import { InvalidArgumentError , Transform } from '@cardano-sdk/util' ;
7
7
import { TrezorTxTransformerContext } from '../types' ;
8
8
9
- type StakeKeyCertificateType =
10
- | Trezor . PROTO . CardanoCertificateType . STAKE_REGISTRATION
11
- | Trezor . PROTO . CardanoCertificateType . STAKE_DEREGISTRATION ;
12
-
13
- type TrezorStakeKeyCertificate = {
14
- type : StakeKeyCertificateType ;
15
- path ?: BIP32Path ;
9
+ type CertCredentialsType = {
16
10
scriptHash ?: Crypto . Ed25519KeyHashHex ;
17
11
keyHash ?: Crypto . Ed25519KeyHashHex ;
18
- } ;
19
-
20
- type TrezorDelegationCertificate = {
21
- type : Trezor . PROTO . CardanoCertificateType . STAKE_DELEGATION ;
22
12
path ?: BIP32Path ;
23
- scriptHash ?: Crypto . Ed25519KeyHashHex ;
24
- pool : string ;
25
- } ;
26
-
27
- type TrezorPoolRegistrationCertificate = {
28
- poolParameters : Trezor . CardanoPoolParameters ;
29
- type : Trezor . PROTO . CardanoCertificateType . STAKE_POOL_REGISTRATION ;
30
- } ;
31
-
32
- type ScriptHashCertCredentials = {
33
- scriptHash : Crypto . Ed25519KeyHashHex ;
34
- } ;
35
-
36
- type KeyHashCertCredentials = {
37
- keyHash : Crypto . Ed25519KeyHashHex ;
38
13
} ;
39
14
40
- type PathCertCredentials = {
41
- path : BIP32Path ;
42
- } ;
43
-
44
- type CertCredentialsType = ScriptHashCertCredentials | KeyHashCertCredentials | PathCertCredentials ;
45
-
46
15
const getCertCredentials = (
47
16
stakeKeyHash : Crypto . Ed25519KeyHashHex ,
48
17
knownAddresses : GroupedAddress [ ] | undefined
@@ -61,57 +30,73 @@ const getCertCredentials = (
61
30
} ;
62
31
} ;
63
32
64
- const getStakeAddressCertificate = (
65
- certificate : Cardano . StakeAddressCertificate ,
66
- context : TrezorTxTransformerContext ,
67
- type : StakeKeyCertificateType
68
- ) : TrezorStakeKeyCertificate => {
33
+ const toPoolMetadata = ( metadataJson : Cardano . PoolMetadataJson ) : Trezor . CardanoPoolMetadata => ( {
34
+ hash : metadataJson . hash ,
35
+ url : metadataJson . url
36
+ } ) ;
37
+
38
+ const getPoolOperatorKeyPath = (
39
+ operator : Cardano . RewardAccount ,
40
+ context : TrezorTxTransformerContext
41
+ ) : BIP32Path | null => {
42
+ const knownAddress = context ?. knownAddresses . find ( ( address ) => address . rewardAccount === operator ) ;
43
+ return util . stakeKeyPathFromGroupedAddress ( knownAddress ) ;
44
+ } ;
45
+
46
+ export const getStakeAddressCertificate : Transform <
47
+ Cardano . StakeAddressCertificate ,
48
+ Trezor . CardanoCertificate ,
49
+ TrezorTxTransformerContext
50
+ > = ( certificate , context ) => {
69
51
const credentials = getCertCredentials (
70
52
certificate . stakeCredential . hash as unknown as Crypto . Ed25519KeyHashHex ,
71
- context . knownAddresses
53
+ context ? .knownAddresses
72
54
) ;
55
+ const certificateType =
56
+ certificate . __typename === Cardano . CertificateType . StakeRegistration
57
+ ? Trezor . PROTO . CardanoCertificateType . STAKE_REGISTRATION
58
+ : Trezor . PROTO . CardanoCertificateType . STAKE_DEREGISTRATION ;
73
59
return {
74
- ...credentials ,
75
- type
60
+ keyHash : credentials . keyHash ,
61
+ path : credentials . path ,
62
+ pool : undefined ,
63
+ poolParameters : undefined ,
64
+ scriptHash : credentials . scriptHash ,
65
+ type : certificateType
76
66
} ;
77
67
} ;
78
68
79
- const getStakeDelegationCertificate = (
80
- certificate : Cardano . StakeDelegationCertificate ,
81
- context : TrezorTxTransformerContext
82
- ) : TrezorDelegationCertificate => {
69
+ export const getStakeDelegationCertificate : Transform <
70
+ Cardano . StakeDelegationCertificate ,
71
+ Trezor . CardanoCertificate ,
72
+ TrezorTxTransformerContext
73
+ > = ( certificate , context ) => {
83
74
const poolIdKeyHash = Cardano . PoolId . toKeyHash ( certificate . poolId ) ;
84
75
const credentials = getCertCredentials (
85
76
certificate . stakeCredential . hash as unknown as Crypto . Ed25519KeyHashHex ,
86
- context . knownAddresses
77
+ context ? .knownAddresses
87
78
) ;
88
79
return {
89
- ...credentials ,
80
+ keyHash : credentials . keyHash ,
81
+ path : credentials . path ,
90
82
pool : poolIdKeyHash ,
83
+ poolParameters : undefined ,
84
+ scriptHash : credentials . scriptHash ,
91
85
type : Trezor . PROTO . CardanoCertificateType . STAKE_DELEGATION
92
86
} ;
93
87
} ;
94
88
95
- const toPoolMetadata = ( metadataJson : Cardano . PoolMetadataJson ) : Trezor . CardanoPoolMetadata => ( {
96
- hash : metadataJson . hash ,
97
- url : metadataJson . url
98
- } ) ;
99
-
100
- const getPoolOperatorKeyPath = (
101
- operator : Cardano . RewardAccount ,
102
- context : TrezorTxTransformerContext
103
- ) : BIP32Path | null => {
104
- const knownAddress = context ?. knownAddresses . find ( ( address ) => address . rewardAccount === operator ) ;
105
- return util . stakeKeyPathFromGroupedAddress ( knownAddress ) ;
106
- } ;
107
-
108
- export const getPoolRegistrationCertificate = (
109
- certificate : Cardano . PoolRegistrationCertificate ,
110
- context : TrezorTxTransformerContext
111
- ) : TrezorPoolRegistrationCertificate => {
89
+ export const getPoolRegistrationCertificate : Transform <
90
+ Cardano . PoolRegistrationCertificate ,
91
+ Trezor . CardanoCertificate ,
92
+ TrezorTxTransformerContext
93
+ > = ( certificate , context ) => {
112
94
if ( ! certificate . poolParameters . metadataJson )
113
95
throw new InvalidArgumentError ( 'certificate' , 'Missing pool registration pool metadata.' ) ;
114
96
return {
97
+ keyHash : undefined ,
98
+ path : undefined ,
99
+ pool : undefined ,
115
100
poolParameters : {
116
101
cost : certificate . poolParameters . cost . toString ( ) ,
117
102
margin : {
@@ -153,16 +138,17 @@ export const getPoolRegistrationCertificate = (
153
138
rewardAccount : certificate . poolParameters . rewardAccount ,
154
139
vrfKeyHash : certificate . poolParameters . vrf
155
140
} ,
141
+ scriptHash : undefined ,
156
142
type : Trezor . PROTO . CardanoCertificateType . STAKE_POOL_REGISTRATION
157
143
} ;
158
144
} ;
159
145
160
146
const toCert = ( cert : Cardano . Certificate , context : TrezorTxTransformerContext ) => {
161
147
switch ( cert . __typename ) {
162
148
case Cardano . CertificateType . StakeRegistration :
163
- return getStakeAddressCertificate ( cert , context , Trezor . PROTO . CardanoCertificateType . STAKE_REGISTRATION ) ;
149
+ return getStakeAddressCertificate ( cert , context ) ;
164
150
case Cardano . CertificateType . StakeDeregistration :
165
- return getStakeAddressCertificate ( cert , context , Trezor . PROTO . CardanoCertificateType . STAKE_DEREGISTRATION ) ;
151
+ return getStakeAddressCertificate ( cert , context ) ;
166
152
case Cardano . CertificateType . StakeDelegation :
167
153
return getStakeDelegationCertificate ( cert , context ) ;
168
154
case Cardano . CertificateType . PoolRegistration :
0 commit comments