-
Notifications
You must be signed in to change notification settings - Fork 54
/
certificate.ts
163 lines (152 loc) · 7.02 KB
/
certificate.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
import { CML } from '../CML';
import {
Certificate,
CertificateType,
Ed25519KeyHash,
GenesisKeyDelegationCertificate,
PoolId,
PoolMetadataJson,
PoolRegistrationCertificate,
PoolRetirementCertificate,
Relay,
RewardAccount,
StakeAddressCertificate,
StakeDelegationCertificate,
VrfVkHex
} from '../../Cardano/types';
import { Hash28ByteBase16, Hash32ByteBase16 } from '../../Cardano/util/primitives';
import { NetworkId } from '../../Cardano/NetworkId';
import { NotImplementedError, SerializationError, SerializationFailure } from '../../errors';
import { usingAutoFree } from '@cardano-sdk/util';
const stakeRegistration = (certificate: CML.StakeRegistration): StakeAddressCertificate =>
usingAutoFree((scope) => ({
__typename: CertificateType.StakeKeyRegistration,
stakeKeyHash: Ed25519KeyHash(
Buffer.from(scope.manage(scope.manage(certificate.stake_credential()).to_keyhash())!.to_bytes()).toString('hex')
)
}));
const stakeDeregistration = (certificate: CML.StakeDeregistration): StakeAddressCertificate =>
usingAutoFree((scope) => ({
__typename: CertificateType.StakeKeyDeregistration,
stakeKeyHash: Ed25519KeyHash(
Buffer.from(scope.manage(scope.manage(certificate.stake_credential()).to_keyhash())!.to_bytes()).toString('hex')
)
}));
const stakeDelegation = (certificate: CML.StakeDelegation): StakeDelegationCertificate =>
usingAutoFree((scope) => ({
__typename: CertificateType.StakeDelegation,
poolId: PoolId(scope.manage(certificate.pool_keyhash()).to_bech32('pool')),
stakeKeyHash: Ed25519KeyHash(
Buffer.from(scope.manage(scope.manage(certificate.stake_credential()).to_keyhash())!.to_bytes()).toString('hex')
)
}));
const createCardanoRelays = (relays: CML.Relays): Relay[] =>
usingAutoFree((scope) => {
const result: Relay[] = [];
for (let i = 0; i < relays.len(); i++) {
const relay = scope.manage(relays.get(i));
const relayByAddress = scope.manage(relay.as_single_host_addr());
const relayByName = scope.manage(relay.as_single_host_name());
const relayByNameMultihost = scope.manage(relay.as_multi_host_name());
if (relayByAddress) {
// RelayByAddress
result.push({
__typename: 'RelayByAddress',
ipv4: scope.manage(relayByAddress.ipv4())?.ip().join('.'),
ipv6: scope.manage(relayByAddress.ipv6())?.ip().join('.'),
port: relayByAddress.port()
});
}
if (relayByName) {
// RelayByName
result.push({
__typename: 'RelayByName',
hostname: scope.manage(relayByName.dns_name()).record(),
port: relayByName.port()
});
}
if (relayByNameMultihost) {
// RelayByNameMultihost
result.push({
__typename: 'RelayByNameMultihost',
dnsName: scope.manage(relayByNameMultihost.dns_name()).record()
});
}
}
return result;
});
const createCardanoOwners = (owners: CML.Ed25519KeyHashes, networkId: NetworkId): RewardAccount[] =>
usingAutoFree((scope) => {
const result: RewardAccount[] = [];
for (let i = 0; i < owners.len(); i++) {
const keyHash = scope.manage(owners.get(i));
const stakeCredential = scope.manage(CML.StakeCredential.from_keyhash(keyHash));
const rewardAccount = scope.manage(CML.RewardAddress.new(networkId, stakeCredential));
result.push(RewardAccount(scope.manage(rewardAccount.to_address()).to_bech32()));
}
return result;
});
const jsonMetadata = (poolMetadata?: CML.PoolMetadata): PoolMetadataJson | undefined =>
usingAutoFree((scope) => {
if (!poolMetadata) return;
return {
hash: Hash32ByteBase16(Buffer.from(scope.manage(poolMetadata.pool_metadata_hash()).to_bytes()).toString('hex')),
url: scope.manage(poolMetadata.url()).url()
};
});
export const poolRegistration = (certificate: CML.PoolRegistration): PoolRegistrationCertificate =>
usingAutoFree((scope) => {
const poolParams = scope.manage(certificate.pool_params());
const rewardAccountAddress = scope.manage(scope.manage(poolParams.reward_account()).to_address());
return {
__typename: CertificateType.PoolRegistration,
poolParameters: {
cost: BigInt(scope.manage(poolParams.cost()).to_str()),
id: PoolId(scope.manage(poolParams.operator()).to_bech32('pool')),
margin: {
denominator: Number(scope.manage(scope.manage(poolParams.margin()).denominator()).to_str()),
numerator: Number(scope.manage(scope.manage(poolParams.margin()).numerator()).to_str())
},
metadataJson: jsonMetadata(scope.manage(poolParams.pool_metadata())),
owners: createCardanoOwners(scope.manage(poolParams.pool_owners()), rewardAccountAddress.network_id()),
pledge: BigInt(scope.manage(poolParams.pledge()).to_str()),
relays: createCardanoRelays(scope.manage(poolParams.relays())),
rewardAccount: RewardAccount(rewardAccountAddress.to_bech32()),
vrf: VrfVkHex(Buffer.from(scope.manage(poolParams.vrf_keyhash()).to_bytes()).toString('hex'))
}
} as PoolRegistrationCertificate;
});
const poolRetirement = (certificate: CML.PoolRetirement): PoolRetirementCertificate =>
usingAutoFree((scope) => ({
__typename: CertificateType.PoolRetirement,
epoch: certificate.epoch(),
poolId: PoolId(scope.manage(certificate.pool_keyhash()).to_bech32('pool'))
}));
const genesisKeyDelegation = (certificate: CML.GenesisKeyDelegation): GenesisKeyDelegationCertificate =>
usingAutoFree((scope) => ({
__typename: CertificateType.GenesisKeyDelegation,
genesisDelegateHash: Hash28ByteBase16(scope.manage(certificate.genesis_delegate_hash()).to_hex()),
genesisHash: Hash28ByteBase16(scope.manage(certificate.genesishash()).to_hex()),
vrfKeyHash: Hash32ByteBase16(scope.manage(certificate.vrf_keyhash()).to_hex())
}));
export const createCertificate = (cmlCertificate: CML.Certificate): Certificate =>
usingAutoFree((scope) => {
switch (cmlCertificate.kind()) {
case CML.CertificateKind.StakeRegistration:
return stakeRegistration(scope.manage(cmlCertificate.as_stake_registration()!));
case CML.CertificateKind.StakeDeregistration:
return stakeDeregistration(scope.manage(cmlCertificate.as_stake_deregistration()!));
case CML.CertificateKind.StakeDelegation:
return stakeDelegation(scope.manage(cmlCertificate.as_stake_delegation()!));
case CML.CertificateKind.PoolRegistration:
return poolRegistration(scope.manage(cmlCertificate.as_pool_registration()!));
case CML.CertificateKind.PoolRetirement:
return poolRetirement(scope.manage(cmlCertificate.as_pool_retirement()!));
case CML.CertificateKind.GenesisKeyDelegation:
return genesisKeyDelegation(scope.manage(cmlCertificate.as_genesis_key_delegation()!));
case CML.CertificateKind.MoveInstantaneousRewardsCert:
throw new NotImplementedError('MIR certificate conversion'); // TODO: support this certificate type
default:
throw new SerializationError(SerializationFailure.InvalidType);
}
});