Skip to content

Commit

Permalink
feat(wallet): createTransactionInternals
Browse files Browse the repository at this point in the history
- adds ValidityInterval type to core package
  • Loading branch information
rhyslbw committed Sep 27, 2021
1 parent 2cac0fb commit 1aa7032
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/core/src/Transaction/ValidityInterval.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Slot } from '@cardano-ogmios/schema';

export interface ValidityInterval {
invalidBefore?: Slot;
invalidHereafter?: Slot;
}
1 change: 1 addition & 0 deletions packages/core/src/Transaction/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './ValidityInterval';
export * from './WithHash';
38 changes: 38 additions & 0 deletions packages/wallet/src/createTransactionInternals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Importing types from cardano-serialization-lib-browser will cause TypeScript errors.
import CardanoSerializationLib from '@emurgo/cardano-serialization-lib-nodejs';
import { SelectionResult } from '@cardano-sdk/cip2';
import { Transaction } from '@cardano-sdk/core';

export type TxInternals = {
hash: CardanoSerializationLib.TransactionHash;
body: CardanoSerializationLib.TransactionBody;
};

export type CreateTxInternalsProps = {
changeAddress: string;
inputSelection: SelectionResult['selection'];
validityInterval: Transaction.ValidityInterval;
};

export const createTransactionInternals = async (
cardanoSerializationLib: typeof CardanoSerializationLib,
props: CreateTxInternalsProps
): Promise<TxInternals> => {
const inputs = cardanoSerializationLib.TransactionInputs.new();
for (const utxo of props.inputSelection.inputs) {
inputs.add(utxo.input());
}
const body = cardanoSerializationLib.TransactionBody.new(
inputs,
props.inputSelection.outputs,
cardanoSerializationLib.BigNum.from_str(props.inputSelection.fee.toString()),
props.validityInterval.invalidHereafter
);
if (props.validityInterval.invalidBefore !== undefined) {
body.set_validity_start_interval(props.validityInterval.invalidBefore);
}
return {
body,
hash: cardanoSerializationLib.hash_transaction(body)
};
};
1 change: 1 addition & 0 deletions packages/wallet/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * as Address from './Address';
export * from './createTransactionInternals';
export * from './InMemoryUtxoRepository';
export * as KeyManagement from './KeyManagement';
export * from './UtxoRepository';
66 changes: 66 additions & 0 deletions packages/wallet/test/createTransactionInternals.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// All types should be imported from cardano-serialization-lib-nodejs.
// Importing from cardano-serialization-lib-browser will cause TypeScript errors.
// Do not create objects from direct imports of serialization lib, use @cardano-sdk/cardano-serialization-lib.
import CardanoSerializationLib from '@emurgo/cardano-serialization-lib-nodejs';
import { loadCardanoSerializationLib } from '@cardano-sdk/cardano-serialization-lib';
import { createTransactionInternals } from '@src/createTransactionInternals';
import { InputSelector, roundRobinRandomImprove } from '@cardano-sdk/cip2';
import { Cardano, CardanoProvider, Ogmios } from '@cardano-sdk/core';
import { providerStub } from './ProviderStub';
import { UtxoRepository } from '@src/UtxoRepository';
import { InMemoryUtxoRepository } from '@src/InMemoryUtxoRepository';
import { createInMemoryKeyManager, util } from '@cardano-sdk/in-memory-key-manager';
import { NO_CONSTRAINTS } from './util';

const address =
'addr_test1qq585l3hyxgj3nas2v3xymd23vvartfhceme6gv98aaeg9muzcjqw982pcftgx53fu5527z2cj2tkx2h8ux2vxsg475q2g7k3g';

const outputs = CardanoSerializationLib.TransactionOutputs.new();

outputs.add(
Ogmios.OgmiosToCardanoWasm.txOut({
address,
// value: { coins: 4_000_000, assets: { '2a286ad895d091f2b3d168a6091ad2627d30a72761a5bc36eef00740': 20n } }
value: { coins: 4_000_000 }
})
);
outputs.add(
Ogmios.OgmiosToCardanoWasm.txOut({
address,
// value: { coins: 2_000_000, assets: { '2a286ad895d091f2b3d168a6091ad2627d30a72761a5bc36eef00740': 20n } }
value: { coins: 2_000_000 }
})
);

describe('createTransactionInternals', () => {
let CSL: typeof CardanoSerializationLib;
let provider: CardanoProvider;
let inputSelector: InputSelector;
let utxoRepository: UtxoRepository;

beforeEach(async () => {
CSL = await loadCardanoSerializationLib();
provider = providerStub();
inputSelector = roundRobinRandomImprove(CSL);
const keyManager = createInMemoryKeyManager({
mnemonic: util.generateMnemonic(),
networkId: Cardano.NetworkId.testnet,
password: '123'
});
utxoRepository = new InMemoryUtxoRepository(provider, keyManager, inputSelector);
});

test('simple transaction', async () => {
const result = await utxoRepository.selectInputs(outputs, NO_CONSTRAINTS);
const ledgerTip = await provider.ledgerTip();
const { body, hash } = await createTransactionInternals(CSL, {
changeAddress: 'addr_test1gz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzerspqgpsqe70et',
inputSelection: result.selection,
validityInterval: {
invalidHereafter: ledgerTip.slot + 3600
}
});
expect(body).toBeInstanceOf(CardanoSerializationLib.TransactionBody);
expect(hash).toBeInstanceOf(CardanoSerializationLib.TransactionHash);
});
});

0 comments on commit 1aa7032

Please sign in to comment.