Skip to content

Commit

Permalink
Merge branch 'main' into commonjs
Browse files Browse the repository at this point in the history
* main:
  Limited Byron support added
  Bump
  Fixed datum resolution
  Move to new maestro submit endpoint

# Conflicts:
#	package.json
#	src/lucid/tx.ts
  • Loading branch information
AlexDochioiu committed Jun 11, 2023
2 parents b6e5115 + 501c5b0 commit 8a1d2e8
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 19 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ npm install lucid-cardano
For JavaScript and TypeScript

```js
import { Lucid } from "https://deno.land/x/lucid@0.10.5/mod.ts";
import { Lucid } from "https://deno.land/x/lucid@0.10.6/mod.ts";
```

#### Web

```html
<script type="module">
import { Lucid } from "https://unpkg.com/lucid-cardano@0.10.5/web/mod.js"
import { Lucid } from "https://unpkg.com/lucid-cardano@0.10.6/web/mod.js"
// ...
</script>
```
Expand All @@ -57,7 +57,7 @@ Outputs a `dist` folder
### Basic usage

```js
// import { Blockfrost, Lucid } from "https://deno.land/x/lucid@0.10.5/mod.ts"; Deno
// import { Blockfrost, Lucid } from "https://deno.land/x/lucid@0.10.6/mod.ts"; Deno
import { Blockfrost, Lucid } from "lucid-cardano"; // NPM

const lucid = await Lucid.new(
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vespr-wallet/lucid-cardano",
"version": "0.10.5",
"version": "0.10.6",
"license": "MIT",
"description": "This is a fork of the original Lucid repo compiled into CommonJS, based on jpg-store's lucid fork. For more information check https://github.com/spacebudz/lucid",
"repository": "https://github.com/vespr-wallet/lucid"
Expand Down
15 changes: 9 additions & 6 deletions src/lucid/tx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { C, Core } from "../core/mod.ts";
import { Data } from "../mod.ts";
import {
Address,
Assets,
Expand Down Expand Up @@ -49,7 +50,7 @@ export class Tx {
this.tasks.push(async (that) => {
for (const utxo of utxos) {
if (utxo.datumHash) {
utxo.datum = await that.lucid.datumOf(utxo);
utxo.datum = Data.to(await that.lucid.datumOf(utxo));
// Add datum to witness set, so it can be read from validators
const plutusData = C.PlutusData.from_bytes(fromHex(utxo.datum!));
that.txBuilder.add_plutus_data(plutusData);
Expand All @@ -69,7 +70,7 @@ export class Tx {
this.tasks.push(async (that) => {
for (const utxo of utxos) {
if (utxo.datumHash && !utxo.datum) {
utxo.datum = await that.lucid.datumOf(utxo);
utxo.datum = Data.to(await that.lucid.datumOf(utxo));
}
const coreUtxo = utxoToCore(utxo);
that.txBuilder.add_input(
Expand Down Expand Up @@ -715,13 +716,15 @@ function addressFromWithNetworkCheck(
address: Address | RewardAddress,
lucid: Lucid,
): Core.Address {
const addressDetails = lucid.utils.getAddressDetails(address);
const { type, networkId } = lucid.utils.getAddressDetails(address);

const actualNetworkId = networkToId(lucid.network);
if (addressDetails.networkId !== actualNetworkId) {
if (networkId !== actualNetworkId) {
throw new Error(
`Invalid address: Expected address with network id ${actualNetworkId}, but got ${addressDetails.networkId}`,
`Invalid address: Expected address with network id ${actualNetworkId}, but got ${networkId}`,
);
}
return C.Address.from_bech32(address);
return type === "Byron"
? C.ByronAddress.from_base58(address).to_address()
: C.Address.from_bech32(address);
}
17 changes: 9 additions & 8 deletions src/provider/maestro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ export class Maestro implements Provider {
const result = await fetch(`${this.url}/protocol-params`, {
headers: this.commonHeaders(),
}).then((res) => res.json());
// Read two numbers which are given in format of number one, then a seperator '/', then number two.
const readTwoNumbers = (str: string): number =>
parseInt(str.substring(0, str.indexOf("/"))) /
parseInt(str.slice(str.indexOf("/") + 1));
// Decimal numbers in Maestro are given as ratio of two numbers represented by string of format "firstNumber/secondNumber".
const decimalFromRationalString = (str: string): number => {
const forwardSlashIndex = str.indexOf("/");
return parseInt(str.slice(0, forwardSlashIndex)) / parseInt(str.slice(forwardSlashIndex + 1));
}
// To rename keys in an object by the given key-map.
// deno-lint-ignore no-explicit-any
const renameKeysAndSort = (obj: any, newKeys: any) => {
Expand All @@ -61,8 +62,8 @@ export class Maestro implements Provider {
maxValSize: parseInt(result.max_value_size),
keyDeposit: BigInt(result.stake_key_deposit),
poolDeposit: BigInt(result.pool_deposit),
priceMem: readTwoNumbers(result.prices.memory),
priceStep: readTwoNumbers(result.prices.steps),
priceMem: decimalFromRationalString(result.prices.memory),
priceStep: decimalFromRationalString(result.prices.steps),
maxTxExMem: BigInt(result.max_execution_units_per_transaction.memory),
maxTxExSteps: BigInt(result.max_execution_units_per_transaction.steps),
coinsPerUtxoByte: BigInt(result.coins_per_utxo_byte),
Expand Down Expand Up @@ -198,7 +199,7 @@ export class Maestro implements Provider {
}

async submitTx(tx: Transaction): Promise<TxHash> {
const response = await fetch(`${this.url}/transactions`, {
const response = await fetch(`${this.url}/txmanager`, {
method: "POST",
headers: {
"Content-Type": "application/cbor",
Expand Down Expand Up @@ -233,7 +234,7 @@ export class Maestro implements Provider {
datumHash: result.datum
? result.datum.type == "inline" ? undefined : result.datum.hash
: undefined,
datum: result?.datum?.bytes ? result.datum.bytes : undefined,
datum: result.datum?.bytes,
scriptRef: result.reference_script
? result.reference_script.type == "native" ? undefined : {
type: result.reference_script.type == "plutusv1"
Expand Down
7 changes: 6 additions & 1 deletion src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,12 @@ export type UTxO = {

export type OutRef = { txHash: TxHash; outputIndex: number };

export type AddressType = "Base" | "Enterprise" | "Pointer" | "Reward";
export type AddressType =
| "Base"
| "Enterprise"
| "Pointer"
| "Reward"
| "Byron";

export type Network = "Mainnet" | "Preview" | "Preprod" | "Custom";

Expand Down
24 changes: 24 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,30 @@ export function getAddressDetails(address: string): AddressDetails {
/* pass */
}

// Limited support for Byron addresses
try {
const parsedAddress = ((address: string): Core.ByronAddress => {
try {
return C.ByronAddress.from_bytes(fromHex(address));
} catch (_e) {
try {
return C.ByronAddress.from_base58(address);
} catch (_e) {
throw new Error("Could not deserialize address.");
}
}
})(address);

return {
type: "Byron",
networkId: parsedAddress.network_id(),
address: {
bech32: "",
hex: toHex(parsedAddress.to_address().to_bytes()),
},
};
} catch (_e) { /* pass */ }

throw new Error("No address type matched for: " + address);
}

Expand Down

0 comments on commit 8a1d2e8

Please sign in to comment.