Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix more serialization errors #30

Merged
merged 22 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
db86e8c
add GovernanceAction variants to class-info to avoid false negatives
rmgaray Jan 2, 2025
fa2a3e0
remove TX with unsupported ProtocolParamUpdate
rmgaray Jan 3, 2025
8ddbf41
add ttl to extraction_unsupported_fields
rmgaray Jan 3, 2025
acd015b
fix: serialization of sets not preserving [in]definite encoding of ar…
rmgaray Jan 3, 2025
1553fe8
fix: fragment_encode_len option in YAML schema not being used
rmgaray Jan 3, 2025
a27cbe1
add 7 TXs with errors in witness set to regression suite
rmgaray Jan 3, 2025
a829123
generate coverage report with serialization tests
rmgaray Jan 7, 2025
190a0af
fix: PlutusMap containing PlutusLists instead of PlutusData
rmgaray Jan 7, 2025
acf70c6
fix: order of fields in TransactionWitnessSet to match wire format
rmgaray Jan 7, 2025
3dee7d4
fix: AuxiliaryDataShelleyMa not consuming the array header
rmgaray Jan 7, 2025
cb05deb
add all Native script variants serialization_bad_variants
rmgaray Jan 7, 2025
a8c3f7d
add README for tests
rmgaray Jan 9, 2025
9bd0912
fix: links and diagram
rmgaray Jan 9, 2025
2a5bac9
fix: PlutusMap not returning PlutusMapValues
rmgaray Jan 10, 2025
e14bc76
remove valid PlutusScript methods fromo api_ignore_methods
rmgaray Jan 10, 2025
28af481
Revert "fix: change `NativeScripts` type to `array`"
rmgaray Jan 14, 2025
4c93d39
fix: PlutusScript hash function bug
marcusbfs Jan 21, 2025
ca7be56
add: `hash` method for `NativeScript`
marcusbfs Jan 21, 2025
0a91806
fix: prefix for Testnet addresses
marcusbfs Jan 22, 2025
157ead4
fix: add error check for bech32 prefix
marcusbfs Jan 23, 2025
b715017
add: Transaction.transaction_hash
marcusbfs Jan 23, 2025
91d2148
Add key method for `Withdrawals` map
marcusbfs Jan 29, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions conway-cddl/codegen/generators/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ export class GenHash extends CodeGeneratorBase {
"to_bech32",
(to_bech32) => `
${to_bech32}(prefix: string): string {
if (!prefix) {
throw new Error("bech32 HRP (prefix) cannot be empty.");
}
if (prefix !== prefix.toLowerCase()) {
throw new Error("bech32 HRP (prefix) must be all lowercase.");
}
if (prefix.length > 83) {
throw new Error("bech32 HRP (prefix) length must not exceed 83 characters.");
}
let bytes = this.to_bytes();
let words = bech32.toWords(bytes);
return bech32.encode(prefix, words, Number.MAX_SAFE_INTEGER);
Expand Down
2 changes: 1 addition & 1 deletion conway-cddl/codegen/generators/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class GenSet extends CodeGeneratorBase {
if (this.nonEmptyTag) {
${writer}.writeTaggedTag(258);
}
${writer}.writeArray(this.items, (writer, x) => ${this.typeUtils.writeType("writer", "x", this.item)});
${writer}.writeArray(this.items, (writer, x) => ${this.typeUtils.writeType("writer", "x", this.item)}, this.definiteEncoding);
`;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class GenRecordFragment extends GenStructuredBase<Field> {
options: GenRecordFragmentOptions,
) {
super(name, customTypes, { genCSL: true, ...options });
this.fragmentEncodeLen = this.fragmentEncodeLen;
this.fragmentEncodeLen = options.fragment_encode_len;
}

generateDeserialize(reader: string, path: string): string {
Expand Down
1 change: 1 addition & 0 deletions conway-cddl/codegen/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ async function main() {
import * as cdlCrypto from "./lib/bip32-ed25519";
import {Address, Credential, CredKind, RewardAddress} from "./address";
import {webcrypto} from "crypto";
import { blake2b } from "@noble/hashes/blake2b";

// Polyfill the global "crypto" object if it doesn't exist
if (typeof globalThis.crypto === 'undefined') {
Expand Down
52 changes: 44 additions & 8 deletions conway-cddl/yaml/conway.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ Transaction:
);
}

transaction_hash(): TransactionHash {
return TransactionHash.new(blake2b(this.to_bytes(), { dkLen: 32 }));
}

# transaction_index = uint .size 2

# header =
Expand Down Expand Up @@ -1713,14 +1717,14 @@ TransactionWitnessSet:
name: inner_plutus_data
type: PlutusSet
optional: true
- id: 5
name: redeemers
type: Redeemers
optional: true
- id: 6
name: plutus_scripts_v2
type: PlutusScripts
optional: true
- id: 5
name: redeemers
type: Redeemers
optional: true
- id: 7
name: plutus_scripts_v3
type: PlutusScripts
Expand Down Expand Up @@ -1754,7 +1758,7 @@ Vkeywitnesses:
item: Vkeywitness

NativeScripts:
type: array
type: set
item: NativeScript

BootstrapWitnesses:
Expand All @@ -1773,7 +1777,7 @@ PlutusScript:
hash(language_version: number): ScriptHash {
let bytes = new Uint8Array(this.bytes().length + 1);
bytes[0] = language_version;
bytes.set(bytes, 1);
bytes.set(this.bytes(), 1);
let hash_bytes = cdlCrypto.blake2b224(bytes);
return new ScriptHash(hash_bytes);
}
Expand Down Expand Up @@ -2205,6 +2209,15 @@ NativeScript:
- name: timelock_expiry
tag: 5
value: TimelockExpiry
extra_methods: |
hash(): ScriptHash {
const thisBytes = this.to_bytes();
let bytes = new Uint8Array(thisBytes.length + 1);
bytes[0] = 0;
bytes.set(thisBytes, 1);
let hash_bytes = cdlCrypto.blake2b224(bytes);
return new ScriptHash(hash_bytes);
}

ScriptPubkey:
type: newtype
Expand Down Expand Up @@ -2368,7 +2381,7 @@ ScriptRef:
let bytes = reader.readBytes(path);
let new_reader = new CBORReader(bytes);

return ScriptRef.deserializeInner(new_reader, path)
return ScriptRef.deserializeInner(new_reader, path)
}

serialize(writer: CBORWriter): void {
Expand Down Expand Up @@ -2436,8 +2449,31 @@ PlutusList:
PlutusMap:
type: map
key: PlutusData
value: PlutusMapValues
value: PlutusData
keys_method_type: PlutusList
methods:
get: getInner
insert: insertInner
extra_methods: |
get(key: PlutusData): PlutusMapValues | undefined {
let v: PlutusData | undefined = this.getInner(key);
if(v) {
let vs = new PlutusMapValues([v]);
return vs;
} else {
return undefined;
}
}

insert(key: PlutusData, values: PlutusMapValues): PlutusMapValues | undefined {
let v: PlutusData = values.get(values.len() - 1);
let ret: PlutusData | undefined = this.insertInner(key, v);
if (ret) {
return new PlutusMapValues([ret]);
} else {
return undefined;
}
}

PlutusMapValues:
type: array
Expand Down
2 changes: 1 addition & 1 deletion conway-cddl/yaml/custom/auxiliary_data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# , auxiliary_scripts: [ * native_script ]
# ]
AuxiliaryDataShelleyMa:
type: record_fragment
type: record
fields:
- name: transaction_metadata
type: GeneralTransactionMetadata
Expand Down
46 changes: 25 additions & 21 deletions csl-types/cardano-data-lite.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,12 +619,12 @@ export declare class CommitteeHotAuth {
}
export declare class Constitution {
private _anchor;
private _scripthash;
constructor(anchor: Anchor, scripthash: ScriptHash | undefined);
private _script_hash;
constructor(anchor: Anchor, script_hash: ScriptHash | undefined);
anchor(): Anchor;
set_anchor(anchor: Anchor): void;
scripthash(): ScriptHash | undefined;
set_scripthash(scripthash: ScriptHash | undefined): void;
script_hash(): ScriptHash | undefined;
set_script_hash(script_hash: ScriptHash | undefined): void;
static deserialize(reader: CBORReader, path: string[]): Constitution;
serialize(writer: CBORWriter): void;
free(): void;
Expand Down Expand Up @@ -1951,12 +1951,12 @@ export declare class PlutusList {
as_set(): PlutusSet;
}
export declare class PlutusMap {
_items: [PlutusData, PlutusMapValues][];
constructor(items: [PlutusData, PlutusMapValues][]);
_items: [PlutusData, PlutusData][];
constructor(items: [PlutusData, PlutusData][]);
static new(): PlutusMap;
len(): number;
insert(key: PlutusData, value: PlutusMapValues): PlutusMapValues | undefined;
get(key: PlutusData): PlutusMapValues | undefined;
insertInner(key: PlutusData, value: PlutusData): PlutusData | undefined;
getInner(key: PlutusData): PlutusData | undefined;
_remove_many(keys: PlutusData[]): void;
keys(): PlutusList;
static deserialize(reader: CBORReader, path: string[]): PlutusMap;
Expand All @@ -1967,6 +1967,8 @@ export declare class PlutusMap {
to_bytes(): Uint8Array;
to_hex(): string;
clone(path: string[]): PlutusMap;
get(key: PlutusData): PlutusMapValues | undefined;
insert(key: PlutusData, values: PlutusMapValues): PlutusMapValues | undefined;
}
export declare class PlutusMapValues {
private items;
Expand Down Expand Up @@ -2278,7 +2280,7 @@ export declare class ProtocolParamUpdate {
private _treasury_growth_rate;
private _min_pool_cost;
private _ada_per_utxo_byte;
private _costmdls;
private _cost_models;
private _execution_costs;
private _max_tx_ex_units;
private _max_block_ex_units;
Expand All @@ -2293,8 +2295,8 @@ export declare class ProtocolParamUpdate {
private _governance_action_deposit;
private _drep_deposit;
private _drep_inactivity_period;
private _script_cost_per_byte;
constructor(minfee_a: BigNum | undefined, minfee_b: BigNum | undefined, max_block_body_size: number | undefined, max_tx_size: number | undefined, max_block_header_size: number | undefined, key_deposit: BigNum | undefined, pool_deposit: BigNum | undefined, max_epoch: number | undefined, n_opt: number | undefined, pool_pledge_influence: UnitInterval | undefined, expansion_rate: UnitInterval | undefined, treasury_growth_rate: UnitInterval | undefined, min_pool_cost: BigNum | undefined, ada_per_utxo_byte: BigNum | undefined, costmdls: Costmdls | undefined, execution_costs: ExUnitPrices | undefined, max_tx_ex_units: ExUnits | undefined, max_block_ex_units: ExUnits | undefined, max_value_size: number | undefined, collateral_percentage: number | undefined, max_collateral_inputs: number | undefined, pool_voting_thresholds: PoolVotingThresholds | undefined, drep_voting_thresholds: DRepVotingThresholds | undefined, min_committee_size: number | undefined, committee_term_limit: number | undefined, governance_action_validity_period: number | undefined, governance_action_deposit: BigNum | undefined, drep_deposit: BigNum | undefined, drep_inactivity_period: number | undefined, script_cost_per_byte: UnitInterval | undefined);
private _ref_script_coins_per_byte;
constructor(minfee_a: BigNum | undefined, minfee_b: BigNum | undefined, max_block_body_size: number | undefined, max_tx_size: number | undefined, max_block_header_size: number | undefined, key_deposit: BigNum | undefined, pool_deposit: BigNum | undefined, max_epoch: number | undefined, n_opt: number | undefined, pool_pledge_influence: UnitInterval | undefined, expansion_rate: UnitInterval | undefined, treasury_growth_rate: UnitInterval | undefined, min_pool_cost: BigNum | undefined, ada_per_utxo_byte: BigNum | undefined, cost_models: Costmdls | undefined, execution_costs: ExUnitPrices | undefined, max_tx_ex_units: ExUnits | undefined, max_block_ex_units: ExUnits | undefined, max_value_size: number | undefined, collateral_percentage: number | undefined, max_collateral_inputs: number | undefined, pool_voting_thresholds: PoolVotingThresholds | undefined, drep_voting_thresholds: DRepVotingThresholds | undefined, min_committee_size: number | undefined, committee_term_limit: number | undefined, governance_action_validity_period: number | undefined, governance_action_deposit: BigNum | undefined, drep_deposit: BigNum | undefined, drep_inactivity_period: number | undefined, ref_script_coins_per_byte: UnitInterval | undefined);
minfee_a(): BigNum | undefined;
set_minfee_a(minfee_a: BigNum | undefined): void;
minfee_b(): BigNum | undefined;
Expand Down Expand Up @@ -2323,8 +2325,8 @@ export declare class ProtocolParamUpdate {
set_min_pool_cost(min_pool_cost: BigNum | undefined): void;
ada_per_utxo_byte(): BigNum | undefined;
set_ada_per_utxo_byte(ada_per_utxo_byte: BigNum | undefined): void;
costmdls(): Costmdls | undefined;
set_costmdls(costmdls: Costmdls | undefined): void;
cost_models(): Costmdls | undefined;
set_cost_models(cost_models: Costmdls | undefined): void;
execution_costs(): ExUnitPrices | undefined;
set_execution_costs(execution_costs: ExUnitPrices | undefined): void;
max_tx_ex_units(): ExUnits | undefined;
Expand Down Expand Up @@ -2353,8 +2355,8 @@ export declare class ProtocolParamUpdate {
set_drep_deposit(drep_deposit: BigNum | undefined): void;
drep_inactivity_period(): number | undefined;
set_drep_inactivity_period(drep_inactivity_period: number | undefined): void;
script_cost_per_byte(): UnitInterval | undefined;
set_script_cost_per_byte(script_cost_per_byte: UnitInterval | undefined): void;
ref_script_coins_per_byte(): UnitInterval | undefined;
set_ref_script_coins_per_byte(ref_script_coins_per_byte: UnitInterval | undefined): void;
static deserialize(reader: CBORReader, path: string[]): ProtocolParamUpdate;
serialize(writer: CBORWriter): void;
free(): void;
Expand Down Expand Up @@ -2828,14 +2830,16 @@ export declare class ScriptRef {
as_plutus_script_v2(): PlutusScript | undefined;
as_plutus_script_v3(): PlutusScript | undefined;
kind(): ScriptRefKind;
static deserialize(reader: CBORReader, path: string[]): ScriptRef;
serialize(writer: CBORWriter): void;
static deserializeInner(reader: CBORReader, path: string[]): ScriptRef;
serializeInner(writer: CBORWriter): void;
free(): void;
static from_bytes(data: Uint8Array, path?: string[]): ScriptRef;
static from_hex(hex_str: string, path?: string[]): ScriptRef;
to_bytes(): Uint8Array;
to_hex(): string;
clone(path: string[]): ScriptRef;
static deserialize(reader: CBORReader, path: string[]): ScriptRef;
serialize(writer: CBORWriter): void;
}
export declare class SingleHostAddr {
private _port;
Expand Down Expand Up @@ -3345,10 +3349,10 @@ export declare class TransactionWitnessSet {
private _bootstraps;
private _plutus_scripts_v1;
private _inner_plutus_data;
private _redeemers;
private _plutus_scripts_v2;
private _redeemers;
private _plutus_scripts_v3;
constructor(vkeys: Vkeywitnesses | undefined, native_scripts: NativeScripts | undefined, bootstraps: BootstrapWitnesses | undefined, plutus_scripts_v1: PlutusScripts | undefined, inner_plutus_data: PlutusSet | undefined, redeemers: Redeemers | undefined, plutus_scripts_v2: PlutusScripts | undefined, plutus_scripts_v3: PlutusScripts | undefined);
constructor(vkeys: Vkeywitnesses | undefined, native_scripts: NativeScripts | undefined, bootstraps: BootstrapWitnesses | undefined, plutus_scripts_v1: PlutusScripts | undefined, inner_plutus_data: PlutusSet | undefined, plutus_scripts_v2: PlutusScripts | undefined, redeemers: Redeemers | undefined, plutus_scripts_v3: PlutusScripts | undefined);
vkeys(): Vkeywitnesses | undefined;
set_vkeys(vkeys: Vkeywitnesses | undefined): void;
native_scripts(): NativeScripts | undefined;
Expand All @@ -3359,10 +3363,10 @@ export declare class TransactionWitnessSet {
set_plutus_scripts_v1(plutus_scripts_v1: PlutusScripts | undefined): void;
inner_plutus_data(): PlutusSet | undefined;
set_inner_plutus_data(inner_plutus_data: PlutusSet | undefined): void;
redeemers(): Redeemers | undefined;
set_redeemers(redeemers: Redeemers | undefined): void;
plutus_scripts_v2(): PlutusScripts | undefined;
set_plutus_scripts_v2(plutus_scripts_v2: PlutusScripts | undefined): void;
redeemers(): Redeemers | undefined;
set_redeemers(redeemers: Redeemers | undefined): void;
plutus_scripts_v3(): PlutusScripts | undefined;
set_plutus_scripts_v3(plutus_scripts_v3: PlutusScripts | undefined): void;
static deserialize(reader: CBORReader, path: string[]): TransactionWitnessSet;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
],
"scripts": {
"test": "npm run test-serialization ; npm run test-api",
"test-serialization": "NODE_OPTIONS=--experimental-vm-modules jest tests/serialization/serialization.test.ts",
"test-serialization": "NODE_OPTIONS=--experimental-vm-modules jest tests/serialization/serialization.test.ts --coverage --collectCoverageFrom=\"src/**/*.ts\"",
"test-serialization-dev": "NODE_OPTIONS=--experimental-vm-modules jest tests/serialization/serialization_dev.test.ts",
"test-api": "npm run generate-cdl-definitions ; NODE_OPTIONS=--experimental-vm-modules jest tests/api/api.test.ts",
"test-implementation": "NODE_OPTIONS=--experimental-vm-modules jest fees hash min_output_ada",
Expand Down
4 changes: 3 additions & 1 deletion src/address/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { MalformedAddress } from "./malformed";
import { EnterpriseAddress } from "./enterprise";
import { BaseAddress } from "./base";
import { Credential } from "./credential";
import { NetworkInfo } from "./network_info";
import { RewardAddress } from "./reward";
import { CBORWriter } from "../lib/cbor/writer";
import { CBORReader } from "../lib/cbor/reader";
Expand Down Expand Up @@ -163,7 +164,8 @@ export class Address {
let prefix_tail = ""
if (this._variant.kind == AddressKind.Malformed) {
prefix_tail = "_malformed";
} else if (this.network_id() != 0) {
} else if (this.network_id() == NetworkInfo.testnet_preprod().network_id()
|| this.network_id() == NetworkInfo.testnet_preview().network_id()) {
prefix_tail = "_test";
}
prefix = prefix_header + prefix_tail;
Expand Down
Loading
Loading