Skip to content

Commit f0e3e4d

Browse files
committed
fix: storeCredentials to support passing credentials along
1 parent 763589a commit f0e3e4d

File tree

1 file changed

+60
-61
lines changed

1 file changed

+60
-61
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,77 @@
1-
import { Cardano, ChainSyncEventType, TxBodyCBOR } from '@cardano-sdk/core';
1+
import { Cardano, ChainSyncEventType } from '@cardano-sdk/core';
22
import { Hash28ByteBase16 } from '@cardano-sdk/crypto';
33
import { Mappers } from '@cardano-sdk/projection';
44
import { credentialsFromAddress } from '@cardano-sdk/projection/src/operators/Mappers/util';
55
import { CredentialEntity, CredentialType, OutputEntity } from '../entity';
66
import { typeormOperator } from './util';
77

8-
export const storeCredentials = typeormOperator<Mappers.WithUtxo & Mappers.WithAddresses>(async (evt) => {
9-
const {
10-
block: { body: txs },
11-
eventType,
12-
queryRunner
13-
} = evt;
8+
export interface WithTxCredentials {
9+
txToCredentials: Map<Cardano.TransactionId, Map<Hash28ByteBase16, CredentialType>>;
10+
}
1411

15-
// produced credentials will be automatically deleted via block cascade
16-
if (txs.length === 0 || eventType !== ChainSyncEventType.RollForward) return;
12+
export const storeCredentials = typeormOperator<Mappers.WithUtxo & Mappers.WithAddresses, WithTxCredentials>(
13+
async (evt) => {
14+
const {
15+
block: { body: txs },
16+
eventType,
17+
queryRunner
18+
} = evt;
1719

18-
const utxoRepository = queryRunner.manager.getRepository(OutputEntity);
19-
const txIdToCredentials = new Map<Cardano.TransactionId, Map<Hash28ByteBase16, CredentialType>>();
20+
const txToCredentials = new Map<Cardano.TransactionId, Map<Hash28ByteBase16, CredentialType>>();
2021

21-
const addCredential = (
22-
{ paymentCredentialHash, stakeCredential }: Mappers.Address,
23-
map: Map<Hash28ByteBase16, CredentialType>
24-
) => {
25-
if (paymentCredentialHash) {
26-
map.set(paymentCredentialHash, CredentialType.PAYMENT);
27-
}
28-
if (stakeCredential && !('slot' in stakeCredential)) {
29-
// FIXME: Support pointer stake credentials
30-
map.set(stakeCredential, CredentialType.STAKE);
31-
}
32-
};
22+
// produced credentials will be automatically deleted via block cascade
23+
if (txs.length > 0 && eventType === ChainSyncEventType.RollForward) {
24+
const utxoRepository = queryRunner.manager.getRepository(OutputEntity);
3325

34-
// get input & output credentials by tx
35-
for (const tx of evt.block.body) {
36-
const txCredentials = new Map<Hash28ByteBase16, CredentialType>();
26+
// get input & output credentials by tx
27+
for (const tx of evt.block.body) {
28+
const txCredentials = new Map<Hash28ByteBase16, CredentialType>();
29+
const addCredential = (source: OutputEntity | null | Cardano.TxOut) => {
30+
if (source && source.address) {
31+
const { paymentCredentialHash, stakeCredential } = credentialsFromAddress(source.address);
32+
if (paymentCredentialHash) {
33+
txCredentials.set(paymentCredentialHash, CredentialType.PAYMENT);
34+
}
35+
if (stakeCredential && !('slot' in stakeCredential)) {
36+
// FIXME: Support pointer stake credentials
37+
txCredentials.set(stakeCredential, CredentialType.STAKE);
38+
}
39+
}
40+
};
3741

38-
// get tx input address
39-
const txInOutputs = await Promise.all(
40-
tx.body.inputs.map(
41-
async ({ txId, index: outputIndex }) =>
42-
await utxoRepository.findOne({ select: { address: true }, where: { outputIndex, txId } })
43-
)
44-
);
42+
// get tx input address
43+
const txInOutputEntities = await Promise.all(
44+
tx.body.inputs.map(
45+
async ({ txId, index: outputIndex }) =>
46+
await utxoRepository.findOne({ select: { address: true }, where: { outputIndex, txId } })
47+
)
48+
);
4549

46-
// add tx input credentials to involved tx credentials
47-
for (const txOut of txInOutputs) {
48-
if (txOut && txOut.address) {
49-
addCredential(credentialsFromAddress(txOut.address), txCredentials);
50-
}
51-
}
50+
// add tx input credentials to involved tx credentials
51+
txInOutputEntities.forEach(addCredential);
52+
// add tx output credentials to involved tx credentials
53+
tx.body.outputs.forEach(addCredential);
5254

53-
// add tx output credentials to involved tx credentials
54-
for (const txOut of tx.body.outputs) {
55-
addCredential(credentialsFromAddress(txOut.address), txCredentials);
56-
}
55+
const credentialEntities = Array.from(txCredentials).map(
56+
([credentialHash, credentialType]): CredentialEntity => ({
57+
credentialHash: Buffer.from(credentialHash, 'hex'),
58+
credentialType
59+
})
60+
);
5761

58-
const credentialEntities = Array.from(txCredentials).map(
59-
([credentialHash, credentialType]): CredentialEntity => ({
60-
credentialHash: Buffer.from(credentialHash, 'hex'),
61-
credentialType
62-
})
63-
);
62+
await queryRunner.manager
63+
.createQueryBuilder()
64+
.insert()
65+
.into(CredentialEntity)
66+
.values(credentialEntities)
67+
.orIgnore()
68+
.execute();
6469

65-
await queryRunner.manager
66-
.createQueryBuilder()
67-
.insert()
68-
.into(CredentialEntity)
69-
.values(credentialEntities)
70-
.orIgnore()
71-
.execute();
70+
// store tx credentials for transaction entities
71+
txToCredentials.set(tx.id, txCredentials);
72+
}
73+
}
7274

73-
// store tx credentials for transaction entities
74-
txIdToCredentials.set(tx.id, txCredentials);
75+
return { txToCredentials: txToCredentials };
7576
}
76-
77-
return { txIdToCredentials };
78-
});
77+
);

0 commit comments

Comments
 (0)