-
Notifications
You must be signed in to change notification settings - Fork 55
/
cmlToCore.test.ts
179 lines (158 loc) · 5.85 KB
/
cmlToCore.test.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import * as Crypto from '@cardano-sdk/crypto';
import { AssetId } from '../../src/Cardano';
import { Base64Blob, HexBlob, ManagedFreeableScope } from '@cardano-sdk/util';
import { Cardano, cmlToCore, coreToCml } from '../../src';
import { NativeScript } from '@dcspark/cardano-multiplatform-lib-nodejs';
import {
babbageTx,
babbageTxBody,
babbageTxOutWithDatumHash,
babbageTxOutWithInlineDatum,
mintTokenMap,
tx,
txBody,
txIn,
txInWithAddress,
txOut,
txOutWithByron,
valueCoinOnly,
valueWithAssets
} from './testData';
import { createAssetId } from '../../src/CML/cmlToCore';
import { parseAssetId } from '../../src/CML/coreToCml';
describe('cmlToCore', () => {
let scope: ManagedFreeableScope;
beforeEach(() => {
scope = new ManagedFreeableScope();
});
afterEach(() => {
scope.dispose();
});
describe('AssetId', () => {
it('createAssetId', async () => {
const assetId = AssetId('659f2917fb63f12b33667463ee575eeac1845bbc736b9c0bbc40ba8254534c41');
const cmlAssetId = parseAssetId(assetId);
expect(createAssetId(cmlAssetId.scriptHash, cmlAssetId.assetName)).toEqual(assetId);
});
});
describe('value', () => {
it('coin only', () => {
expect(cmlToCore.value(coreToCml.value(scope, valueCoinOnly))).toEqual(valueCoinOnly);
});
it('cmlToCore coin with assets', () => {
expect(cmlToCore.value(coreToCml.value(scope, valueWithAssets))).toEqual(valueWithAssets);
});
});
describe('txIn', () => {
it('converts an input', () => {
expect(cmlToCore.txIn(coreToCml.txIn(scope, txIn))).toEqual(txIn);
});
it('doesnt serialize address', () => {
expect(cmlToCore.txIn(coreToCml.txIn(scope, txInWithAddress))).toEqual({
...txInWithAddress,
address: undefined
});
});
});
describe('txOut', () => {
it('can convert a CML.TransactionOutput which contains a Shelley address to Core.TxOut', () => {
expect(cmlToCore.txOut(coreToCml.txOut(scope, txOut))).toEqual(txOut);
});
it('can convert a CML.TransactionOutput which contains a Byron address to Core.TxOut', () => {
expect(cmlToCore.txOut(coreToCml.txOut(scope, txOutWithByron))).toEqual(txOutWithByron);
});
it('can convert a CML.TransactionOutput with babbage fields to Core.TxOut', () => {
expect(cmlToCore.txOut(coreToCml.txOut(scope, babbageTxOutWithInlineDatum))).toEqual(babbageTxOutWithInlineDatum);
expect(cmlToCore.txOut(coreToCml.txOut(scope, babbageTxOutWithDatumHash))).toEqual(babbageTxOutWithDatumHash);
});
});
it('utxo', () => {
const utxo: Cardano.Utxo[] = [[txIn as Cardano.HydratedTxIn, txOut]];
expect(cmlToCore.utxo(coreToCml.utxo(scope, utxo))).toEqual(utxo);
});
it('txMint', () => {
expect(cmlToCore.txMint(coreToCml.txMint(scope, mintTokenMap))).toEqual(mintTokenMap);
});
it('NativeScript', () => {
const cmlScript: NativeScript = scope.manage(
NativeScript.from_bytes(
Uint8Array.from(
Buffer.from(
// eslint-disable-next-line max-len
'8202828200581cb275b08c999097247f7c17e77007c7010cd19f20cc086ad99d3985388201838205190bb88200581c966e394a544f242081e41d1965137b1bb412ac230d40ed5407821c378204190fa0',
'hex'
)
)
)
);
const coreScript: Cardano.NativeScript = {
__type: Cardano.ScriptType.Native,
kind: Cardano.NativeScriptKind.RequireAnyOf,
scripts: [
{
__type: Cardano.ScriptType.Native,
keyHash: Crypto.Ed25519KeyHashHex('b275b08c999097247f7c17e77007c7010cd19f20cc086ad99d398538'),
kind: Cardano.NativeScriptKind.RequireSignature
},
{
__type: Cardano.ScriptType.Native,
kind: Cardano.NativeScriptKind.RequireAllOf,
scripts: [
{
__type: Cardano.ScriptType.Native,
kind: Cardano.NativeScriptKind.RequireTimeBefore,
slot: Cardano.Slot(3000)
},
{
__type: Cardano.ScriptType.Native,
keyHash: Crypto.Ed25519KeyHashHex('966e394a544f242081e41d1965137b1bb412ac230d40ed5407821c37'),
kind: Cardano.NativeScriptKind.RequireSignature
},
{
__type: Cardano.ScriptType.Native,
kind: Cardano.NativeScriptKind.RequireTimeAfter,
slot: Cardano.Slot(4000)
}
]
}
]
};
expect(cmlToCore.nativeScript(cmlScript)).toStrictEqual(coreScript);
});
describe('txAuxiliaryData', () => {
// mostly covered by newTx - could just remove this
it.todo('txMetadata');
it.todo('txScripts');
});
it('txBody', () => {
expect(cmlToCore.txBody(scope.manage(coreToCml.txBody(scope, txBody)))).toEqual(txBody);
});
it('Babbage txBody', () => {
expect(cmlToCore.txBody(scope.manage(coreToCml.txBody(scope, babbageTxBody)))).toEqual(babbageTxBody);
});
it('newTx', () => {
expect(cmlToCore.newTx(scope.manage(coreToCml.tx(scope, tx)))).toEqual(tx);
});
it('Babbage newTx', () => {
expect(cmlToCore.newTx(scope.manage(coreToCml.tx(scope, babbageTx)))).toEqual(babbageTx);
});
it('txWitnessBootstrap', () => {
// values from ogmios.wsp.json
const bootstrap: Cardano.BootstrapWitness[] = [
{
addressAttributes: Base64Blob('oA=='),
chainCode: HexBlob('b6dbf0b03c93afe5696f10d49e8a8304ebfac01deeb8f82f2af5836ebbc1b450'),
key: Crypto.Ed25519PublicKeyHex('deeb8f82f2af5836ebbc1b450b6dbf0b03c93afe5696f10d49e8a8304ebfac01'),
signature: Crypto.Ed25519SignatureHex(
Buffer.from(
'ZGdic3hnZ3RvZ2hkanB0ZXR2dGtjb2N2eWZpZHFxZ2d1cmpocmhxYWlpc3BxcnVlbGh2eXBxeGVld3ByeWZ2dw==',
'base64'
).toString('hex')
)
}
];
expect(cmlToCore.txWitnessBootstrap(scope.manage(coreToCml.txWitnessBootstrap(scope, bootstrap)))).toEqual(
bootstrap
);
});
});