Skip to content

Commit

Permalink
support 4.0.1 more
Browse files Browse the repository at this point in the history
  • Loading branch information
micahkendall committed Dec 27, 2023
1 parent 8fe54e0 commit f155176
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 260 deletions.
6 changes: 3 additions & 3 deletions src/misc/sign_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function signData(

const priv = C.PrivateKey.from_bech32(privateKey);

const signedSigStruc = priv.sign(toSign).to_bytes();
const signedSigStruc = priv.sign(toSign).to_raw_bytes();
const coseSign1 = builder.build(signedSigStruc);

const key = M.COSEKey.new(
Expand All @@ -45,7 +45,7 @@ export function signData(
); // crv (-1) set to Ed25519 (6)
key.set_header(
M.Label.new_int(M.Int.new_negative(M.BigNum.from_str("2"))),
M.CBORValue.new_bytes(priv.to_public().as_bytes()),
M.CBORValue.new_bytes(priv.to_public().to_raw_bytes()),
); // x (-2) set to public key

return {
Expand Down Expand Up @@ -137,7 +137,7 @@ export function verifyData(
}
})();

const signature = C.Ed25519Signature.from_bytes(cose1.signature());
const signature = C.Ed25519Signature.from_raw_bytes(cose1.signature());

const data = cose1.signed_data(undefined, undefined).to_bytes();

Expand Down
16 changes: 8 additions & 8 deletions src/plutus/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,18 +271,18 @@ function to<T = Data>(data: Exact<T>, type?: T): Datum | Redeemer {
return C.PlutusData.new_bytes(fromHex(data));
} else if (data instanceof Constr) {
const { index, fields } = data;
const plutusList = C.PlutusList.new();
const plutusList = C.PlutusDataList.new();

fields.forEach((field) => plutusList.add(serialize(field)));

return C.PlutusData.new_constr_plutus_data(
C.ConstrPlutusData.new(
C.BigNum.from_str(index.toString()),
BigInt(index),
plutusList,
),
);
} else if (data instanceof Array) {
const plutusList = C.PlutusList.new();
const plutusList = C.PlutusDataList.new();

data.forEach((arg) => plutusList.add(serialize(arg)));

Expand All @@ -291,7 +291,7 @@ function to<T = Data>(data: Exact<T>, type?: T): Datum | Redeemer {
const plutusMap = C.PlutusMap.new();

for (const [key, value] of data.entries()) {
plutusMap.insert(serialize(key), serialize(value));
plutusMap.set(serialize(key), serialize(value));
}

return C.PlutusData.new_map(plutusMap);
Expand All @@ -302,7 +302,7 @@ function to<T = Data>(data: Exact<T>, type?: T): Datum | Redeemer {
}
}
const d = type ? castTo<T>(data, type) : (data as Data);
return toHex(serialize(d).to_bytes()) as Datum | Redeemer;
return toHex(serialize(d).to_cbor_bytes()) as Datum | Redeemer;
}

/**
Expand All @@ -313,12 +313,12 @@ function from<T = Data>(raw: Datum | Redeemer, type?: T): T {
function deserialize(data: C.PlutusData): Data {
if (data.kind() === 0) {
const constr = data.as_constr_plutus_data()!;
const l = constr.data();
const l = constr.fields();
const desL = [];
for (let i = 0; i < l.len(); i++) {
desL.push(deserialize(l.get(i)));
}
return new Constr(parseInt(constr.alternative().to_str()), desL);
return new Constr(parseInt(constr.alternative().toString()), desL);
} else if (data.kind() === 1) {
const m = data.as_map()!;
const desM: Map<Data, Data> = new Map();
Expand All @@ -341,7 +341,7 @@ function from<T = Data>(raw: Datum | Redeemer, type?: T): T {
}
throw new Error("Unsupported type");
}
const data = deserialize(C.PlutusData.from_bytes(fromHex(raw)));
const data = deserialize(C.PlutusData.from_cbor_hex(raw));

return type ? castFrom<T>(data, type) : (data as T);
}
Expand Down
2 changes: 0 additions & 2 deletions src/provider/kupmios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import {
costModelKeys,
fromHex,
fromUnit,
toHex,
Utils,
} from "../utils/mod.ts";
import * as ogmios from "@cardano-ogmios/schema";

Expand Down
42 changes: 17 additions & 25 deletions src/translucent/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,15 @@ export class Tx {
C.PartialPlutusWitness.new(
//C.Script.new_plutus_v2(C.PlutusV2Script.from_cbor_hex(script.inlineScript))
C.PlutusScriptWitness.from_script(script.inlineScript),
C.PlutusData.from_bytes(fromHex(redeemer)),
C.PlutusData.from_cbor_hex(redeemer),
),
C.RequiredSigners.new(),
);
} else {
mr = mintBuilder.plutus_script(
C.PartialPlutusWitness.new(
C.PlutusScriptWitness.from_ref(script.referenceScript.hash()),
C.PlutusData.from_bytes(fromHex(redeemer)),
C.PlutusData.from_cbor_hex(redeemer),
),
C.RequiredSigners.new(),
);
Expand Down Expand Up @@ -392,7 +392,7 @@ export class Tx {
if ("inlineScript" in script) {
cr = certBuilder.plutus_script(
C.PartialPlutusWitness.new(
C.PlutusScriptWitness.from_script(script.inlineScript),
C.PlutusScriptWitness.from(script.inlineScript),
C.PlutusData.from_cbor_hex(redeemer),
),
C.RequiredSigners.new(),
Expand Down Expand Up @@ -620,12 +620,12 @@ export class Tx {
);
}
} else {
if (rewAdd.payment_cred().kind() == 0) {
if (rewAdd.to_address().payment_cred()!.kind() == 0) {
wr = certBuilder.payment_key();
} else {
let ns =
this.native_scripts[
rewAdd.payment_cred()?.to_scripthash()?.to_hex()!
rewAdd.to_address().payment_cred()!.as_script()!.to_hex()
];
if (!ns) {
throw "Script with no redeemer should be a nativescript, but none provided";
Expand Down Expand Up @@ -709,12 +709,7 @@ export class Tx {
let md = C.Metadata.new()
md.set(BigInt(label), C.TransactionMetadatum.new_text(JSON.stringify(metadata)))

let aux = C.AuxiliaryData.new();
aux.add_json_metadatum_with_schema(
BigInt(label),
JSON.stringify(metadata),
C.MetadataJsonSchema.BasicConversions,
);
let aux = C.AuxiliaryData.new_shelley(md);
that.txBuilder.add_auxiliary_data(aux);
});
return this;
Expand All @@ -723,12 +718,10 @@ export class Tx {
/* Same as above but MORE detailed! */
attachMetadataWithDetailedConversion(label: Label, metadata: Json): Tx {
this.tasks.push((that) => {
let aux = C.AuxiliaryData.new();
aux.add_json_metadatum_with_schema(
C.BigNum.from_str(label.toString()),
JSON.stringify(metadata),
C.MetadataJsonSchema.DetailedSchema,
);
let md = C.Metadata.new()
md.set(BigInt(label), C.TransactionMetadatum.new_text(JSON.stringify(metadata)))

let aux = C.AuxiliaryData.new_shelley(md);
that.txBuilder.add_auxiliary_data(aux);
});
return this;
Expand Down Expand Up @@ -919,6 +912,7 @@ export class Tx {
draftTx = C.Transaction.new(
draftTx.body(),
witnessSet,
draftTx.is_valid(),
draftTx.auxiliary_data(),
);
}
Expand All @@ -928,7 +922,7 @@ export class Tx {
draftTxBytes,
allUtxos.map((x) => x.input().to_bytes()),
allUtxos.map((x) => x.output().to_bytes()),
costMdls.to_bytes(),
costMdls.to_cbor_bytes(),
protocolParameters.maxTxExSteps,
protocolParameters.maxTxExMem,
BigInt(slotConfig.zeroTime),
Expand Down Expand Up @@ -987,35 +981,33 @@ async function createPoolRegistration(
switch (relay.type) {
case "SingleHostIp": {
const ipV4 = relay.ipV4
? C.Ipv4.new(
? C.Ipv4.from_cbor_bytes(
new Uint8Array(relay.ipV4.split(".").map((b) => parseInt(b))),
)
: undefined;
const ipV6 = relay.ipV6
? C.Ipv6.new(fromHex(relay.ipV6.replaceAll(":", "")))
? C.Ipv6.from_cbor_hex(relay.ipV6.replaceAll(":", ""))
: undefined;
relays.add(
C.Relay.new_single_host_addr(
C.SingleHostAddr.new(relay.port, ipV4, ipV6),
relay.port, ipV4, ipV6,
),
);
break;
}
case "SingleHostDomainName": {
relays.add(
C.Relay.new_single_host_name(
C.SingleHostName.new(
relay.port,
C.DNSRecordAorAAAA.new(relay.domainName!),
),
C.DnsName.from_cbor_hex(relay.domainName!),
),
);
break;
}
case "MultiHost": {
relays.add(
C.Relay.new_multi_host_name(
C.MultiHostName.new(C.DNSRecordSRV.new(relay.domainName!)),
C.DnsName.from_cbor_hex(relay.domainName!),
),
);
break;
Expand Down
29 changes: 15 additions & 14 deletions src/translucent/tx_complete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ export class TxComplete {
this.witnessSetBuilder = C.TransactionWitnessSetBuilder.new();
this.tasks = [];

this.fee = parseInt(tx.body().fee().to_str());
this.fee = Number(tx.body().fee().valueOf());
const redeemers = tx.witness_set().redeemers();
if (redeemers) {
const exUnits = { cpu: 0, mem: 0 };
for (let i = 0; i < redeemers.len(); i++) {
const redeemer = redeemers.get(i);
exUnits.cpu += parseInt(redeemer.ex_units().steps().to_str());
exUnits.mem += parseInt(redeemer.ex_units().mem().to_str());
exUnits.cpu += Number(redeemer.ex_units().steps().valueOf());
exUnits.mem += Number(redeemer.ex_units().mem().valueOf());
}
this.exUnits = exUnits;
}
Expand All @@ -46,9 +46,9 @@ export class TxComplete {
/** Add an extra signature from a private key. */
signWithPrivateKey(privateKey: PrivateKey): TxComplete {
const priv = C.PrivateKey.from_bech32(privateKey);
const witness = C.make_vkey_witness(
C.hash_transaction(this.txComplete.body()),
priv,
const witness = C.Vkeywitness.new(
priv.to_public(),
priv.sign(this.txComplete.body().to_cbor_bytes())
);
this.witnessSetBuilder.add_vkey(witness);
return this;
Expand All @@ -58,7 +58,7 @@ export class TxComplete {
async partialSign(): Promise<TransactionWitnesses> {
const witnesses = await this.translucent.wallet.signTx(this.txComplete);
this.witnessSetBuilder.add_existing(witnesses);
return toHex(witnesses.to_bytes());
return toHex(witnesses.to_cbor_bytes());
}

/**
Expand All @@ -67,21 +67,21 @@ export class TxComplete {
*/
partialSignWithPrivateKey(privateKey: PrivateKey): TransactionWitnesses {
const priv = C.PrivateKey.from_bech32(privateKey);
const witness = C.make_vkey_witness(
C.hash_transaction(this.txComplete.body()),
priv,
const witness = C.Vkeywitness.new(
priv.to_public(),
priv.sign(this.txComplete.body().to_cbor_bytes())
);
this.witnessSetBuilder.add_vkey(witness);
const witnesses = C.TransactionWitnessSetBuilder.new();
witnesses.add_vkey(witness);
return toHex(witnesses.build().to_bytes());
return toHex(witnesses.build().to_cbor_bytes());
}

/** Sign the transaction with the given witnesses. */
assemble(witnesses: TransactionWitnesses[]): TxComplete {
witnesses.forEach((witness) => {
const witnessParsed = C.TransactionWitnessSet.from_bytes(
fromHex(witness),
const witnessParsed = C.TransactionWitnessSet.from_cbor_hex(
witness
);
this.witnessSetBuilder.add_existing(witnessParsed);
});
Expand All @@ -97,14 +97,15 @@ export class TxComplete {
const signedTx = C.Transaction.new(
this.txComplete.body(),
this.witnessSetBuilder.build(),
this.txComplete.is_valid(),
this.txComplete.auxiliary_data(),
);
return new TxSigned(this.translucent, signedTx);
}

/** Return the transaction in Hex encoded Cbor. */
toString(): Transaction {
return toHex(this.txComplete.to_bytes());
return this.txComplete.to_cbor_hex();
}

/** Return the transaction hash. */
Expand Down
24 changes: 12 additions & 12 deletions src/utils/cost_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import { C } from "../core/mod.ts";
import { CostModels } from "../mod.ts";
import { ProtocolParameters } from "../types/types.ts";

export function createCostModels(costModels: CostModels): C.Costmdls {
const costmdls = C.Costmdls.new();
export function createCostModels(costModels: CostModels): C.CostModels {
const costmdls = C.CostModels.new();

// add plutus v1
const costmdlV1 = C.CostModel.empty_model(C.Language.new_plutus_v1());
Object.values(costModels.PlutusV1).forEach((cost, index) => {
costmdlV1.set(index, C.Int.new(C.BigNum.from_str(cost.toString())));
});
costmdls.insert(costmdlV1);
const costmdlV1 = C.IntList.new()
for (const cost of Object.values(costModels.PlutusV1)){
costmdlV1.add(C.Int.new(BigInt(cost)))
}
costmdls.set_plutus_v1(costmdlV1)

// add plutus v2
const costmdlV2 = C.CostModel.empty_model(C.Language.new_plutus_v2());
Object.values(costModels.PlutusV2 || []).forEach((cost, index) => {
costmdlV2.set(index, C.Int.new(C.BigNum.from_str(cost.toString())));
});
costmdls.insert(costmdlV2);
const costmdlV2 = C.IntList.new()
for (const cost of Object.values(costModels.PlutusV2)){
costmdlV2.add(C.Int.new(BigInt(cost)))
}
costmdls.set_plutus_v2(costmdlV2);

return costmdls;
}
Expand Down
Loading

0 comments on commit f155176

Please sign in to comment.