cosmos-connect-core 0.1.13 → 0.1.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -25,23 +25,28 @@ npm install cosmos-connect-core
25
25
  ## Usage
26
26
 
27
27
  ```typescript
28
- import { KeplrWallet, LeapWallet } from 'cosmos-connect-core';
29
-
30
- const keplr = new KeplrWallet();
31
- const leap = new LeapWallet();
32
-
33
- // Connect wallet
34
- await keplr.connect({
35
- chainId: 'columbus-5',
36
- rpc: 'https://terra-classic-rpc.publicnode.com',
37
- rest: 'https://terra-classic-lcd.publicnode.com',
28
+ import { createClient, KeplrWallet, LeapWallet } from 'cosmos-connect-core';
29
+
30
+ const client = createClient({
31
+ chains: [
32
+ {
33
+ chainId: 'columbus-5',
34
+ rpc: 'https://terra-classic-rpc.publicnode.com',
35
+ rest: 'https://terra-classic-lcd.publicnode.com',
36
+ bech32Prefix: 'terra',
37
+ },
38
+ ],
39
+ walletConnectProjectId: 'YOUR_PROJECT_ID', // from https://cloud.walletconnect.com
40
+ wallets: [new KeplrWallet(), new LeapWallet()],
38
41
  });
39
42
 
40
- // Get account
41
- const account = await keplr.getAccount('columbus-5');
42
- console.log(account.address); // terra1...
43
+ // Connect wallet
44
+ await client.connect('keplr', 'columbus-5');
45
+ console.log(client.state.account?.address); // terra1...
43
46
  ```
44
47
 
48
+ > **Note:** Set `walletConnectProjectId` once in the config — it will be automatically passed to all wallets that support WalletConnect.
49
+
45
50
  ## Supported Chains
46
51
 
47
52
  - Terra Classic (columbus-5)
@@ -29,6 +29,8 @@ export declare class CosmostationWallet implements WalletAdapter {
29
29
  constructor(options?: {
30
30
  projectId?: string;
31
31
  });
32
+ setProjectId(projectId: string): void;
33
+ private _initWC;
32
34
  installed(): boolean;
33
35
  getUri(): string;
34
36
  onUpdate(callback: () => void): void;
@@ -7,19 +7,27 @@ export class CosmostationWallet {
7
7
  wc;
8
8
  constructor(options) {
9
9
  if (options?.projectId) {
10
- this.wc = new WalletConnectWallet({
11
- projectId: options.projectId,
12
- id: this.id,
13
- name: this.name,
14
- icon: this.icon,
15
- mobileAppDetails: {
16
- name: 'Cosmostation',
17
- android: 'intent://wc#Intent;package=wannabit.io.cosmostaion;scheme=cosmostation;end;',
18
- ios: 'cosmostation://wc',
19
- },
20
- });
10
+ this._initWC(options.projectId);
21
11
  }
22
12
  }
13
+ setProjectId(projectId) {
14
+ if (!this.wc) {
15
+ this._initWC(projectId);
16
+ }
17
+ }
18
+ _initWC(projectId) {
19
+ this.wc = new WalletConnectWallet({
20
+ projectId,
21
+ id: this.id,
22
+ name: this.name,
23
+ icon: this.icon,
24
+ mobileAppDetails: {
25
+ name: 'Cosmostation',
26
+ android: 'intent://wc#Intent;package=wannabit.io.cosmostaion;scheme=cosmostation;end;',
27
+ ios: 'cosmostation://wc',
28
+ },
29
+ });
30
+ }
23
31
  installed() {
24
32
  return typeof window !== 'undefined' && !!window.cosmostation;
25
33
  }
@@ -24,6 +24,8 @@ export declare class GalaxyStationWallet implements WalletAdapter {
24
24
  constructor(options?: {
25
25
  projectId?: string;
26
26
  });
27
+ setProjectId(projectId: string): void;
28
+ private _initWC;
27
29
  installed(): boolean;
28
30
  getUri(): string;
29
31
  onUpdate(callback: () => void): void;
@@ -6,27 +6,35 @@ export class GalaxyStationWallet {
6
6
  wc;
7
7
  constructor(options) {
8
8
  if (options?.projectId) {
9
- this.wc = new WalletConnectWallet({
10
- projectId: options.projectId,
11
- id: this.id,
12
- name: this.name,
13
- icon: this.icon,
14
- mobileAppDetails: {
15
- name: 'Galaxy Station',
16
- android: 'https://station.hexxagon.io/wcV2#Intent;package=io.hexxagon.station;scheme=galaxystation;end;',
17
- ios: 'https://station.hexxagon.io/wcV2',
18
- },
19
- signerMetadata: {
20
- name: 'LUNCConnect',
21
- description: 'Connect to LUNCConnect',
22
- url: typeof window !== 'undefined' ? window.location.origin : '',
23
- icons: [
24
- 'https://raw.githubusercontent.com/terra-money/station-assets/main/img/station.png',
25
- ],
26
- },
27
- });
9
+ this._initWC(options.projectId);
28
10
  }
29
11
  }
12
+ setProjectId(projectId) {
13
+ if (!this.wc) {
14
+ this._initWC(projectId);
15
+ }
16
+ }
17
+ _initWC(projectId) {
18
+ this.wc = new WalletConnectWallet({
19
+ projectId,
20
+ id: this.id,
21
+ name: this.name,
22
+ icon: this.icon,
23
+ mobileAppDetails: {
24
+ name: 'Galaxy Station',
25
+ android: 'https://station.hexxagon.io/wcV2#Intent;package=io.hexxagon.station;scheme=galaxystation;end;',
26
+ ios: 'https://station.hexxagon.io/wcV2',
27
+ },
28
+ signerMetadata: {
29
+ name: 'LUNCConnect',
30
+ description: 'Connect to LUNCConnect',
31
+ url: typeof window !== 'undefined' ? window.location.origin : '',
32
+ icons: [
33
+ 'https://raw.githubusercontent.com/terra-money/station-assets/main/img/station.png',
34
+ ],
35
+ },
36
+ });
37
+ }
30
38
  installed() {
31
39
  return typeof window !== 'undefined' && !!window.galaxyStation;
32
40
  }
@@ -25,6 +25,8 @@ export declare class KeplrWallet implements WalletAdapter {
25
25
  constructor(options?: {
26
26
  projectId?: string;
27
27
  });
28
+ setProjectId(projectId: string): void;
29
+ private _initWC;
28
30
  installed(): boolean;
29
31
  getUri(): string;
30
32
  onUpdate(callback: () => void): void;
@@ -7,19 +7,27 @@ export class KeplrWallet {
7
7
  wc;
8
8
  constructor(options) {
9
9
  if (options?.projectId) {
10
- this.wc = new WalletConnectWallet({
11
- projectId: options.projectId,
12
- id: this.id,
13
- name: this.name,
14
- icon: this.icon,
15
- mobileAppDetails: {
16
- name: 'Keplr',
17
- android: 'intent://wcV2#Intent;package=com.chainapsis.keplr;scheme=keplrwallet;end;',
18
- ios: 'keplrwallet://wcV2',
19
- },
20
- });
10
+ this._initWC(options.projectId);
21
11
  }
22
12
  }
13
+ setProjectId(projectId) {
14
+ if (!this.wc) {
15
+ this._initWC(projectId);
16
+ }
17
+ }
18
+ _initWC(projectId) {
19
+ this.wc = new WalletConnectWallet({
20
+ projectId,
21
+ id: this.id,
22
+ name: this.name,
23
+ icon: this.icon,
24
+ mobileAppDetails: {
25
+ name: 'Keplr',
26
+ android: 'intent://wcV2#Intent;package=com.chainapsis.keplr;scheme=keplrwallet;end;',
27
+ ios: 'keplrwallet://wcV2',
28
+ },
29
+ });
30
+ }
23
31
  installed() {
24
32
  return typeof window !== 'undefined' && !!window.keplr;
25
33
  }
@@ -8,9 +8,10 @@ export declare class LUNCDashWallet implements WalletAdapter {
8
8
  private _updateCallback?;
9
9
  private _connectPromise;
10
10
  private _wcInstance;
11
- constructor({ projectId }: {
12
- projectId: string;
11
+ constructor(_options?: {
12
+ projectId?: string;
13
13
  });
14
+ setProjectId(_projectId: string): void;
14
15
  installed(): boolean;
15
16
  getUri(): string;
16
17
  onUpdate(callback: () => void): void;
@@ -11,7 +11,7 @@ export class LUNCDashWallet {
11
11
  _updateCallback;
12
12
  _connectPromise = null;
13
13
  _wcInstance = null;
14
- constructor({ projectId }) {
14
+ constructor(_options) {
15
15
  this.wc = new WalletConnectV1('cosmes.wallet.luncdash.wcSession', {
16
16
  name: 'LUNCDash',
17
17
  android: 'luncdash://wallet_connect',
@@ -24,6 +24,8 @@ export class LUNCDashWallet {
24
24
  this._updateCallback?.();
25
25
  });
26
26
  }
27
+ // LUNCDash uses V1, projectId not needed
28
+ setProjectId(_projectId) { }
27
29
  installed() {
28
30
  return false; // LUNCDash is mobile-only
29
31
  }
@@ -23,6 +23,8 @@ export declare class LeapWallet implements WalletAdapter {
23
23
  constructor(options?: {
24
24
  projectId?: string;
25
25
  });
26
+ setProjectId(projectId: string): void;
27
+ private _initWC;
26
28
  installed(): boolean;
27
29
  getUri(): string;
28
30
  onUpdate(callback: () => void): void;
@@ -7,19 +7,27 @@ export class LeapWallet {
7
7
  wc;
8
8
  constructor(options) {
9
9
  if (options?.projectId) {
10
- this.wc = new WalletConnectWallet({
11
- projectId: options.projectId,
12
- id: this.id,
13
- name: this.name,
14
- icon: this.icon,
15
- mobileAppDetails: {
16
- name: 'Leap',
17
- android: 'leapcosmos://wcV2',
18
- ios: 'leapcosmos://wcV2',
19
- },
20
- });
10
+ this._initWC(options.projectId);
21
11
  }
22
12
  }
13
+ setProjectId(projectId) {
14
+ if (!this.wc) {
15
+ this._initWC(projectId);
16
+ }
17
+ }
18
+ _initWC(projectId) {
19
+ this.wc = new WalletConnectWallet({
20
+ projectId,
21
+ id: this.id,
22
+ name: this.name,
23
+ icon: this.icon,
24
+ mobileAppDetails: {
25
+ name: 'Leap',
26
+ android: 'leapcosmos://wcV2',
27
+ ios: 'leapcosmos://wcV2',
28
+ },
29
+ });
30
+ }
23
31
  installed() {
24
32
  return typeof window !== 'undefined' && !!window.leap;
25
33
  }
@@ -24,6 +24,8 @@ export declare class StationWallet implements WalletAdapter {
24
24
  constructor(options?: {
25
25
  projectId?: string;
26
26
  });
27
+ setProjectId(projectId: string): void;
28
+ private _initWC;
27
29
  installed(): boolean;
28
30
  getUri(): string;
29
31
  onUpdate(callback: () => void): void;
@@ -6,28 +6,36 @@ export class StationWallet {
6
6
  wc;
7
7
  constructor(options) {
8
8
  if (options?.projectId) {
9
- this.wc = new WalletConnectWallet({
10
- projectId: options.projectId,
11
- id: this.id,
12
- name: this.name,
13
- icon: this.icon,
14
- mobileAppDetails: {
15
- name: 'Station Wallet',
16
- android: 'terrastation://wc',
17
- ios: 'terrastation://wc',
18
- isStation: true,
19
- },
20
- signerMetadata: {
21
- name: 'LUNCConnect',
22
- description: 'Connect to LUNCConnect',
23
- url: typeof window !== 'undefined' ? window.location.origin : '',
24
- icons: [
25
- 'https://raw.githubusercontent.com/terra-money/station-assets/main/img/station.png',
26
- ],
27
- },
28
- });
9
+ this._initWC(options.projectId);
29
10
  }
30
11
  }
12
+ setProjectId(projectId) {
13
+ if (!this.wc) {
14
+ this._initWC(projectId);
15
+ }
16
+ }
17
+ _initWC(projectId) {
18
+ this.wc = new WalletConnectWallet({
19
+ projectId,
20
+ id: this.id,
21
+ name: this.name,
22
+ icon: this.icon,
23
+ mobileAppDetails: {
24
+ name: 'Station Wallet',
25
+ android: 'terrastation://wc',
26
+ ios: 'terrastation://wc',
27
+ isStation: true,
28
+ },
29
+ signerMetadata: {
30
+ name: 'LUNCConnect',
31
+ description: 'Connect to LUNCConnect',
32
+ url: typeof window !== 'undefined' ? window.location.origin : '',
33
+ icons: [
34
+ 'https://raw.githubusercontent.com/terra-money/station-assets/main/img/station.png',
35
+ ],
36
+ },
37
+ });
38
+ }
31
39
  installed() {
32
40
  // Note: Both Station and Galaxy Station might use window.station.
33
41
  // Usually one is installed at a time.
@@ -17,6 +17,7 @@ export declare class WalletConnectWallet implements WalletAdapter {
17
17
  mobileAppDetails?: MobileAppDetails;
18
18
  signerMetadata?: SignerMetadata;
19
19
  });
20
+ setProjectId(_projectId: string): void;
20
21
  installed(): boolean;
21
22
  getUri(): string;
22
23
  private _connectPromise;
@@ -34,6 +34,8 @@ export class WalletConnectWallet {
34
34
  this._updateCallback?.();
35
35
  });
36
36
  }
37
+ // projectId is set at construction for WalletConnectWallet
38
+ setProjectId(_projectId) { }
37
39
  installed() {
38
40
  return false;
39
41
  }
@@ -1,2 +1,2 @@
1
- import { Client, ClientConfig } from "./types.js";
1
+ import { Client, ClientConfig } from './types.js';
2
2
  export declare function createClient(config: ClientConfig): Client;
@@ -1,14 +1,22 @@
1
- import { defaultStorage } from "./storage.js";
2
- const STORAGE_KEY_CHAIN = "cosmos-connect.chain";
3
- const STORAGE_KEY_WALLET = "cosmos-connect.wallet";
1
+ import { defaultStorage } from './storage.js';
2
+ const STORAGE_KEY_CHAIN = 'cosmos-connect.chain';
3
+ const STORAGE_KEY_WALLET = 'cosmos-connect.wallet';
4
4
  export function createClient(config) {
5
5
  const { chains, wallets } = config;
6
6
  const storage = config.storage || defaultStorage;
7
+ // Propagate global walletConnectProjectId to all wallets
8
+ if (config.walletConnectProjectId) {
9
+ wallets.forEach((w) => {
10
+ if (w.setProjectId) {
11
+ w.setProjectId(config.walletConnectProjectId);
12
+ }
13
+ });
14
+ }
7
15
  let state = {
8
16
  currentChain: null,
9
17
  currentWallet: null,
10
18
  account: null,
11
- status: "disconnected",
19
+ status: 'disconnected',
12
20
  };
13
21
  const listeners = new Set();
14
22
  function setState(partial) {
@@ -29,7 +37,7 @@ export function createClient(config) {
29
37
  const wallet = getWallet(walletId);
30
38
  if (!wallet)
31
39
  throw new Error(`Wallet ${walletId} not found`);
32
- setState({ status: "connecting" });
40
+ setState({ status: 'connecting' });
33
41
  if (!wallet.installed() && !wallet.getUri) {
34
42
  throw new Error(`Wallet ${wallet.name} is not installed`);
35
43
  }
@@ -38,13 +46,13 @@ export function createClient(config) {
38
46
  currentChain: chain,
39
47
  currentWallet: wallet,
40
48
  account,
41
- status: "connected",
49
+ status: 'connected',
42
50
  });
43
51
  storage.setItem(STORAGE_KEY_CHAIN, chainId);
44
52
  storage.setItem(STORAGE_KEY_WALLET, walletId);
45
53
  }
46
54
  catch (error) {
47
- setState({ status: "disconnected", account: null });
55
+ setState({ status: 'disconnected', account: null });
48
56
  throw error;
49
57
  }
50
58
  }
@@ -54,21 +62,21 @@ export function createClient(config) {
54
62
  await state.currentWallet.disconnect();
55
63
  }
56
64
  catch (e) {
57
- console.error("Wallet disconnect failed", e);
65
+ console.error('Wallet disconnect failed', e);
58
66
  }
59
67
  }
60
68
  setState({
61
69
  currentChain: null,
62
70
  currentWallet: null,
63
71
  account: null,
64
- status: "disconnected",
72
+ status: 'disconnected',
65
73
  });
66
74
  storage.removeItem(STORAGE_KEY_CHAIN);
67
75
  storage.removeItem(STORAGE_KEY_WALLET);
68
76
  }
69
77
  async function signAndBroadcast(txBytes) {
70
78
  if (!state.currentChain || !state.currentWallet || !state.account) {
71
- throw new Error("Client not connected");
79
+ throw new Error('Client not connected');
72
80
  }
73
81
  // 1. Sign
74
82
  const signedTxBytes = await state.currentWallet.signTx(txBytes);
@@ -122,7 +130,7 @@ export function createClient(config) {
122
130
  }
123
131
  // Browser-compatible base64 encoding
124
132
  function toBase64(bytes) {
125
- let binary = "";
133
+ let binary = '';
126
134
  const len = bytes.byteLength;
127
135
  for (let i = 0; i < len; i++) {
128
136
  binary += String.fromCharCode(bytes[i]);
@@ -132,18 +140,18 @@ function toBase64(bytes) {
132
140
  async function broadcastTx(rpc, signedTx) {
133
141
  const txBytesBase64 = toBase64(signedTx);
134
142
  const body = {
135
- jsonrpc: "2.0",
136
- id: "1",
137
- method: "broadcast_tx_sync",
143
+ jsonrpc: '2.0',
144
+ id: '1',
145
+ method: 'broadcast_tx_sync',
138
146
  params: {
139
147
  tx: txBytesBase64,
140
148
  },
141
149
  };
142
- if (typeof fetch === "undefined") {
143
- throw new Error("Fetch is not defined in this environment");
150
+ if (typeof fetch === 'undefined') {
151
+ throw new Error('Fetch is not defined in this environment');
144
152
  }
145
153
  const res = await fetch(rpc, {
146
- method: "POST",
154
+ method: 'POST',
147
155
  body: JSON.stringify(body),
148
156
  });
149
157
  if (!res.ok) {
@@ -24,6 +24,7 @@ export interface WalletAdapter {
24
24
  signMsg?(msg: string): Promise<Uint8Array>;
25
25
  getUri?(): string;
26
26
  onUpdate?(callback: () => void): void;
27
+ setProjectId?(projectId: string): void;
27
28
  }
28
29
  export type ClientStatus = 'disconnected' | 'connecting' | 'connected';
29
30
  export interface ClientState {
@@ -35,6 +36,7 @@ export interface ClientState {
35
36
  export interface ClientConfig {
36
37
  chains: Chain[];
37
38
  wallets: WalletAdapter[];
39
+ walletConnectProjectId?: string;
38
40
  storage?: StorageAdapter;
39
41
  }
40
42
  export interface StorageAdapter {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cosmos-connect-core",
3
- "version": "0.1.13",
3
+ "version": "0.1.14",
4
4
  "description": "Core SDK for Cosmos Connect",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",