-
Notifications
You must be signed in to change notification settings - Fork 55
/
createTransactionInternals.ts
45 lines (42 loc) · 1.25 KB
/
createTransactionInternals.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
import { CSL, Cardano, coreToCsl } from '@cardano-sdk/core';
import { SelectionResult } from '@cardano-sdk/cip2';
export type TxInternals = {
hash: Cardano.TransactionId;
body: Cardano.TxBodyAlonzo;
};
export type CreateTxInternalsProps = {
changeAddress: Cardano.Address;
inputSelection: SelectionResult['selection'];
validityInterval: Cardano.ValidityInterval;
certificates?: Cardano.Certificate[];
withdrawals?: Cardano.Withdrawal[];
};
export const createTransactionInternals = async ({
changeAddress,
withdrawals,
certificates,
validityInterval,
inputSelection
}: CreateTxInternalsProps): Promise<TxInternals> => {
const outputs = [...inputSelection.outputs];
for (const value of inputSelection.change) {
outputs.push({
address: changeAddress,
value
});
}
const body = {
// TODO: return more fields. Also add support in coreToCsl.txBody:
// collaterals, mint, requiredExtraSignatures, scriptIntegrityHash
certificates,
fee: inputSelection.fee,
inputs: [...inputSelection.inputs].map(([txIn]) => txIn),
outputs,
validityInterval,
withdrawals
};
return {
body,
hash: Cardano.TransactionId(Buffer.from(CSL.hash_transaction(coreToCsl.txBody(body)).to_bytes()).toString('hex'))
};
};