diff --git a/bun.lockb b/bun.lockb deleted file mode 100755 index 923271e..0000000 Binary files a/bun.lockb and /dev/null differ diff --git a/packages/translucent/index.ts b/index.ts similarity index 100% rename from packages/translucent/index.ts rename to index.ts diff --git a/package.json b/package.json index 1a74075..e82446f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,39 @@ { - "name": "translucent-monorepo", - "workspaces": [ - "packages/*" - ], - "version": "0.0.1" + "name": "translucent-cardano", + "module": "index.ts", + "type": "module", + "version": "0.0.5", + "scripts": { + "build:wasm": "cd ./uplc && ./build.sh && rm pkg-node/.gitignore && rm pkg-web/.gitignore && cd .. && npm i" + }, + "devDependencies": { + "@types/jest": "^29.5.5", + "@types/sha256": "^0.2.2", + "bun-types": "latest", + "eslint": "^8.50.0", + "jest": "^29.7.0", + "ts-jest": "^29.1.1", + "typescript": "^5.3.3", + "@cardano-ogmios/schema": "^6.0.0-rc6" + }, + "dependencies": { + "@dcspark/cardano-multiplatform-lib-browser": "^3.1.2", + "@dcspark/cardano-multiplatform-lib-nodejs": "^3.1.2", + "@emurgo/cardano-message-signing-browser": "^1.0.1", + "@emurgo/cardano-message-signing-nodejs": "^1.0.1", + "@sinclair/typebox": "^0.31.28", + "fast-check": "^3.14.0", + "prettier": "^3.1.1", + "sha256": "^0.2.0", + "uplc-node": "^0.0.3", + "uplc-web": "^0.0.3" + }, + "browser": { + "@dcspark/cardano-multiplatform-lib-nodejs": "@dcspark/cardano-multiplatform-lib-browser", + "@emurgo/cardano-message-signing-nodejs": "@emurgo/cardano-message-signing-browser", + "uplc-node": "uplc-web" + }, + "peerDependencies": { + "typescript": "^5.0.0" + } } diff --git a/packages/translucent-blueprint/cli.ts b/packages/translucent-blueprint/cli.ts old mode 100644 new mode 100755 diff --git a/packages/translucent/package.json b/packages/translucent/package.json deleted file mode 100644 index e82446f..0000000 --- a/packages/translucent/package.json +++ /dev/null @@ -1,39 +0,0 @@ -{ - "name": "translucent-cardano", - "module": "index.ts", - "type": "module", - "version": "0.0.5", - "scripts": { - "build:wasm": "cd ./uplc && ./build.sh && rm pkg-node/.gitignore && rm pkg-web/.gitignore && cd .. && npm i" - }, - "devDependencies": { - "@types/jest": "^29.5.5", - "@types/sha256": "^0.2.2", - "bun-types": "latest", - "eslint": "^8.50.0", - "jest": "^29.7.0", - "ts-jest": "^29.1.1", - "typescript": "^5.3.3", - "@cardano-ogmios/schema": "^6.0.0-rc6" - }, - "dependencies": { - "@dcspark/cardano-multiplatform-lib-browser": "^3.1.2", - "@dcspark/cardano-multiplatform-lib-nodejs": "^3.1.2", - "@emurgo/cardano-message-signing-browser": "^1.0.1", - "@emurgo/cardano-message-signing-nodejs": "^1.0.1", - "@sinclair/typebox": "^0.31.28", - "fast-check": "^3.14.0", - "prettier": "^3.1.1", - "sha256": "^0.2.0", - "uplc-node": "^0.0.3", - "uplc-web": "^0.0.3" - }, - "browser": { - "@dcspark/cardano-multiplatform-lib-nodejs": "@dcspark/cardano-multiplatform-lib-browser", - "@emurgo/cardano-message-signing-nodejs": "@emurgo/cardano-message-signing-browser", - "uplc-node": "uplc-web" - }, - "peerDependencies": { - "typescript": "^5.0.0" - } -} diff --git a/packages/translucent/src/core/core-browser.ts b/src/core/core-browser.ts similarity index 100% rename from packages/translucent/src/core/core-browser.ts rename to src/core/core-browser.ts diff --git a/packages/translucent/src/core/core.ts b/src/core/core.ts similarity index 100% rename from packages/translucent/src/core/core.ts rename to src/core/core.ts diff --git a/packages/translucent/src/core/mod.ts b/src/core/mod.ts similarity index 100% rename from packages/translucent/src/core/mod.ts rename to src/core/mod.ts diff --git a/packages/translucent/src/examples/common.ts b/src/examples/common.ts similarity index 100% rename from packages/translucent/src/examples/common.ts rename to src/examples/common.ts diff --git a/packages/translucent/src/examples/mint_nft.ts b/src/examples/mint_nft.ts similarity index 100% rename from packages/translucent/src/examples/mint_nft.ts rename to src/examples/mint_nft.ts diff --git a/packages/translucent/src/misc/bip39.ts b/src/misc/bip39.ts similarity index 100% rename from packages/translucent/src/misc/bip39.ts rename to src/misc/bip39.ts diff --git a/packages/translucent/src/misc/crc8.ts b/src/misc/crc8.ts similarity index 100% rename from packages/translucent/src/misc/crc8.ts rename to src/misc/crc8.ts diff --git a/packages/translucent/src/misc/sign_data.ts b/src/misc/sign_data.ts similarity index 100% rename from packages/translucent/src/misc/sign_data.ts rename to src/misc/sign_data.ts diff --git a/packages/translucent/src/misc/wallet.ts b/src/misc/wallet.ts similarity index 100% rename from packages/translucent/src/misc/wallet.ts rename to src/misc/wallet.ts diff --git a/packages/translucent/src/mod.ts b/src/mod.ts similarity index 100% rename from packages/translucent/src/mod.ts rename to src/mod.ts diff --git a/packages/translucent/src/plutus/data.ts b/src/plutus/data.ts similarity index 100% rename from packages/translucent/src/plutus/data.ts rename to src/plutus/data.ts diff --git a/packages/translucent/src/plutus/mod.ts b/src/plutus/mod.ts similarity index 100% rename from packages/translucent/src/plutus/mod.ts rename to src/plutus/mod.ts diff --git a/packages/translucent/src/plutus/time.ts b/src/plutus/time.ts similarity index 100% rename from packages/translucent/src/plutus/time.ts rename to src/plutus/time.ts diff --git a/packages/translucent/src/provider/emulator.ts b/src/provider/emulator.ts similarity index 100% rename from packages/translucent/src/provider/emulator.ts rename to src/provider/emulator.ts diff --git a/packages/translucent/src/provider/kupmios.ts b/src/provider/kupmios.ts similarity index 72% rename from packages/translucent/src/provider/kupmios.ts rename to src/provider/kupmios.ts index 21e533e..9eea8c9 100644 --- a/packages/translucent/src/provider/kupmios.ts +++ b/src/provider/kupmios.ts @@ -16,7 +16,7 @@ import { UTxO, } from "../types/mod.ts"; import { C } from "../core/mod.ts"; -import { costModelKeys, fromUnit } from "../utils/mod.ts"; +import { costModelKeys, fromHex, fromUnit, toHex } from "../utils/mod.ts"; import * as ogmios from "@cardano-ogmios/schema"; function fromMaybeBuffer(x: string | Buffer) { @@ -68,7 +68,7 @@ export class Kupmios implements Provider { client.addEventListener( "message", (msg: MessageEvent): unknown => { - console.log("queryLedgerState/protocolParameters", msg.data); + //console.log("queryLedgerState/protocolParameters", msg.data); try { const { result, @@ -97,11 +97,11 @@ export class Kupmios implements Provider { result.scriptExecutionPrices!.cpu.split("/"); const protocolParams: ProtocolParameters = { minFeeA: result.minFeeCoefficient, - minFeeB: Number(result.minFeeConstant.lovelace), + minFeeB: Number(result.minFeeConstant.ada.lovelace), maxTxSize: result.maxTransactionSize!.bytes, maxValSize: result.maxValueSize!.bytes, - keyDeposit: BigInt(result.stakeCredentialDeposit.lovelace), - poolDeposit: BigInt(result.stakePoolDeposit.lovelace), + keyDeposit: BigInt(result.stakeCredentialDeposit.ada.lovelace), + poolDeposit: BigInt(result.stakePoolDeposit.ada.lovelace), priceMem: [BigInt(memNum), BigInt(memDenom)], priceStep: [BigInt(stepsNum), BigInt(stepsDenom)], maxTxExMem: BigInt( @@ -126,54 +126,17 @@ export class Kupmios implements Provider { } async getUtxos(addressOrCredential: Address | Credential): Promise { - let addy = - typeof addressOrCredential == "string" + const isAddress = typeof addressOrCredential === "string"; + const queryPredicate = isAddress ? addressOrCredential - : C.EnterpriseAddress.new( - 0, - C.StakeCredential.from_keyhash( - C.Ed25519KeyHash.from_hex(addressOrCredential.hash), - ), - ) - .to_address() - .to_bech32(undefined); - let params: ogmios.UtxoByAddresses | ogmios.UtxoByOutputReferences = { - addresses: [addy], - }; - const client = await this.ogmiosWsp("queryLedgerState/utxo", params); - return new Promise((res, rej) => { - client.addEventListener( - "message", - (msg: MessageEvent) => { - try { - const response: - | ogmios.QueryLedgerStateUtxoResponse - | ogmios.QueryLedgerStateEraMismatch - | ogmios.QueryLedgerStateAcquiredExpired = JSON.parse( - fromMaybeBuffer(msg.data), - ); - if ("result" in response) { - res( - response.result.map((utxo) => { - return { - txHash: utxo.transaction.id, - outputIndex: utxo.index, - assets: fromOgmiosValue(utxo.value), - address: utxo.address, - datumHash: utxo.datumHash, - datum: utxo.datum, - script: utxo.script, - } as UTxO; - }), - ); - } else { - console.error("UTXO Fetch error", response.error); - } - } catch {} - }, - ); - }); - } + : addressOrCredential.hash; + const result = await fetch( + `${this.kupoUrl}/matches/${queryPredicate}${ + isAddress ? "" : "/*" + }?unspent`, + ).then((res) => res.json()); + return this.kupmiosUtxosToUtxos(result); + } async getUtxosWithUnit( addressOrCredential: Address | Credential, @@ -231,44 +194,26 @@ export class Kupmios implements Provider { } async getUtxosByOutRef(outRefs: Array): Promise { - let params: ogmios.UtxoByAddresses | ogmios.UtxoByOutputReferences = { - outputReferences: outRefs.map((x) => { - return { transaction: { id: x.txHash }, index: x.outputIndex }; + const queryHashes = [...new Set(outRefs.map((outRef) => outRef.txHash))]; + + const utxos = await Promise.all( + queryHashes.map(async (txHash) => { + const result = await fetch( + `${this.kupoUrl}/matches/*@${txHash}?unspent`, + ).then((res) => res.json()); + return this.kupmiosUtxosToUtxos(result); }), - }; - const client = await this.ogmiosWsp("queryLedgerState/utxo", params); - return new Promise((res, rej) => { - client.addEventListener( - "message", - (msg: MessageEvent) => { - try { - const response: - | ogmios.QueryLedgerStateUtxoResponse - | ogmios.QueryLedgerStateEraMismatch - | ogmios.QueryLedgerStateAcquiredExpired = JSON.parse( - fromMaybeBuffer(msg.data), - ); - if ("result" in response) { - res( - response.result.map((utxo) => { - return { - txHash: utxo.transaction.id, - outputIndex: utxo.index, - assets: fromOgmiosValue(utxo.value), - address: utxo.address, - datumHash: utxo.datumHash, - datum: utxo.datum, - script: utxo.script, - } as UTxO; - }), - ); - } else { - console.error("UTXO Fetch error", response.error); - } - } catch {} - }, + ); + + return utxos + .reduce((acc, utxos) => acc.concat(utxos), []) + .filter((utxo) => + outRefs.some( + (outRef) => + utxo.txHash === outRef.txHash && + utxo.outputIndex === outRef.outputIndex, + ), ); - }); } async getDelegation(rewardAddress: RewardAddress): Promise { @@ -376,4 +321,54 @@ export class Kupmios implements Provider { ); return client; } + + private kupmiosUtxosToUtxos(utxos: unknown): Promise { + // deno-lint-ignore no-explicit-any + return Promise.all( + (utxos as any).map(async (utxo: any) => { + return { + txHash: utxo.transaction_id, + outputIndex: parseInt(utxo.output_index), + address: utxo.address, + assets: (() => { + const a: Assets = { lovelace: BigInt(utxo.value.coins) }; + Object.keys(utxo.value.assets).forEach((unit) => { + a[unit.replace(".", "")] = BigInt(utxo.value.assets[unit]); + }); + return a; + })(), + datumHash: utxo?.datum_type === "hash" ? utxo.datum_hash : null, + datum: + utxo?.datum_type === "inline" + ? await this.getDatum(utxo.datum_hash) + : null, + scriptRef: + utxo.script_hash && + (await (async () => { + const { script, language } = await fetch( + `${this.kupoUrl}/scripts/${utxo.script_hash}`, + ).then((res) => res.json()); + + if (language === "native") { + return { type: "Native", script }; + } else if (language === "plutus:v1") { + return { + type: "PlutusV1", + script: toHex( + C.PlutusV1Script.new(fromHex(script)).to_bytes(), + ), + }; + } else if (language === "plutus:v2") { + return { + type: "PlutusV2", + script: toHex( + C.PlutusV2Script.new(fromHex(script)).to_bytes(), + ), + }; + } + })()), + } as UTxO; + }), + ); + } } diff --git a/packages/translucent/src/provider/kupmiosv5.ts b/src/provider/kupmiosv5.ts similarity index 100% rename from packages/translucent/src/provider/kupmiosv5.ts rename to src/provider/kupmiosv5.ts diff --git a/packages/translucent/src/provider/maestro.ts b/src/provider/maestro.ts similarity index 100% rename from packages/translucent/src/provider/maestro.ts rename to src/provider/maestro.ts diff --git a/packages/translucent/src/provider/mod.ts b/src/provider/mod.ts similarity index 100% rename from packages/translucent/src/provider/mod.ts rename to src/provider/mod.ts diff --git a/packages/translucent/src/translucent/message.ts b/src/translucent/message.ts similarity index 100% rename from packages/translucent/src/translucent/message.ts rename to src/translucent/message.ts diff --git a/packages/translucent/src/translucent/mod.ts b/src/translucent/mod.ts similarity index 100% rename from packages/translucent/src/translucent/mod.ts rename to src/translucent/mod.ts diff --git a/packages/translucent/src/translucent/translucent.ts b/src/translucent/translucent.ts similarity index 100% rename from packages/translucent/src/translucent/translucent.ts rename to src/translucent/translucent.ts diff --git a/packages/translucent/src/translucent/tx.ts b/src/translucent/tx.ts similarity index 100% rename from packages/translucent/src/translucent/tx.ts rename to src/translucent/tx.ts diff --git a/packages/translucent/src/translucent/tx_complete.ts b/src/translucent/tx_complete.ts similarity index 100% rename from packages/translucent/src/translucent/tx_complete.ts rename to src/translucent/tx_complete.ts diff --git a/packages/translucent/src/translucent/tx_signed.ts b/src/translucent/tx_signed.ts similarity index 100% rename from packages/translucent/src/translucent/tx_signed.ts rename to src/translucent/tx_signed.ts diff --git a/packages/translucent/src/types/global.ts b/src/types/global.ts similarity index 100% rename from packages/translucent/src/types/global.ts rename to src/types/global.ts diff --git a/packages/translucent/src/types/mod.ts b/src/types/mod.ts similarity index 100% rename from packages/translucent/src/types/mod.ts rename to src/types/mod.ts diff --git a/packages/translucent/src/types/types.ts b/src/types/types.ts similarity index 100% rename from packages/translucent/src/types/types.ts rename to src/types/types.ts diff --git a/packages/translucent/src/utils/cost_model.ts b/src/utils/cost_model.ts similarity index 100% rename from packages/translucent/src/utils/cost_model.ts rename to src/utils/cost_model.ts diff --git a/packages/translucent/src/utils/mod.ts b/src/utils/mod.ts similarity index 100% rename from packages/translucent/src/utils/mod.ts rename to src/utils/mod.ts diff --git a/packages/translucent/src/utils/to.ts b/src/utils/to.ts similarity index 100% rename from packages/translucent/src/utils/to.ts rename to src/utils/to.ts diff --git a/packages/translucent/src/utils/utils.ts b/src/utils/utils.ts similarity index 100% rename from packages/translucent/src/utils/utils.ts rename to src/utils/utils.ts diff --git a/packages/translucent/src/wallets/abstract.ts b/src/wallets/abstract.ts similarity index 100% rename from packages/translucent/src/wallets/abstract.ts rename to src/wallets/abstract.ts diff --git a/packages/translucent/src/wallets/chained.ts b/src/wallets/chained.ts similarity index 100% rename from packages/translucent/src/wallets/chained.ts rename to src/wallets/chained.ts diff --git a/packages/translucent/src/wallets/mod.ts b/src/wallets/mod.ts similarity index 100% rename from packages/translucent/src/wallets/mod.ts rename to src/wallets/mod.ts diff --git a/packages/translucent/src/wallets/private_key.ts b/src/wallets/private_key.ts similarity index 100% rename from packages/translucent/src/wallets/private_key.ts rename to src/wallets/private_key.ts diff --git a/packages/translucent/src/wallets/public_wallet.ts b/src/wallets/public_wallet.ts similarity index 100% rename from packages/translucent/src/wallets/public_wallet.ts rename to src/wallets/public_wallet.ts diff --git a/packages/translucent/src/wallets/seed.ts b/src/wallets/seed.ts similarity index 100% rename from packages/translucent/src/wallets/seed.ts rename to src/wallets/seed.ts diff --git a/packages/translucent/src/wallets/wallet_connector.ts b/src/wallets/wallet_connector.ts similarity index 100% rename from packages/translucent/src/wallets/wallet_connector.ts rename to src/wallets/wallet_connector.ts diff --git a/packages/translucent/tests/data.test.ts b/tests/data.test.ts similarity index 100% rename from packages/translucent/tests/data.test.ts rename to tests/data.test.ts diff --git a/packages/translucent/tests/emulator.test.ts b/tests/emulator.test.ts similarity index 100% rename from packages/translucent/tests/emulator.test.ts rename to tests/emulator.test.ts diff --git a/packages/translucent/tests/mod.test.ts b/tests/mod.test.ts similarity index 100% rename from packages/translucent/tests/mod.test.ts rename to tests/mod.test.ts diff --git a/packages/translucent/tests/test_utils/config.ts b/tests/test_utils/config.ts similarity index 100% rename from packages/translucent/tests/test_utils/config.ts rename to tests/test_utils/config.ts diff --git a/packages/translucent/tests/test_utils/mod.ts b/tests/test_utils/mod.ts similarity index 100% rename from packages/translucent/tests/test_utils/mod.ts rename to tests/test_utils/mod.ts diff --git a/packages/translucent/tests/txs/mint.test.ts b/tests/txs/mint.test.ts similarity index 100% rename from packages/translucent/tests/txs/mint.test.ts rename to tests/txs/mint.test.ts diff --git a/tsconfig.json b/tsconfig.json index 5216b4a..e9c3ad3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -18,5 +18,5 @@ "esModuleInterop": true }, "exclude": ["node_modules"], - "include": ["packages/**/*.ts", "*.json"] + "include": ["src/**/*.ts", "*.json"] }