Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions examples/developer-controlled-wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ async function main() {

// Get Cardano wallet
const { cardanoWallet: cardano } = await sdk.wallet.getWallet(info.id, "cardano");
const addresses = cardano!.getAddresses();
console.log("Cardano base address:", addresses.baseAddressBech32);
const cardanoAddress = await cardano!.getChangeAddressBech32();
console.log("Cardano base address:", cardanoAddress);

// Get Spark wallet info
const sparkWalletInfo = await sdk.wallet.sparkIssuer.getWallet(info.id);
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default function Home() {
const user = web3Wallet.getUser();

const cardanoAddress =
(await web3Wallet.cardano.getChangeAddress()) || "";
(await web3Wallet.cardano.getChangeAddressBech32()) || "";
const bitcoinAddresses = await web3Wallet.bitcoin.getAddresses();
const bitcoinAddress = bitcoinAddresses[0]?.address || "";
const sparkAddressInfo = web3Wallet.spark.getAddress();
Expand Down
2 changes: 1 addition & 1 deletion examples/react-native/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default function App() {
const user = web3Wallet.getUser();

const cardanoAddress =
(await web3Wallet.cardano.getChangeAddress()) || "";
(await web3Wallet.cardano.getChangeAddressBech32()) || "";
const bitcoinAddresses = await web3Wallet.bitcoin.getAddresses();
const bitcoinAddress = bitcoinAddresses[0]?.address || "";
const sparkAddressInfo = web3Wallet.spark.getAddress();
Expand Down
2 changes: 1 addition & 1 deletion examples/third-party-auth/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default function Home() {
refreshToken: token as string,
});

console.log(await wallet.cardano.getChangeAddress());
console.log(await wallet.cardano.getChangeAddressBech32());
}}
>
Auto Sign In With Token{" "}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@utxos/sdk",
"version": "0.1.5",
"version": "0.2.0",
"description": "UTXOS SDK - Web3 infrastructure platform for UTXO blockchains",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
Expand Down Expand Up @@ -66,7 +66,7 @@
"@meshsdk/common": "1.9.0-beta.89",
"@meshsdk/core-cst": "1.9.0-beta.89",
"@meshsdk/transaction": "1.9.0-beta.89",
"@meshsdk/wallet": "1.9.0-beta.89",
"@meshsdk/wallet": "2.0.0-beta.5",
"@peculiar/webcrypto": "^1.5.0",
"axios": "^1.8.3",
"base32-encoding": "^1.0.0",
Expand Down
35 changes: 10 additions & 25 deletions src/functions/client/derive-wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import { encryptWithCipher } from "../crypto";
import { spiltKeyIntoShards } from "../key-shard";

// Mock external wallet SDKs
const mockCardanoWalletInstance = {
getUsedAddressesBech32: jest.fn().mockResolvedValue(["addr_test1..."]),
};

jest.mock("@meshsdk/wallet", () => ({
MeshWallet: jest.fn().mockImplementation(() => ({
init: jest.fn().mockResolvedValue(undefined),
getUsedAddresses: jest.fn().mockResolvedValue(["addr_test1..."]),
})),
MeshCardanoHeadlessWallet: {
fromMnemonic: jest.fn().mockResolvedValue(mockCardanoWalletInstance),
},
}));

jest.mock("@meshsdk/bitcoin", () => ({
Expand All @@ -28,7 +31,7 @@ jest.mock("@buildonspark/spark-sdk", () => ({

// Import after mocks
import { clientDeriveWallet } from "./derive-wallet";
import { MeshWallet } from "@meshsdk/wallet";
import { MeshCardanoHeadlessWallet } from "@meshsdk/wallet";
import { EmbeddedWallet } from "@meshsdk/bitcoin";
import { SparkWallet } from "@buildonspark/spark-sdk";

Expand Down Expand Up @@ -122,7 +125,7 @@ describe("clientDeriveWallet", () => {
expect(EmbeddedWallet).toHaveBeenCalledWith(
expect.objectContaining({ network: "Testnet" }),
);
expect(MeshWallet).toHaveBeenCalledWith(
expect(MeshCardanoHeadlessWallet.fromMnemonic).toHaveBeenCalledWith(
expect.objectContaining({ networkId: 0 }),
);
expect(SparkWallet.initialize).toHaveBeenCalledWith(
Expand All @@ -146,7 +149,7 @@ describe("clientDeriveWallet", () => {
expect(EmbeddedWallet).toHaveBeenCalledWith(
expect.objectContaining({ network: "Mainnet" }),
);
expect(MeshWallet).toHaveBeenCalledWith(
expect(MeshCardanoHeadlessWallet.fromMnemonic).toHaveBeenCalledWith(
expect.objectContaining({ networkId: 1 }),
);
expect(SparkWallet.initialize).toHaveBeenCalledWith(
Expand Down Expand Up @@ -241,24 +244,6 @@ describe("clientDeriveWallet", () => {
expect(result.key).toBe(testMnemonic);
});

it("initializes cardano wallet", async () => {
const shards = await spiltKeyIntoShards(testMnemonic);
const deviceKey = await deriveKeyFromPassword("device-password");

const encryptedDeviceShard = await encryptWithCipher({
data: shards[0]!,
key: deviceKey,
});

const result = await clientDeriveWallet(
encryptedDeviceShard,
deviceKey,
shards[1]!,
0,
);

expect(result.cardanoWallet.init).toHaveBeenCalled();
});
});

describe("clientDeriveWallet with 24-word mnemonic", () => {
Expand Down
3 changes: 3 additions & 0 deletions src/functions/client/derive-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IBitcoinProvider } from "@meshsdk/bitcoin";
import { IFetcher } from "@meshsdk/common";
import { decryptWithCipher } from "../crypto";
import { combineShardsBuildWallet } from "../key-shard";

Expand All @@ -8,6 +9,7 @@ export async function clientDeriveWallet(
custodialShard: string,
networkId: 0 | 1,
bitcoinProvider?: IBitcoinProvider,
fetcher?: IFetcher,
) {
const keyShare1 = await decryptWithCipher({
encryptedDataJSON: encryptedKeyShard,
Expand All @@ -21,6 +23,7 @@ export async function clientDeriveWallet(
keyShare1,
keyShare2,
bitcoinProvider,
fetcher,
);

return { bitcoinWallet, cardanoWallet, sparkWallet, key };
Expand Down
22 changes: 12 additions & 10 deletions src/functions/client/generate-wallet.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { crypto } from "../crypto";

// Mock external wallet SDKs
const mockCardanoWalletInstance = {
getChangeAddressBech32: jest.fn().mockResolvedValue(
"addr_test1qz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3jcu5d8ps7zex2k2xt3uqxgjqnnj83ws8lhrn648jjxtwq2ytjqp",
),
};

jest.mock("@meshsdk/wallet", () => ({
MeshWallet: jest.fn().mockImplementation(() => ({
init: jest.fn().mockResolvedValue(undefined),
getAddresses: jest.fn().mockResolvedValue({
baseAddressBech32:
"addr_test1qz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3jcu5d8ps7zex2k2xt3uqxgjqnnj83ws8lhrn648jjxtwq2ytjqp",
}),
})),
MeshCardanoHeadlessWallet: {
fromMnemonic: jest.fn().mockResolvedValue(mockCardanoWalletInstance),
},
}));

jest.mock("@meshsdk/common", () => ({
Expand Down Expand Up @@ -47,7 +49,7 @@ jest.mock("@buildonspark/spark-sdk", () => ({
// Import after mocks
import { clientGenerateWallet } from "./generate-wallet";
import { generateMnemonic } from "@meshsdk/common";
import { MeshWallet } from "@meshsdk/wallet";
import { MeshCardanoHeadlessWallet } from "@meshsdk/wallet";
import { EmbeddedWallet } from "@meshsdk/bitcoin";
import { SparkWallet } from "@buildonspark/spark-sdk";
import { decryptWithCipher } from "../crypto";
Expand Down Expand Up @@ -204,13 +206,13 @@ describe("clientGenerateWallet", () => {
);
});

it("creates MeshWallet with networkId 1", async () => {
it("creates MeshCardanoHeadlessWallet with networkId 1", async () => {
const deviceKey = await deriveKeyFromPassword("device-password");
const recoveryKey = await deriveKeyFromPassword("recovery-password");

await clientGenerateWallet(deviceKey, recoveryKey);

expect(MeshWallet).toHaveBeenCalledWith(
expect(MeshCardanoHeadlessWallet.fromMnemonic).toHaveBeenCalledWith(
expect.objectContaining({ networkId: 1 }),
);
});
Expand Down
15 changes: 6 additions & 9 deletions src/functions/client/generate-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MeshWallet } from "@meshsdk/wallet";
import { MeshCardanoHeadlessWallet } from "@meshsdk/wallet";
import { generateMnemonic } from "@meshsdk/common";
import { deserializeBech32Address } from "@meshsdk/core-cst";
import { EmbeddedWallet } from "@meshsdk/bitcoin";
Expand Down Expand Up @@ -47,18 +47,15 @@ export async function clientGenerateWallet(
const bitcoinMainnetPubKeyHash = bitcoinMainnetWallet.getPublicKey();

/* cardano */
const cardanoWallet = new MeshWallet({
const cardanoWallet = await MeshCardanoHeadlessWallet.fromMnemonic({
mnemonic: mnemonic.split(" "),
networkId: 1,
key: {
type: "mnemonic",
words: mnemonic.split(" "),
},
walletAddressType: 1,
});
await cardanoWallet.init();

const cardanoAddresses = await cardanoWallet.getAddresses();
const cardanoBaseAddress = await cardanoWallet.getChangeAddressBech32();
const cardanoKeyHashes = deserializeBech32Address(
cardanoAddresses.baseAddressBech32!,
cardanoBaseAddress,
);

/* spark */
Expand Down
6 changes: 3 additions & 3 deletions src/functions/client/recovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { shamirCombine } from "../key-shard";

// Mock external wallet SDKs
jest.mock("@meshsdk/wallet", () => ({
MeshWallet: jest.fn().mockImplementation(() => ({
init: jest.fn().mockResolvedValue(undefined),
})),
MeshCardanoHeadlessWallet: {
fromMnemonic: jest.fn().mockResolvedValue({}),
},
}));

jest.mock("@meshsdk/bitcoin", () => ({
Expand Down
41 changes: 14 additions & 27 deletions src/functions/key-shard/combine-shards-build-wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import { hexToBytes, bytesToString } from "../convertors";
import { shamirCombine } from "./shamir-secret-sharing";

// Mock external wallet SDKs
const mockCardanoWalletInstance = {
getUsedAddressesBech32: jest.fn().mockResolvedValue(["addr_test1..."]),
};

jest.mock("@meshsdk/wallet", () => ({
MeshWallet: jest.fn().mockImplementation(() => ({
init: jest.fn().mockResolvedValue(undefined),
getUsedAddresses: jest.fn().mockResolvedValue(["addr_test1..."]),
})),
MeshCardanoHeadlessWallet: {
fromMnemonic: jest.fn().mockResolvedValue(mockCardanoWalletInstance),
},
}));

jest.mock("@meshsdk/bitcoin", () => ({
Expand All @@ -28,7 +31,7 @@ jest.mock("@buildonspark/spark-sdk", () => ({

// Import after mocks are set up
import { combineShardsBuildWallet } from "./combine-shards-build-wallet";
import { MeshWallet } from "@meshsdk/wallet";
import { MeshCardanoHeadlessWallet } from "@meshsdk/wallet";
import { EmbeddedWallet } from "@meshsdk/bitcoin";
import { SparkWallet } from "@buildonspark/spark-sdk";

Expand Down Expand Up @@ -101,38 +104,32 @@ describe("combineShardsBuildWallet", () => {
);
});

it("initializes MeshWallet with correct networkId for testnet", async () => {
it("initializes MeshCardanoHeadlessWallet with correct networkId for testnet", async () => {
const mnemonic =
"abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
const shards = await spiltKeyIntoShards(mnemonic);

await combineShardsBuildWallet(0, shards[0]!, shards[1]!);

expect(MeshWallet).toHaveBeenCalledWith(
expect(MeshCardanoHeadlessWallet.fromMnemonic).toHaveBeenCalledWith(
expect.objectContaining({
networkId: 0,
key: expect.objectContaining({
type: "mnemonic",
words: mnemonic.split(" "),
}),
mnemonic: mnemonic.split(" "),
}),
);
});

it("initializes MeshWallet with correct networkId for mainnet", async () => {
it("initializes MeshCardanoHeadlessWallet with correct networkId for mainnet", async () => {
const mnemonic =
"abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
const shards = await spiltKeyIntoShards(mnemonic);

await combineShardsBuildWallet(1, shards[0]!, shards[1]!);

expect(MeshWallet).toHaveBeenCalledWith(
expect(MeshCardanoHeadlessWallet.fromMnemonic).toHaveBeenCalledWith(
expect.objectContaining({
networkId: 1,
key: expect.objectContaining({
type: "mnemonic",
words: mnemonic.split(" "),
}),
mnemonic: mnemonic.split(" "),
}),
);
});
Expand Down Expand Up @@ -171,16 +168,6 @@ describe("combineShardsBuildWallet", () => {
);
});

it("calls cardanoWallet.init()", async () => {
const mnemonic =
"abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
const shards = await spiltKeyIntoShards(mnemonic);

const result = await combineShardsBuildWallet(0, shards[0]!, shards[1]!);

expect(result.cardanoWallet.init).toHaveBeenCalled();
});

it("passes bitcoinProvider to EmbeddedWallet when provided", async () => {
const mnemonic =
"abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
Expand Down
15 changes: 7 additions & 8 deletions src/functions/key-shard/combine-shards-build-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { MeshWallet } from "@meshsdk/wallet";
import { MeshCardanoHeadlessWallet } from "@meshsdk/wallet";
import { shamirCombine } from "./shamir-secret-sharing";
import { bytesToString, hexToBytes } from "../convertors";
import { EmbeddedWallet, IBitcoinProvider } from "@meshsdk/bitcoin";
import { IFetcher } from "@meshsdk/common";
import { SparkWallet } from "@buildonspark/spark-sdk";

export async function combineShardsBuildWallet(
networkId: 0 | 1,
keyShard1: string,
keyShard2: string,
bitcoinProvider?: IBitcoinProvider,
fetcher?: IFetcher,
) {
const _share1 = hexToBytes(keyShard1);
const _share2 = hexToBytes(keyShard2);
Expand All @@ -25,16 +27,13 @@ export async function combineShardsBuildWallet(
provider: bitcoinProvider,
});

const cardanoWallet = new MeshWallet({
const cardanoWallet = await MeshCardanoHeadlessWallet.fromMnemonic({
mnemonic: mnemonic.split(" "),
networkId: networkId,
key: {
type: "mnemonic",
words: mnemonic.split(" "),
},
walletAddressType: 1,
fetcher,
});

await cardanoWallet.init();

const { wallet: sparkWallet } = await SparkWallet.initialize({
mnemonicOrSeed: mnemonic,
options: {
Expand Down
Loading