@cartridge/controller
Advanced tools
Comparing version 0.5.7 to 0.5.8
import { WalletAccount, AllowArray, Call, InvokeFunctionResponse, TypedData } from 'starknet'; | ||
import { SPEC } from '@starknet-io/types-js'; | ||
import { K as Keychain, j as KeychainOptions, M as Modal } from './types-BReKRAuh.js'; | ||
import { K as Keychain, k as KeychainOptions, M as Modal } from './types-CVnDQVqD.js'; | ||
import { AsyncMethodReturns } from '@cartridge/penpal'; | ||
@@ -13,3 +13,3 @@ import BaseProvider from './provider.js'; | ||
private options?; | ||
constructor(provider: BaseProvider, address: string, keychain: AsyncMethodReturns<Keychain>, options: KeychainOptions, modal: Modal); | ||
constructor(provider: BaseProvider, rpcUrl: string, address: string, keychain: AsyncMethodReturns<Keychain>, options: KeychainOptions, modal: Modal); | ||
/** | ||
@@ -16,0 +16,0 @@ * Invoke execute function in account contract |
@@ -21,4 +21,4 @@ // src/account.ts | ||
var ControllerAccount = class extends WalletAccount { | ||
constructor(provider, address, keychain, options, modal) { | ||
super({ nodeUrl: provider.rpc.toString() }, provider); | ||
constructor(provider, rpcUrl, address, keychain, options, modal) { | ||
super({ nodeUrl: rpcUrl }, provider); | ||
this.address = address; | ||
@@ -25,0 +25,0 @@ this.keychain = keychain; |
@@ -1,6 +0,6 @@ | ||
import { g as ControllerOptions, l as ProfileContextTypeVariant } from './types-BReKRAuh.js'; | ||
import { g as ControllerOptions, m as ProfileContextTypeVariant } from './types-CVnDQVqD.js'; | ||
import BaseProvider from './provider.js'; | ||
import { WalletAccount } from 'starknet'; | ||
import { i as Policy } from './index.d-BbTUPBeO.js'; | ||
import '@starknet-io/types-js'; | ||
import { AddStarknetChainParameters } from '@starknet-io/types-js'; | ||
import '@cartridge/penpal'; | ||
@@ -13,5 +13,9 @@ | ||
private iframes; | ||
private selectedChain; | ||
private chains; | ||
constructor(options: ControllerOptions); | ||
probe(): Promise<WalletAccount | undefined>; | ||
connect(): Promise<WalletAccount | undefined>; | ||
switchStarknetChain(chainId: string): Promise<boolean>; | ||
addStarknetChain(_chain: AddStarknetChainParameters): Promise<boolean>; | ||
disconnect(): Promise<void>; | ||
@@ -21,2 +25,3 @@ openProfile(tab?: ProfileContextTypeVariant): Promise<void>; | ||
revoke(origin: string, _policy: Policy[]): Promise<void> | null; | ||
rpcUrl(): string; | ||
username(): Promise<string> | undefined; | ||
@@ -23,0 +28,0 @@ fetchControllers(contractAddresses: string[]): Promise<Record<string, string>>; |
@@ -21,4 +21,4 @@ // src/account.ts | ||
var ControllerAccount = class extends WalletAccount { | ||
constructor(provider, address, keychain, options, modal) { | ||
super({ nodeUrl: provider.rpc.toString() }, provider); | ||
constructor(provider, rpcUrl, address, keychain, options, modal) { | ||
super({ nodeUrl: rpcUrl }, provider); | ||
this.address = address; | ||
@@ -298,3 +298,3 @@ this.keychain = keychain; | ||
name: "@cartridge/controller", | ||
version: "0.5.7", | ||
version: "0.5.8", | ||
description: "Cartridge Controller", | ||
@@ -351,3 +351,3 @@ module: "dist/index.js", | ||
var BaseProvider = class { | ||
constructor(options) { | ||
constructor() { | ||
this.id = "controller"; | ||
@@ -370,4 +370,5 @@ this.name = "Controller"; | ||
} | ||
const silentMode = call.params && call.params.silent_mode; | ||
this.account = await this.probe(); | ||
if (!this.account) { | ||
if (!this.account && !silentMode) { | ||
this.account = await this.connect(); | ||
@@ -386,14 +387,10 @@ } | ||
}; | ||
case "wallet_addStarknetChain": | ||
throw { | ||
code: 63, | ||
message: "An unexpected error occurred", | ||
data: "wallet_addStarknetChain not implemented" | ||
}; | ||
case "wallet_switchStarknetChain": | ||
throw { | ||
code: 63, | ||
message: "An unexpected error occurred", | ||
data: "wallet_switchStarknetChain not implemented" | ||
}; | ||
case "wallet_addStarknetChain": { | ||
let params2 = call.params; | ||
return this.addStarknetChain(params2); | ||
} | ||
case "wallet_switchStarknetChain": { | ||
let params2 = call.params; | ||
return this.switchStarknetChain(params2.chainId); | ||
} | ||
case "wallet_requestChainId": | ||
@@ -404,3 +401,3 @@ if (!this.account) { | ||
message: "An unexpected error occurred", | ||
data: "wallet_deploymentData not implemented" | ||
data: "Account not initialized" | ||
}; | ||
@@ -420,3 +417,3 @@ } | ||
message: "An unexpected error occurred", | ||
data: "wallet_deploymentData not implemented" | ||
data: "Account not initialized" | ||
}; | ||
@@ -477,12 +474,56 @@ } | ||
}; | ||
const { rpc } = options; | ||
this.rpc = new URL(rpc); | ||
} | ||
emitNetworkChanged(chainId) { | ||
this.subscriptions.filter((sub) => sub.type === "networkChanged").forEach((sub) => { | ||
sub.handler(chainId); | ||
}); | ||
} | ||
emitAccountsChanged(accounts) { | ||
this.subscriptions.filter((sub) => sub.type === "accountsChanged").forEach((sub) => { | ||
sub.handler(accounts); | ||
}); | ||
} | ||
}; | ||
// src/controller.ts | ||
import { constants } from "starknet"; | ||
var ControllerProvider = class extends BaseProvider { | ||
constructor(options) { | ||
const { rpc } = options; | ||
super({ rpc }); | ||
super(); | ||
const chains = /* @__PURE__ */ new Map(); | ||
for (const chain of options.chains) { | ||
let chainId; | ||
const url = new URL(chain.rpcUrl); | ||
const parts = url.pathname.split("/"); | ||
if (parts.includes("starknet")) { | ||
if (parts.includes("mainnet")) { | ||
chainId = constants.StarknetChainId.SN_MAIN; | ||
} else if (parts.includes("sepolia")) { | ||
chainId = constants.StarknetChainId.SN_SEPOLIA; | ||
} | ||
} else if (parts.length >= 3) { | ||
const projectName = parts[2]; | ||
if (parts.includes("katana")) { | ||
chainId = `WP_${projectName.toUpperCase()}`; | ||
} else if (parts.includes("mainnet")) { | ||
chainId = `GG_${projectName.toUpperCase()}`; | ||
} | ||
} | ||
if (!chainId) { | ||
throw new Error(`Chain ${chain.rpcUrl} not supported`); | ||
} | ||
chains.set(chainId, chain); | ||
} | ||
if (options.policies?.messages?.length && options.policies.messages.length !== chains.size) { | ||
console.warn( | ||
"Each message policy is associated with a specific chain. The number of message policies does not match the number of chains specified - session message signing may not work on some chains." | ||
); | ||
} | ||
this.chains = chains; | ||
this.selectedChain = options.defaultChainId; | ||
if (!this.chains.has(this.selectedChain)) { | ||
throw new Error( | ||
`Chain ${this.selectedChain} not found in configured chains` | ||
); | ||
} | ||
this.iframes = { | ||
@@ -509,7 +550,6 @@ keychain: new KeychainIFrame({ | ||
} | ||
const response = await this.keychain.probe( | ||
this.rpc.toString() | ||
); | ||
const response = await this.keychain.probe(this.rpcUrl()); | ||
this.account = new account_default( | ||
this, | ||
this.rpcUrl(), | ||
response.address, | ||
@@ -536,3 +576,3 @@ this.keychain, | ||
}, | ||
rpcUrl: this.rpc.toString(), | ||
rpcUrl: this.rpcUrl(), | ||
username, | ||
@@ -561,4 +601,4 @@ version: this.version | ||
let response = await this.keychain.connect( | ||
this.options.policies || [], | ||
this.rpc.toString() | ||
this.options.policies || {}, | ||
this.rpcUrl() | ||
); | ||
@@ -571,2 +611,3 @@ if (response.code !== "SUCCESS" /* SUCCESS */) { | ||
this, | ||
this.rpcUrl(), | ||
response.address, | ||
@@ -584,2 +625,19 @@ this.keychain, | ||
} | ||
async switchStarknetChain(chainId) { | ||
try { | ||
this.selectedChain = chainId; | ||
this.account = await this.probe(); | ||
if (!this.account) { | ||
this.account = await this.connect(); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
return false; | ||
} | ||
this.emitNetworkChanged(chainId); | ||
return true; | ||
} | ||
addStarknetChain(_chain) { | ||
return Promise.resolve(true); | ||
} | ||
async disconnect() { | ||
@@ -637,2 +695,5 @@ if (!this.keychain) { | ||
} | ||
rpcUrl() { | ||
return this.chains.get(this.selectedChain).rpcUrl; | ||
} | ||
username() { | ||
@@ -639,0 +700,0 @@ if (!this.keychain) { |
import '@cartridge/penpal'; | ||
export { o as IFrame, n as IFrameOptions } from '../types-BReKRAuh.js'; | ||
export { o as IFrame, n as IFrameOptions } from '../types-CVnDQVqD.js'; | ||
import 'starknet'; | ||
import '@starknet-io/types-js'; | ||
import '../index.d-BbTUPBeO.js'; |
@@ -1,2 +0,2 @@ | ||
export { o as IFrame, n as IFrameOptions, p as KeychainIFrame, r as ProfileIFrame, q as ProfileIFrameOptions } from '../types-BReKRAuh.js'; | ||
export { o as IFrame, n as IFrameOptions, p as KeychainIFrame, r as ProfileIFrame, q as ProfileIFrameOptions } from '../types-CVnDQVqD.js'; | ||
import 'starknet'; | ||
@@ -3,0 +3,0 @@ import '@starknet-io/types-js'; |
@@ -1,2 +0,2 @@ | ||
export { p as KeychainIFrame } from '../types-BReKRAuh.js'; | ||
export { p as KeychainIFrame } from '../types-CVnDQVqD.js'; | ||
import 'starknet'; | ||
@@ -3,0 +3,0 @@ import '@starknet-io/types-js'; |
@@ -1,2 +0,2 @@ | ||
export { r as ProfileIFrame, q as ProfileIFrameOptions } from '../types-BReKRAuh.js'; | ||
export { r as ProfileIFrame, q as ProfileIFrameOptions } from '../types-CVnDQVqD.js'; | ||
import 'starknet'; | ||
@@ -3,0 +3,0 @@ import '@starknet-io/types-js'; |
export { default } from './controller.js'; | ||
export { NotReadyToConnect } from './errors.js'; | ||
export { C as ConnectError, b as ConnectReply, e as ControllerAccounts, a as ControllerError, g as ControllerOptions, D as DeployReply, E as ExecuteReply, h as IFrameOptions, I as IFrames, K as Keychain, j as KeychainOptions, L as LookupRequest, d as LookupResponse, c as LookupResult, M as Modal, m as Prefund, P as ProbeReply, f as Profile, l as ProfileContextTypeVariant, k as ProfileOptions, i as ProviderOptions, R as ResponseCodes, S as Session, T as Tokens } from './types-BReKRAuh.js'; | ||
export { i as Chain, C as ConnectError, b as ConnectReply, e as ControllerAccounts, a as ControllerError, g as ControllerOptions, D as DeployReply, E as ExecuteReply, h as IFrameOptions, I as IFrames, K as Keychain, k as KeychainOptions, L as LookupRequest, d as LookupResponse, c as LookupResult, M as Modal, P as ProbeReply, f as Profile, m as ProfileContextTypeVariant, l as ProfileOptions, j as ProviderOptions, R as ResponseCodes, S as Session, T as Tokens } from './types-CVnDQVqD.js'; | ||
export { lookupAddresses, lookupUsernames } from './lookup.js'; | ||
@@ -5,0 +5,0 @@ export { toArray, toSessionPolicies, toWasmPolicies } from './utils.js'; |
import { WalletAccount } from 'starknet'; | ||
import { StarknetWindowObject, WalletEvents, RequestFn, WalletEventListener } from '@starknet-io/types-js'; | ||
import { i as ProviderOptions } from './types-BReKRAuh.js'; | ||
import '@cartridge/penpal'; | ||
import './index.d-BbTUPBeO.js'; | ||
import { StarknetWindowObject, WalletEvents, RequestFn, WalletEventListener, AddStarknetChainParameters } from '@starknet-io/types-js'; | ||
@@ -12,13 +9,15 @@ declare abstract class BaseProvider implements StarknetWindowObject { | ||
icon: string; | ||
rpc: URL; | ||
account?: WalletAccount; | ||
subscriptions: WalletEvents[]; | ||
constructor(options: ProviderOptions); | ||
request: RequestFn; | ||
on: WalletEventListener; | ||
off: WalletEventListener; | ||
protected emitNetworkChanged(chainId: string): void; | ||
protected emitAccountsChanged(accounts: string[]): void; | ||
abstract probe(): Promise<WalletAccount | undefined>; | ||
abstract connect(): Promise<WalletAccount | undefined>; | ||
abstract switchStarknetChain(chainId: string): Promise<boolean>; | ||
abstract addStarknetChain(chain: AddStarknetChainParameters): Promise<boolean>; | ||
} | ||
export { BaseProvider as default }; |
@@ -9,3 +9,3 @@ // src/provider.ts | ||
name: "@cartridge/controller", | ||
version: "0.5.7", | ||
version: "0.5.8", | ||
description: "Cartridge Controller", | ||
@@ -62,3 +62,3 @@ module: "dist/index.js", | ||
var BaseProvider = class { | ||
constructor(options) { | ||
constructor() { | ||
this.id = "controller"; | ||
@@ -81,4 +81,5 @@ this.name = "Controller"; | ||
} | ||
const silentMode = call.params && call.params.silent_mode; | ||
this.account = await this.probe(); | ||
if (!this.account) { | ||
if (!this.account && !silentMode) { | ||
this.account = await this.connect(); | ||
@@ -97,14 +98,10 @@ } | ||
}; | ||
case "wallet_addStarknetChain": | ||
throw { | ||
code: 63, | ||
message: "An unexpected error occurred", | ||
data: "wallet_addStarknetChain not implemented" | ||
}; | ||
case "wallet_switchStarknetChain": | ||
throw { | ||
code: 63, | ||
message: "An unexpected error occurred", | ||
data: "wallet_switchStarknetChain not implemented" | ||
}; | ||
case "wallet_addStarknetChain": { | ||
let params2 = call.params; | ||
return this.addStarknetChain(params2); | ||
} | ||
case "wallet_switchStarknetChain": { | ||
let params2 = call.params; | ||
return this.switchStarknetChain(params2.chainId); | ||
} | ||
case "wallet_requestChainId": | ||
@@ -115,3 +112,3 @@ if (!this.account) { | ||
message: "An unexpected error occurred", | ||
data: "wallet_deploymentData not implemented" | ||
data: "Account not initialized" | ||
}; | ||
@@ -131,3 +128,3 @@ } | ||
message: "An unexpected error occurred", | ||
data: "wallet_deploymentData not implemented" | ||
data: "Account not initialized" | ||
}; | ||
@@ -188,5 +185,13 @@ } | ||
}; | ||
const { rpc } = options; | ||
this.rpc = new URL(rpc); | ||
} | ||
emitNetworkChanged(chainId) { | ||
this.subscriptions.filter((sub) => sub.type === "networkChanged").forEach((sub) => { | ||
sub.handler(chainId); | ||
}); | ||
} | ||
emitAccountsChanged(accounts) { | ||
this.subscriptions.filter((sub) => sub.type === "accountsChanged").forEach((sub) => { | ||
sub.handler(accounts); | ||
}); | ||
} | ||
}; | ||
@@ -193,0 +198,0 @@ export { |
@@ -6,3 +6,3 @@ import { Policy } from '@cartridge/account-wasm'; | ||
export { NotReadyToConnect } from '../errors.js'; | ||
export { C as ConnectError, b as ConnectReply, e as ControllerAccounts, a as ControllerError, g as ControllerOptions, D as DeployReply, E as ExecuteReply, h as IFrameOptions, I as IFrames, K as Keychain, j as KeychainOptions, L as LookupRequest, d as LookupResponse, c as LookupResult, M as Modal, m as Prefund, P as ProbeReply, f as Profile, l as ProfileContextTypeVariant, k as ProfileOptions, i as ProviderOptions, R as ResponseCodes, S as Session, T as Tokens } from '../types-BReKRAuh.js'; | ||
export { i as Chain, C as ConnectError, b as ConnectReply, e as ControllerAccounts, a as ControllerError, g as ControllerOptions, D as DeployReply, E as ExecuteReply, h as IFrameOptions, I as IFrames, K as Keychain, k as KeychainOptions, L as LookupRequest, d as LookupResponse, c as LookupResult, M as Modal, P as ProbeReply, f as Profile, m as ProfileContextTypeVariant, l as ProfileOptions, j as ProviderOptions, R as ResponseCodes, S as Session, T as Tokens } from '../types-CVnDQVqD.js'; | ||
import '@starknet-io/types-js'; | ||
@@ -9,0 +9,0 @@ import '@cartridge/penpal'; |
export { SessionOptions, default } from './provider.js'; | ||
export { NotReadyToConnect } from '../errors.js'; | ||
export { C as ConnectError, b as ConnectReply, e as ControllerAccounts, a as ControllerError, g as ControllerOptions, D as DeployReply, E as ExecuteReply, h as IFrameOptions, I as IFrames, K as Keychain, j as KeychainOptions, L as LookupRequest, d as LookupResponse, c as LookupResult, M as Modal, m as Prefund, P as ProbeReply, f as Profile, l as ProfileContextTypeVariant, k as ProfileOptions, i as ProviderOptions, R as ResponseCodes, S as Session, T as Tokens } from '../types-BReKRAuh.js'; | ||
export { i as Chain, C as ConnectError, b as ConnectReply, e as ControllerAccounts, a as ControllerError, g as ControllerOptions, D as DeployReply, E as ExecuteReply, h as IFrameOptions, I as IFrames, K as Keychain, k as KeychainOptions, L as LookupRequest, d as LookupResponse, c as LookupResult, M as Modal, P as ProbeReply, f as Profile, m as ProfileContextTypeVariant, l as ProfileOptions, j as ProviderOptions, R as ResponseCodes, S as Session, T as Tokens } from '../types-CVnDQVqD.js'; | ||
import 'starknet'; | ||
@@ -5,0 +5,0 @@ import '../provider.js'; |
@@ -126,3 +126,3 @@ // src/session/provider.ts | ||
name: "@cartridge/controller", | ||
version: "0.5.7", | ||
version: "0.5.8", | ||
description: "Cartridge Controller", | ||
@@ -179,3 +179,3 @@ module: "dist/index.js", | ||
var BaseProvider = class { | ||
constructor(options) { | ||
constructor() { | ||
this.id = "controller"; | ||
@@ -198,4 +198,5 @@ this.name = "Controller"; | ||
} | ||
const silentMode = call.params && call.params.silent_mode; | ||
this.account = await this.probe(); | ||
if (!this.account) { | ||
if (!this.account && !silentMode) { | ||
this.account = await this.connect(); | ||
@@ -214,14 +215,10 @@ } | ||
}; | ||
case "wallet_addStarknetChain": | ||
throw { | ||
code: 63, | ||
message: "An unexpected error occurred", | ||
data: "wallet_addStarknetChain not implemented" | ||
}; | ||
case "wallet_switchStarknetChain": | ||
throw { | ||
code: 63, | ||
message: "An unexpected error occurred", | ||
data: "wallet_switchStarknetChain not implemented" | ||
}; | ||
case "wallet_addStarknetChain": { | ||
let params2 = call.params; | ||
return this.addStarknetChain(params2); | ||
} | ||
case "wallet_switchStarknetChain": { | ||
let params2 = call.params; | ||
return this.switchStarknetChain(params2.chainId); | ||
} | ||
case "wallet_requestChainId": | ||
@@ -232,3 +229,3 @@ if (!this.account) { | ||
message: "An unexpected error occurred", | ||
data: "wallet_deploymentData not implemented" | ||
data: "Account not initialized" | ||
}; | ||
@@ -248,3 +245,3 @@ } | ||
message: "An unexpected error occurred", | ||
data: "wallet_deploymentData not implemented" | ||
data: "Account not initialized" | ||
}; | ||
@@ -305,5 +302,13 @@ } | ||
}; | ||
const { rpc } = options; | ||
this.rpc = new URL(rpc); | ||
} | ||
emitNetworkChanged(chainId) { | ||
this.subscriptions.filter((sub) => sub.type === "networkChanged").forEach((sub) => { | ||
sub.handler(chainId); | ||
}); | ||
} | ||
emitAccountsChanged(accounts) { | ||
this.subscriptions.filter((sub) => sub.type === "accountsChanged").forEach((sub) => { | ||
sub.handler(accounts); | ||
}); | ||
} | ||
}; | ||
@@ -314,5 +319,6 @@ | ||
constructor({ rpc, chainId, policies, redirectUrl }) { | ||
super({ rpc }); | ||
super(); | ||
this.id = "controller_session"; | ||
this.name = "Controller Session"; | ||
this._rpcUrl = rpc; | ||
this._chainId = chainId; | ||
@@ -349,3 +355,3 @@ this._redirectUrl = redirectUrl; | ||
this._policies | ||
)}&rpc_url=${this.rpc}`; | ||
)}&rpc_url=${this._rpcUrl}`; | ||
localStorage.setItem("lastUsedConnector", this.id); | ||
@@ -355,2 +361,8 @@ window.open(url, "_blank"); | ||
} | ||
switchStarknetChain(_chainId) { | ||
throw new Error("switchStarknetChain not implemented"); | ||
} | ||
addStarknetChain(_chain) { | ||
throw new Error("addStarknetChain not implemented"); | ||
} | ||
disconnect() { | ||
@@ -389,3 +401,3 @@ localStorage.removeItem("sessionSigner"); | ||
this.account = new SessionAccount(this, { | ||
rpcUrl: this.rpc.toString(), | ||
rpcUrl: this._rpcUrl, | ||
privateKey: signer.privKey, | ||
@@ -392,0 +404,0 @@ address: sessionRegistration.address, |
import { WalletAccount } from 'starknet'; | ||
import BaseProvider from '../provider.js'; | ||
import { S as SessionPolicies } from '../index.d-BbTUPBeO.js'; | ||
import '@starknet-io/types-js'; | ||
import '../types-BReKRAuh.js'; | ||
import '@cartridge/penpal'; | ||
import { AddStarknetChainParameters } from '@starknet-io/types-js'; | ||
@@ -18,2 +16,3 @@ type SessionOptions = { | ||
protected _chainId: string; | ||
protected _rpcUrl: string; | ||
protected _username?: string; | ||
@@ -26,2 +25,4 @@ protected _redirectUrl: string; | ||
connect(): Promise<WalletAccount | undefined>; | ||
switchStarknetChain(_chainId: string): Promise<boolean>; | ||
addStarknetChain(_chain: AddStarknetChainParameters): Promise<boolean>; | ||
disconnect(): Promise<void>; | ||
@@ -28,0 +29,0 @@ tryRetrieveFromQueryOrStorage(): Promise<WalletAccount | undefined>; |
@@ -108,3 +108,3 @@ // src/session/provider.ts | ||
name: "@cartridge/controller", | ||
version: "0.5.7", | ||
version: "0.5.8", | ||
description: "Cartridge Controller", | ||
@@ -161,3 +161,3 @@ module: "dist/index.js", | ||
var BaseProvider = class { | ||
constructor(options) { | ||
constructor() { | ||
this.id = "controller"; | ||
@@ -180,4 +180,5 @@ this.name = "Controller"; | ||
} | ||
const silentMode = call.params && call.params.silent_mode; | ||
this.account = await this.probe(); | ||
if (!this.account) { | ||
if (!this.account && !silentMode) { | ||
this.account = await this.connect(); | ||
@@ -196,14 +197,10 @@ } | ||
}; | ||
case "wallet_addStarknetChain": | ||
throw { | ||
code: 63, | ||
message: "An unexpected error occurred", | ||
data: "wallet_addStarknetChain not implemented" | ||
}; | ||
case "wallet_switchStarknetChain": | ||
throw { | ||
code: 63, | ||
message: "An unexpected error occurred", | ||
data: "wallet_switchStarknetChain not implemented" | ||
}; | ||
case "wallet_addStarknetChain": { | ||
let params2 = call.params; | ||
return this.addStarknetChain(params2); | ||
} | ||
case "wallet_switchStarknetChain": { | ||
let params2 = call.params; | ||
return this.switchStarknetChain(params2.chainId); | ||
} | ||
case "wallet_requestChainId": | ||
@@ -214,3 +211,3 @@ if (!this.account) { | ||
message: "An unexpected error occurred", | ||
data: "wallet_deploymentData not implemented" | ||
data: "Account not initialized" | ||
}; | ||
@@ -230,3 +227,3 @@ } | ||
message: "An unexpected error occurred", | ||
data: "wallet_deploymentData not implemented" | ||
data: "Account not initialized" | ||
}; | ||
@@ -287,5 +284,13 @@ } | ||
}; | ||
const { rpc } = options; | ||
this.rpc = new URL(rpc); | ||
} | ||
emitNetworkChanged(chainId) { | ||
this.subscriptions.filter((sub) => sub.type === "networkChanged").forEach((sub) => { | ||
sub.handler(chainId); | ||
}); | ||
} | ||
emitAccountsChanged(accounts) { | ||
this.subscriptions.filter((sub) => sub.type === "accountsChanged").forEach((sub) => { | ||
sub.handler(accounts); | ||
}); | ||
} | ||
}; | ||
@@ -296,5 +301,6 @@ | ||
constructor({ rpc, chainId, policies, redirectUrl }) { | ||
super({ rpc }); | ||
super(); | ||
this.id = "controller_session"; | ||
this.name = "Controller Session"; | ||
this._rpcUrl = rpc; | ||
this._chainId = chainId; | ||
@@ -331,3 +337,3 @@ this._redirectUrl = redirectUrl; | ||
this._policies | ||
)}&rpc_url=${this.rpc}`; | ||
)}&rpc_url=${this._rpcUrl}`; | ||
localStorage.setItem("lastUsedConnector", this.id); | ||
@@ -337,2 +343,8 @@ window.open(url, "_blank"); | ||
} | ||
switchStarknetChain(_chainId) { | ||
throw new Error("switchStarknetChain not implemented"); | ||
} | ||
addStarknetChain(_chain) { | ||
throw new Error("addStarknetChain not implemented"); | ||
} | ||
disconnect() { | ||
@@ -371,3 +383,3 @@ localStorage.removeItem("sessionSigner"); | ||
this.account = new SessionAccount(this, { | ||
rpcUrl: this.rpc.toString(), | ||
rpcUrl: this._rpcUrl, | ||
privateKey: signer.privKey, | ||
@@ -374,0 +386,0 @@ address: sessionRegistration.address, |
import { WalletAccount } from 'starknet'; | ||
import BaseProvider from '../provider.js'; | ||
import { S as SessionPolicies } from '../index.d-BbTUPBeO.js'; | ||
import '@starknet-io/types-js'; | ||
import '../types-BReKRAuh.js'; | ||
import '@cartridge/penpal'; | ||
import { AddStarknetChainParameters } from '@starknet-io/types-js'; | ||
@@ -13,2 +11,3 @@ declare class TelegramProvider extends BaseProvider { | ||
protected _policies: SessionPolicies; | ||
private _rpcUrl; | ||
constructor({ rpc, chainId, policies, tmaUrl, }: { | ||
@@ -22,2 +21,4 @@ rpc: string; | ||
connect(): Promise<WalletAccount | undefined>; | ||
switchStarknetChain(_chainId: string): Promise<boolean>; | ||
addStarknetChain(_chain: AddStarknetChainParameters): Promise<boolean>; | ||
disconnect(): Promise<void>; | ||
@@ -24,0 +25,0 @@ tryRetrieveFromQueryOrStorage(): Promise<WalletAccount | undefined>; |
@@ -114,3 +114,3 @@ // src/telegram/provider.ts | ||
name: "@cartridge/controller", | ||
version: "0.5.7", | ||
version: "0.5.8", | ||
description: "Cartridge Controller", | ||
@@ -167,3 +167,3 @@ module: "dist/index.js", | ||
var BaseProvider = class { | ||
constructor(options) { | ||
constructor() { | ||
this.id = "controller"; | ||
@@ -186,4 +186,5 @@ this.name = "Controller"; | ||
} | ||
const silentMode = call.params && call.params.silent_mode; | ||
this.account = await this.probe(); | ||
if (!this.account) { | ||
if (!this.account && !silentMode) { | ||
this.account = await this.connect(); | ||
@@ -202,14 +203,10 @@ } | ||
}; | ||
case "wallet_addStarknetChain": | ||
throw { | ||
code: 63, | ||
message: "An unexpected error occurred", | ||
data: "wallet_addStarknetChain not implemented" | ||
}; | ||
case "wallet_switchStarknetChain": | ||
throw { | ||
code: 63, | ||
message: "An unexpected error occurred", | ||
data: "wallet_switchStarknetChain not implemented" | ||
}; | ||
case "wallet_addStarknetChain": { | ||
let params2 = call.params; | ||
return this.addStarknetChain(params2); | ||
} | ||
case "wallet_switchStarknetChain": { | ||
let params2 = call.params; | ||
return this.switchStarknetChain(params2.chainId); | ||
} | ||
case "wallet_requestChainId": | ||
@@ -220,3 +217,3 @@ if (!this.account) { | ||
message: "An unexpected error occurred", | ||
data: "wallet_deploymentData not implemented" | ||
data: "Account not initialized" | ||
}; | ||
@@ -236,3 +233,3 @@ } | ||
message: "An unexpected error occurred", | ||
data: "wallet_deploymentData not implemented" | ||
data: "Account not initialized" | ||
}; | ||
@@ -293,5 +290,13 @@ } | ||
}; | ||
const { rpc } = options; | ||
this.rpc = new URL(rpc); | ||
} | ||
emitNetworkChanged(chainId) { | ||
this.subscriptions.filter((sub) => sub.type === "networkChanged").forEach((sub) => { | ||
sub.handler(chainId); | ||
}); | ||
} | ||
emitAccountsChanged(accounts) { | ||
this.subscriptions.filter((sub) => sub.type === "accountsChanged").forEach((sub) => { | ||
sub.handler(accounts); | ||
}); | ||
} | ||
}; | ||
@@ -307,5 +312,4 @@ | ||
}) { | ||
super({ | ||
rpc | ||
}); | ||
super(); | ||
this._rpcUrl = rpc; | ||
this._tmaUrl = tmaUrl; | ||
@@ -338,3 +342,3 @@ this._chainId = chainId; | ||
this._policies | ||
)}&rpc_url=${this.rpc}`; | ||
)}&rpc_url=${this._rpcUrl}`; | ||
localStorage.setItem("lastUsedConnector", this.id); | ||
@@ -345,2 +349,8 @@ openLink(url); | ||
} | ||
switchStarknetChain(_chainId) { | ||
throw new Error("switchStarknetChain not implemented"); | ||
} | ||
addStarknetChain(_chain) { | ||
throw new Error("addStarknetChain not implemented"); | ||
} | ||
disconnect() { | ||
@@ -373,3 +383,3 @@ cloudStorage.deleteItem("sessionSigner"); | ||
this.account = new SessionAccount(this, { | ||
rpcUrl: this.rpc.toString(), | ||
rpcUrl: this._rpcUrl, | ||
privateKey: signer.privKey, | ||
@@ -376,0 +386,0 @@ address: sessionRegistration.address, |
import 'starknet'; | ||
import '@starknet-io/types-js'; | ||
export { C as ConnectError, b as ConnectReply, e as ControllerAccounts, a as ControllerError, g as ControllerOptions, D as DeployReply, E as ExecuteReply, h as IFrameOptions, I as IFrames, K as Keychain, j as KeychainOptions, L as LookupRequest, d as LookupResponse, c as LookupResult, M as Modal, m as Prefund, P as ProbeReply, f as Profile, l as ProfileContextTypeVariant, k as ProfileOptions, i as ProviderOptions, R as ResponseCodes, S as Session, T as Tokens } from './types-BReKRAuh.js'; | ||
export { i as Chain, C as ConnectError, b as ConnectReply, e as ControllerAccounts, a as ControllerError, g as ControllerOptions, D as DeployReply, E as ExecuteReply, h as IFrameOptions, I as IFrames, K as Keychain, k as KeychainOptions, L as LookupRequest, d as LookupResponse, c as LookupResult, M as Modal, P as ProbeReply, f as Profile, m as ProfileContextTypeVariant, l as ProfileOptions, j as ProviderOptions, R as ResponseCodes, S as Session, T as Tokens } from './types-CVnDQVqD.js'; | ||
import './index.d-BbTUPBeO.js'; | ||
import '@cartridge/penpal'; |
{ | ||
"name": "@cartridge/controller", | ||
"version": "0.5.7", | ||
"version": "0.5.8", | ||
"description": "Cartridge Controller", | ||
@@ -34,3 +34,3 @@ "module": "dist/index.js", | ||
"starknet": "^6.11.0", | ||
"@cartridge/account-wasm": "0.5.7" | ||
"@cartridge/account-wasm": "0.5.8" | ||
}, | ||
@@ -40,3 +40,3 @@ "devDependencies": { | ||
"typescript": "^5.4.5", | ||
"@cartridge/tsconfig": "0.5.7" | ||
"@cartridge/tsconfig": "0.5.8" | ||
}, | ||
@@ -43,0 +43,0 @@ "scripts": { |
@@ -30,2 +30,3 @@ import { | ||
provider: BaseProvider, | ||
rpcUrl: string, | ||
address: string, | ||
@@ -36,3 +37,3 @@ keychain: AsyncMethodReturns<Keychain>, | ||
) { | ||
super({ nodeUrl: provider.rpc.toString() }, provider); | ||
super({ nodeUrl: rpcUrl }, provider); | ||
@@ -39,0 +40,0 @@ this.address = address; |
@@ -16,6 +16,8 @@ import { AsyncMethodReturns } from "@cartridge/penpal"; | ||
ProfileContextTypeVariant, | ||
Chain, | ||
} from "./types"; | ||
import BaseProvider from "./provider"; | ||
import { WalletAccount } from "starknet"; | ||
import { constants, WalletAccount } from "starknet"; | ||
import { Policy } from "@cartridge/presets"; | ||
import { AddStarknetChainParameters, ChainId } from "@starknet-io/types-js"; | ||
@@ -27,7 +29,56 @@ export default class ControllerProvider extends BaseProvider { | ||
private iframes: IFrames; | ||
private selectedChain: ChainId; | ||
private chains: Map<ChainId, Chain>; | ||
constructor(options: ControllerOptions) { | ||
const { rpc } = options; | ||
super({ rpc }); | ||
super(); | ||
const chains = new Map<ChainId, Chain>(); | ||
for (const chain of options.chains) { | ||
let chainId: ChainId | undefined; | ||
const url = new URL(chain.rpcUrl); | ||
const parts = url.pathname.split("/"); | ||
if (parts.includes("starknet")) { | ||
if (parts.includes("mainnet")) { | ||
chainId = constants.StarknetChainId.SN_MAIN; | ||
} else if (parts.includes("sepolia")) { | ||
chainId = constants.StarknetChainId.SN_SEPOLIA; | ||
} | ||
} else if (parts.length >= 3) { | ||
const projectName = parts[2]; | ||
if (parts.includes("katana")) { | ||
chainId = `WP_${projectName.toUpperCase()}` as ChainId; | ||
} else if (parts.includes("mainnet")) { | ||
chainId = `GG_${projectName.toUpperCase()}` as ChainId; | ||
} | ||
} | ||
if (!chainId) { | ||
throw new Error(`Chain ${chain.rpcUrl} not supported`); | ||
} | ||
chains.set(chainId, chain); | ||
} | ||
if ( | ||
options.policies?.messages?.length && | ||
options.policies.messages.length !== chains.size | ||
) { | ||
console.warn( | ||
"Each message policy is associated with a specific chain. " + | ||
"The number of message policies does not match the number of chains specified - " + | ||
"session message signing may not work on some chains.", | ||
); | ||
} | ||
this.chains = chains; | ||
this.selectedChain = options.defaultChainId; | ||
if (!this.chains.has(this.selectedChain)) { | ||
throw new Error( | ||
`Chain ${this.selectedChain} not found in configured chains`, | ||
); | ||
} | ||
this.iframes = { | ||
@@ -59,8 +110,7 @@ keychain: new KeychainIFrame({ | ||
const response = (await this.keychain.probe( | ||
this.rpc.toString(), | ||
)) as ProbeReply; | ||
const response = (await this.keychain.probe(this.rpcUrl())) as ProbeReply; | ||
this.account = new ControllerAccount( | ||
this, | ||
this.rpcUrl(), | ||
response.address, | ||
@@ -89,3 +139,3 @@ this.keychain, | ||
}, | ||
rpcUrl: this.rpc.toString(), | ||
rpcUrl: this.rpcUrl(), | ||
username, | ||
@@ -120,4 +170,4 @@ version: this.version, | ||
let response = await this.keychain.connect( | ||
this.options.policies || [], | ||
this.rpc.toString(), | ||
this.options.policies || {}, | ||
this.rpcUrl(), | ||
); | ||
@@ -131,2 +181,3 @@ if (response.code !== ResponseCodes.SUCCESS) { | ||
this, | ||
this.rpcUrl(), | ||
response.address, | ||
@@ -146,2 +197,22 @@ this.keychain, | ||
async switchStarknetChain(chainId: string): Promise<boolean> { | ||
try { | ||
this.selectedChain = chainId; | ||
this.account = await this.probe(); | ||
if (!this.account) { | ||
this.account = await this.connect(); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
return false; | ||
} | ||
this.emitNetworkChanged(chainId); | ||
return true; | ||
} | ||
addStarknetChain(_chain: AddStarknetChainParameters): Promise<boolean> { | ||
return Promise.resolve(true); | ||
} | ||
async disconnect() { | ||
@@ -207,2 +278,6 @@ if (!this.keychain) { | ||
rpcUrl(): string { | ||
return this.chains.get(this.selectedChain)!.rpcUrl; | ||
} | ||
username() { | ||
@@ -209,0 +284,0 @@ if (!this.keychain) { |
import { WalletAccount } from "starknet"; | ||
import { | ||
AddInvokeTransactionParameters, | ||
AddStarknetChainParameters, | ||
Errors, | ||
Permission, | ||
RequestAccountsParameters, | ||
RequestFn, | ||
StarknetWindowObject, | ||
SwitchStarknetChainParameters, | ||
TypedData, | ||
@@ -16,3 +19,2 @@ WalletEventHandlers, | ||
import { icon } from "./icon"; | ||
import { ProviderOptions } from "./types"; | ||
@@ -25,12 +27,5 @@ export default abstract class BaseProvider implements StarknetWindowObject { | ||
public rpc: URL; | ||
public account?: WalletAccount; | ||
public subscriptions: WalletEvents[] = []; | ||
constructor(options: ProviderOptions) { | ||
const { rpc } = options; | ||
this.rpc = new URL(rpc); | ||
} | ||
request: RequestFn = async (call) => { | ||
@@ -52,4 +47,7 @@ switch (call.type) { | ||
const silentMode = | ||
call.params && (call.params as RequestAccountsParameters).silent_mode; | ||
this.account = await this.probe(); | ||
if (!this.account) { | ||
if (!this.account && !silentMode) { | ||
this.account = await this.connect(); | ||
@@ -72,15 +70,11 @@ } | ||
case "wallet_addStarknetChain": | ||
throw { | ||
code: 63, | ||
message: "An unexpected error occurred", | ||
data: "wallet_addStarknetChain not implemented", | ||
} as Errors.UNEXPECTED_ERROR; | ||
case "wallet_addStarknetChain": { | ||
let params = call.params as AddStarknetChainParameters; | ||
return this.addStarknetChain(params); | ||
} | ||
case "wallet_switchStarknetChain": | ||
throw { | ||
code: 63, | ||
message: "An unexpected error occurred", | ||
data: "wallet_switchStarknetChain not implemented", | ||
} as Errors.UNEXPECTED_ERROR; | ||
case "wallet_switchStarknetChain": { | ||
let params = call.params as SwitchStarknetChainParameters; | ||
return this.switchStarknetChain(params.chainId); | ||
} | ||
@@ -92,3 +86,3 @@ case "wallet_requestChainId": | ||
message: "An unexpected error occurred", | ||
data: "wallet_deploymentData not implemented", | ||
data: "Account not initialized", | ||
} as Errors.UNEXPECTED_ERROR; | ||
@@ -111,3 +105,3 @@ } | ||
message: "An unexpected error occurred", | ||
data: "wallet_deploymentData not implemented", | ||
data: "Account not initialized", | ||
} as Errors.UNEXPECTED_ERROR; | ||
@@ -182,4 +176,24 @@ } | ||
protected emitNetworkChanged(chainId: string) { | ||
this.subscriptions | ||
.filter((sub) => sub.type === "networkChanged") | ||
.forEach((sub) => { | ||
(sub.handler as WalletEventHandlers["networkChanged"])(chainId); | ||
}); | ||
} | ||
protected emitAccountsChanged(accounts: string[]) { | ||
this.subscriptions | ||
.filter((sub) => sub.type === "accountsChanged") | ||
.forEach((sub) => { | ||
(sub.handler as WalletEventHandlers["accountsChanged"])(accounts); | ||
}); | ||
} | ||
abstract probe(): Promise<WalletAccount | undefined>; | ||
abstract connect(): Promise<WalletAccount | undefined>; | ||
abstract switchStarknetChain(chainId: string): Promise<boolean>; | ||
abstract addStarknetChain( | ||
chain: AddStarknetChainParameters, | ||
): Promise<boolean>; | ||
} |
@@ -8,2 +8,3 @@ import { ec, stark, WalletAccount } from "starknet"; | ||
import { SessionPolicies } from "@cartridge/presets"; | ||
import { AddStarknetChainParameters } from "@starknet-io/types-js"; | ||
@@ -30,3 +31,3 @@ interface SessionRegistration { | ||
protected _chainId: string; | ||
protected _rpcUrl: string; | ||
protected _username?: string; | ||
@@ -37,4 +38,5 @@ protected _redirectUrl: string; | ||
constructor({ rpc, chainId, policies, redirectUrl }: SessionOptions) { | ||
super({ rpc }); | ||
super(); | ||
this._rpcUrl = rpc; | ||
this._chainId = chainId; | ||
@@ -82,3 +84,3 @@ this._redirectUrl = redirectUrl; | ||
this._policies, | ||
)}&rpc_url=${this.rpc}`; | ||
)}&rpc_url=${this._rpcUrl}`; | ||
@@ -91,2 +93,10 @@ localStorage.setItem("lastUsedConnector", this.id); | ||
switchStarknetChain(_chainId: string): Promise<boolean> { | ||
throw new Error("switchStarknetChain not implemented"); | ||
} | ||
addStarknetChain(_chain: AddStarknetChainParameters): Promise<boolean> { | ||
throw new Error("addStarknetChain not implemented"); | ||
} | ||
disconnect(): Promise<void> { | ||
@@ -135,3 +145,3 @@ localStorage.removeItem("sessionSigner"); | ||
this.account = new SessionAccount(this, { | ||
rpcUrl: this.rpc.toString(), | ||
rpcUrl: this._rpcUrl, | ||
privateKey: signer.privKey, | ||
@@ -138,0 +148,0 @@ address: sessionRegistration.address, |
@@ -14,2 +14,3 @@ import { | ||
import { SessionPolicies } from "@cartridge/presets"; | ||
import { AddStarknetChainParameters } from "@starknet-io/types-js"; | ||
@@ -29,2 +30,3 @@ interface SessionRegistration { | ||
protected _policies: SessionPolicies; | ||
private _rpcUrl: string; | ||
@@ -42,6 +44,5 @@ constructor({ | ||
}) { | ||
super({ | ||
rpc, | ||
}); | ||
super(); | ||
this._rpcUrl = rpc; | ||
this._tmaUrl = tmaUrl; | ||
@@ -83,3 +84,3 @@ this._chainId = chainId; | ||
this._policies, | ||
)}&rpc_url=${this.rpc}`; | ||
)}&rpc_url=${this._rpcUrl}`; | ||
@@ -93,2 +94,10 @@ localStorage.setItem("lastUsedConnector", this.id); | ||
switchStarknetChain(_chainId: string): Promise<boolean> { | ||
throw new Error("switchStarknetChain not implemented"); | ||
} | ||
addStarknetChain(_chain: AddStarknetChainParameters): Promise<boolean> { | ||
throw new Error("addStarknetChain not implemented"); | ||
} | ||
disconnect(): Promise<void> { | ||
@@ -126,3 +135,3 @@ cloudStorage.deleteItem("sessionSigner"); | ||
this.account = new SessionAccount(this, { | ||
rpcUrl: this.rpc.toString(), | ||
rpcUrl: this._rpcUrl, | ||
privateKey: signer.privKey, | ||
@@ -129,0 +138,0 @@ address: sessionRegistration.address, |
@@ -10,2 +10,3 @@ import { | ||
AddInvokeTransactionResult, | ||
ChainId, | ||
Signature, | ||
@@ -15,8 +16,3 @@ TypedData, | ||
import { KeychainIFrame, ProfileIFrame } from "./iframe"; | ||
import { | ||
ColorMode, | ||
Policies, | ||
Policy, | ||
SessionPolicies, | ||
} from "@cartridge/presets"; | ||
import { ColorMode, Policy, SessionPolicies } from "@cartridge/presets"; | ||
@@ -105,3 +101,3 @@ export type Session = { | ||
connect( | ||
policies: Policies, | ||
policies: SessionPolicies, | ||
rpcUrl: string, | ||
@@ -140,2 +136,3 @@ ): Promise<ConnectReply | ConnectError>; | ||
} | ||
export interface Profile { | ||
@@ -168,9 +165,13 @@ navigate(path: string): void; | ||
export type Chain = { | ||
rpcUrl: string; | ||
}; | ||
export type ProviderOptions = { | ||
/** The URL of the RPC */ | ||
rpc: string; | ||
defaultChainId: ChainId; | ||
chains: Chain[]; | ||
}; | ||
export type KeychainOptions = IFrameOptions & { | ||
policies?: Policies; | ||
policies?: SessionPolicies; | ||
/** The URL of keychain */ | ||
@@ -201,6 +202,4 @@ url?: string; | ||
export type Prefund = { address: string; min: string }; | ||
export type Tokens = { | ||
erc20?: string[]; | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1281649
15404
+ Added@cartridge/account-wasm@0.5.8(transitive)
- Removed@cartridge/account-wasm@0.5.7(transitive)