Skip to content

Commit a8b98b2

Browse files
feat(core): added transaction body serialization classes
1 parent c078a91 commit a8b98b2

File tree

7 files changed

+231
-0
lines changed

7 files changed

+231
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import * as Cardano from '../../Cardano';
2+
import { InvalidStateError } from '@cardano-sdk/util';
3+
import { PlutusData } from '../PlutusData';
4+
5+
export enum DatumKind {
6+
dataHash,
7+
inlineData
8+
}
9+
10+
export class Datum {
11+
#datumKind: DatumKind;
12+
#dataHash: Cardano.DatumHash | undefined;
13+
#inlineData: PlutusData | undefined;
14+
15+
constructor(dataHash?: Cardano.DatumHash, inlineData?: PlutusData) {
16+
if (dataHash && inlineData) throw new InvalidStateError('Datum can only be DataHash or PlutusData but not both');
17+
if (!dataHash && !inlineData) throw new InvalidStateError('Datum must be either DataHash or PlutusData');
18+
19+
if (dataHash) this.#datumKind = DatumKind.dataHash;
20+
if (inlineData) this.#datumKind = DatumKind.inlineData;
21+
22+
this.#dataHash = dataHash;
23+
this.#inlineData = inlineData;
24+
}
25+
26+
kind(): DatumKind {
27+
return this.#datumKind;
28+
}
29+
30+
asDataHash(): Cardano.DatumHash | undefined {
31+
return this.#dataHash;
32+
}
33+
34+
asInlineData(): PlutusData | undefined {
35+
return this.#inlineData;
36+
}
37+
38+
static newDataHash(dataHash: Cardano.DatumHash): Datum {
39+
return new Datum(dataHash);
40+
}
41+
42+
static newInlineData(inlineData: PlutusData): Datum {
43+
return new Datum(undefined, inlineData);
44+
}
45+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as Cardano from '../../Cardano';
2+
import * as Crypto from '@cardano-sdk/crypto';
3+
import { Certificate } from '../Certificates';
4+
import { TransactionInput } from './TransactionInput';
5+
import { TransactionOutput } from './TransactionOutput';
6+
import { Update } from '../Update';
7+
8+
export class TransactionBody {
9+
constructor(inputs: Array<TransactionInput>, outputs: Array<TransactionOutput>, fee: bigint, ttl?: bigint);
10+
inputs(): Array<TransactionInput>;
11+
outputs(): Array<TransactionOutput>;
12+
fee(): bigint;
13+
ttl(): bigint | undefined;
14+
set_certs(certs: Array<Certificate>): void;
15+
certs(): Array<Certificate> | undefined;
16+
set_withdrawals(withdrawals: Map<Cardano.RewardAccount, Cardano.Lovelace>): void;
17+
withdrawals(): Map<Cardano.RewardAccount, Cardano.Lovelace> | undefined;
18+
set_update(update: Update): void;
19+
update(): Update | undefined;
20+
set_auxiliary_data_hash(auxiliary_data_hash: Crypto.Hash32ByteBase16): void;
21+
auxiliary_data_hash(): Crypto.Hash32ByteBase16 | undefined;
22+
set_validity_start_interval(validity_start_interval: bigint): void;
23+
validity_start_interval(): bigint | undefined;
24+
set_mint(mint: Map<Crypto.Hash32ByteBase16 /* Script Hash */, Cardano.TokenMap>): void;
25+
mint(): Map<Crypto.Hash32ByteBase16 /* Script Hash */, Cardano.TokenMap> | undefined;
26+
set_script_data_hash(script_data_hash: Crypto.Hash32ByteBase16): void;
27+
script_data_hash(): Crypto.Hash32ByteBase16 | undefined;
28+
set_collateral(collateral: Array<TransactionInput>): void;
29+
collateral(): Array<TransactionInput> | undefined;
30+
set_required_signers(required_signers: Array<Crypto.Ed25519KeyHashHex>): void;
31+
required_signers(): Array<Crypto.Ed25519KeyHashHex> | undefined;
32+
set_network_id(network_id: Cardano.NetworkId): void;
33+
network_id(): Cardano.NetworkId | undefined;
34+
set_collateral_return(collateral_return: TransactionOutput): void;
35+
collateral_return(): TransactionOutput | undefined;
36+
set_total_collateral(total_collateral: bigint): void;
37+
total_collateral(): bigint | undefined;
38+
set_reference_inputs(reference_inputs: Array<TransactionInput>): void;
39+
reference_inputs(): Array<TransactionInput> | undefined;
40+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as Cardano from '../../Cardano';
2+
3+
export class TransactionInput {
4+
#id: Cardano.TransactionId;
5+
#index: bigint;
6+
7+
constructor(id: Cardano.TransactionId, index: bigint) {
8+
this.#id = id;
9+
this.#index = index;
10+
}
11+
12+
transactionId(): Cardano.TransactionId {
13+
return this.#id;
14+
}
15+
16+
index(): bigint {
17+
return this.#index;
18+
}
19+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import * as Cardano from '../../Cardano';
2+
import { Datum } from './Datum';
3+
import { Script } from '../Scripts';
4+
import { Value } from './Value';
5+
6+
/**
7+
* A TransactionOutput object includes the address which represents a public key
8+
* hash or a script hash that can unlock the output, and the funds that are held
9+
* inside.
10+
*/
11+
export class TransactionOutput {
12+
#address: Cardano.Address;
13+
#amount: Value;
14+
#datum: Datum | undefined;
15+
#scriptRef: Script | undefined;
16+
17+
constructor(address: Cardano.Address, amount: Value) {
18+
this.#address = address;
19+
this.#amount = amount;
20+
}
21+
22+
address(): Cardano.Address {
23+
return this.#address;
24+
}
25+
26+
amount(): Value {
27+
return this.#amount;
28+
}
29+
30+
datum(): Datum | undefined {
31+
return this.#datum;
32+
}
33+
34+
setDatum(data: Datum): void {
35+
this.#datum = data;
36+
}
37+
38+
/**
39+
* The key idea is to use reference inputs and modified outputs which carry actual scripts ("reference scripts"), and
40+
* allow such reference scripts to satisfy the script witnessing requirement for a transaction.
41+
*
42+
* This means that the transaction which uses the script will not need to provide it at all, so long as it referenced
43+
* an output which contained the script.
44+
*/
45+
scriptRef(): Script | undefined {
46+
return this.#scriptRef;
47+
}
48+
49+
setScriptRef(script: Script): void {
50+
this.#scriptRef = script;
51+
}
52+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { Lovelace, TokenMap } from '../../Cardano/types/Value';
2+
3+
/**
4+
* A Value object encapsulates the quantity of assets of different types,
5+
* including ADA (Cardano's native cryptocurrency) expressed in lovelace,
6+
* where 1 ADA = 1,000,000 lovelace, and other native tokens. Each key in the
7+
* tokens object is a unique identifier for an asset, and the corresponding
8+
* value is the quantity of that asset.
9+
*/
10+
export class Value {
11+
#coin = 0n;
12+
#multiasset: TokenMap | undefined = undefined;
13+
14+
/**
15+
* Initializes a nre instance of the Value class.
16+
*
17+
* @param coin Amount of lovelace represented by this Value.
18+
* @param multiasset The collection of native assets represented by this Value.
19+
*/
20+
constructor(coin: Lovelace, multiasset?: TokenMap) {
21+
this.#coin = coin;
22+
this.#multiasset = multiasset;
23+
}
24+
25+
/**
26+
* Gets the coin amount included in this Value.
27+
*
28+
* @returns The coin amount of this value in lovelace,
29+
* where 1 ADA = 1,000,000 lovelace.
30+
*/
31+
coin(): Lovelace {
32+
return this.#coin;
33+
}
34+
35+
/**
36+
* Sets the coin amount to be included in this Value.
37+
*
38+
* @param coin The coin amount of this value in lovelace,
39+
* where 1 ADA = 1,000,000 lovelace.
40+
*/
41+
setCoin(coin: Lovelace): void {
42+
this.#coin = coin;
43+
}
44+
45+
/**
46+
* Gets the assets included in this Value.
47+
*
48+
* Each key in the tokens object is a unique identifier for an asset,
49+
* and the corresponding value is the quantity of that asset.
50+
*
51+
* The asset identifiers for native tokens are constructed
52+
* as the hash of the policy script under which they were minted,
53+
* concatenated with a name for the token.
54+
*
55+
* @returns A mapping of asset identifiers to their quantities.
56+
*/
57+
multiasset(): TokenMap | undefined {
58+
return this.#multiasset;
59+
}
60+
61+
/**
62+
* Sets the assets included in this Value.
63+
*
64+
* @param multiasset A mapping of asset identifiers to their quantities.
65+
*/
66+
setMultiasset(multiasset: TokenMap): void {
67+
this.#multiasset = multiasset;
68+
}
69+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export * from './Datum';
2+
export * from './TransactionBody';
3+
export * from './TransactionInput';
4+
export * from './TransactionOutput';
5+
export * from './Value';

packages/core/src/Serialization/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './PlutusData';
55
export * from './CBOR';
66
export * from './Certificates';
77
export * from './Update';
8+
export * from './TransactionBody';

0 commit comments

Comments
 (0)