Skip to content

Commit

Permalink
Merge pull request #34 from input-output-hk/hrajchert/isMainnet
Browse files Browse the repository at this point in the history
Changed getCIP30Network for isMainnet
  • Loading branch information
nhenin authored Sep 28, 2023
2 parents 91f889c + eed622e commit b24f3ba
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 27 deletions.
3 changes: 3 additions & 0 deletions changelog.d/20230927_204043_hrajchert_isMainnet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### @marlowe.io/wallet

- BREAKING CHANGE: Refactored getNetworkCIP30 to isMainnet
23 changes: 8 additions & 15 deletions packages/wallet/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ import {
TxOutRef,
} from "@marlowe.io/runtime-core";

/**
* Represents the connected network as seen by the CIP30.
*
* @remarks
* The Network Id returned by CIP30 Interface doesn't provide information on which Testnet Network
* the extension in configured.
* {@link https://github.com/cardano-foundation/CIPs/pull/323 | See github issue}
*
*/
export type CIP30Network = "Mainnet" | "Testnets";
export type WalletDI = { wallet: WalletAPI };
/**
* The WalletAPI is an interface for interacting with a Cardano wallet.
Expand Down Expand Up @@ -76,13 +66,16 @@ export interface WalletAPI {
* @returns The list of UTXOs available to the wallet.
*/
getUTxOs(): Promise<TxOutRef[]>;
// DISCUSSION: should we keep CIP30 as part of the name of the api?
/**
* What type of network is the wallet connected to.
* @returns The connected network as seen by the CIP30.
* @experimental
* Returns a flag that indicates wether the wallet is connected to the Mainnet or a Testnet.
*
* @remarks
* The Network Id returned by CIP30 Interface doesn't provide information on which Testnet Network
* the extension in configured.
* {@link https://github.com/cardano-foundation/CIPs/pull/323 | See github issue}
*
*/
getCIP30Network(): Promise<CIP30Network>;
isMainnet(): Promise<boolean>;
// DISCUSSION: should we rename this function to getAssets?
// DISCUSSION: Being this a Marlowe Wallet API, should we return a Marlowe Asset (Marlowe Token + Quantity) instead of
// CIP30 Token?
Expand Down
8 changes: 4 additions & 4 deletions packages/wallet/src/browser/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CIP30Network, WalletAPI } from "../api.js";
import { WalletAPI } from "../api.js";
import { C, Core, WalletApi } from "lucid-cardano";
import { hex, utf8 } from "@47ng/codec";
// DISCUSSION: these should be imported from a cardano helpers library.
Expand Down Expand Up @@ -38,7 +38,7 @@ export async function mkBrowserWallet(
getUsedAddresses: getUsedAddresses(di),
getCollaterals: getCollaterals(di),
getUTxOs: getUTxOs(di),
getCIP30Network: getCIP30Network(di),
isMainnet: isMainnet(di),
getTokens: getTokens(di),
getLovelaces: getLovelaces(di),
};
Expand Down Expand Up @@ -100,11 +100,11 @@ const getCollaterals =
return collaterals.map(deserializeTxOutRef);
};

const getCIP30Network =
const isMainnet =
({ extension }: ExtensionDI) =>
async () => {
const networkId = await extension.getNetworkId();
return networkId == 1 ? "Mainnet" : "Testnets";
return networkId == 1;
};

const getTokens =
Expand Down
8 changes: 2 additions & 6 deletions packages/wallet/src/nodejs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,8 @@ export class SingleAddressWallet implements WalletAPI {
this.getCollaterals = T.of([]);
}

async getCIP30Network() {
if (this.lucid.network === "Mainnet") {
return "Mainnet" as const;
} else {
return "Testnets" as const;
}
async isMainnet() {
return this.lucid.network === "Mainnet";
}

async getTokens(): Promise<Token[]> {
Expand Down
4 changes: 2 additions & 2 deletions pocs/wallet-flow.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
log("Reading Wallet information...");
log("");

const cip30Network = await wallet.getCIP30Network();
log(`* <b>Network</b>: ${cip30Network}`);
const isMainnet = await wallet.isMainnet();
log(`* <b>Network</b>: ${isMainnet ? "Mainnet" : "Testnet"}`);
log(
"&nbsp;&nbsp;&nbsp;NOTE: The CIP30 standard can't distinguish between testnet networks (preprod/preview/etc)"
);
Expand Down

0 comments on commit b24f3ba

Please sign in to comment.