@quirks/core
Version:
215 lines (214 loc) • 6.3 kB
JavaScript
import { Wallet as l } from "./index2.js";
import { toByteArray as a, fromByteArray as d } from "base64-js";
import { WcEventTypes as g, WcProviderEventType as h } from "./index6.js";
import { assertIsDefined as c } from "./index8.js";
class b extends l {
constructor(e) {
super(e), this.injected = !0;
}
async init(e) {
const n = (await import("@walletconnect/universal-provider")).UniversalProvider;
try {
return this.client = await n.init(e), this.client && this.addListeners(), this.client;
} catch (s) {
this.injectionError = s;
}
}
removeListeners() {
if (super.removeListeners(), this.client)
for (const e of this.wcEventNames)
this.client.removeListener(e, () => {
});
}
addListeners() {
if (this.removeListeners(), this.client) {
for (const e of this.wcEventNames)
this.client.on(e, (n) => {
this.events.emit(e, n), e === "accountsChanged" && this.events.emit("keystorechange");
});
this.events.on("display_uri", (e) => {
this.pairingURI = e;
});
}
}
/**
* This method connect the client with wallet connect server and retrieve
* the topic (uri), to start a wc connection.
*/
async generateURI(e) {
return c(this.client, "client is undefined"), this.client.connect({
...e
});
}
async enable() {
console.debug("enable method not implemented");
}
async disable() {
c(this.client, "client is undefined"), this.client.session && await this.client.disconnect(), await this.client.cleanupPendingPairings();
}
async getAccount(e) {
var t, r;
c(this.client, "client is undefined");
const n = (r = (t = this.client.session) == null ? void 0 : t.namespaces) == null ? void 0 : r.cosmos, s = n == null ? void 0 : n.accounts.map((o) => o.split(":")).find(([, o]) => o === e);
let i = "";
return s && (i = s.pop() ?? ""), {
name: "",
pubKey: null,
address: null,
bech32Address: i ?? "",
algo: "unknown",
isKeystone: null,
isNanoLedger: null
};
}
async getAccounts(e) {
c(this.client, "client is undefined");
const n = [];
for (const s of e) {
const i = await this.getAccount(s);
n.push(i);
}
return n;
}
async getSignerAccounts() {
var s, i;
return c(this.client, "client is undefined"), (i = (s = this.client.session) == null ? void 0 : s.sessionProperties) != null && i.keys ? JSON.parse(
this.client.session.sessionProperties.keys
).map((o) => ({
address: o.bech32Address,
algo: o.algo,
pubkey: a(o.pubKey)
})) : (await this.client.request({
method: "cosmos_getAccounts"
})).map((t) => ({
address: t.address,
algo: t.algo,
pubkey: a(t.pubkey)
}));
}
async getOfflineSigner(e) {
return c(this.client, "client is undefined"), {
getAccounts: async () => this.getSignerAccounts(),
signAmino: (n, s) => this.signAmino(e, n, s),
signDirect: (n, s) => this.signDirect(e, n, s)
};
}
async getOfflineSignerOnlyAmino(e) {
return c(this.client, "client is undefined"), {
getAccounts: async () => this.getSignerAccounts(),
signAmino: (n, s) => this.signAmino(e, n, s)
};
}
async getOfflineSignerAuto(e) {
return c(this.client, "client is undefined"), {
getAccounts: async () => this.getSignerAccounts(),
signAmino: (n, s) => this.signAmino(e, n, s),
signDirect: (n, s) => this.signDirect(e, n, s)
};
}
async signAmino(e, n, s) {
return c(this.client, "client is undefined"), this.client.request(
{
method: "cosmos_signAmino",
params: {
signerAddress: n,
signDoc: s
}
},
`cosmos:${e}`
);
}
async signDirect(e, n, s) {
c(this.client, "client is undefined");
const i = {
signerAddress: n,
signDoc: {
chainId: s.chainId,
bodyBytes: d(s.bodyBytes),
authInfoBytes: d(s.authInfoBytes),
accountNumber: s.accountNumber.toString()
}
}, t = await this.client.request(
{
method: "cosmos_signDirect",
params: i
},
`cosmos:${e}`
);
return {
...t,
signed: {
chainId: t.signed.chainId,
accountNumber: BigInt(t.signed.accountNumber),
authInfoBytes: a(t.signed.authInfoBytes),
bodyBytes: a(t.signed.bodyBytes)
}
};
}
async signArbitrary(e, n, s) {
return c(this.client, "client is undefined"), {
...(await this.client.request(
{
method: "cosmos_signAmino",
params: {
signerAddress: n,
signDoc: {
chain_id: "",
account_number: "0",
sequence: "0",
fee: {
gas: "0",
amount: []
},
msgs: [
{
type: "sign/MsgSignData",
value: {
signer: n,
data: typeof s == "string" ? btoa(s) : d(s)
}
}
],
memo: ""
}
}
},
`cosmos:${e}`
)).signature
};
}
async verifyArbitrary() {
throw new Error("[Quirks]: Method not implemented.");
}
async suggestTokens() {
console.debug("suggestTokens method not implemented.");
}
async suggestChains() {
console.debug("suggestChains method not implemented.");
}
get wcEventNames() {
return [
...Object.keys(g),
...Object.keys(h)
];
}
get deeplinks() {
var s, i, t, r, o, u;
const { mobile: e, wallet_connect: n } = this.options;
if (!(!e || !n))
return {
ios: `${(s = e.ios) == null ? void 0 : s.schema}://${(t = (i = n.deeplink) == null ? void 0 : i.path) == null ? void 0 : t.ios}`,
android: `${(r = e.android) == null ? void 0 : r.schema}://${(u = (o = n.deeplink) == null ? void 0 : o.path) == null ? void 0 : u.android}`
};
}
get pairingDeeplinks() {
if (this.deeplinks)
return {
ios: `${this.deeplinks.ios}?${this.pairingURI}`,
android: `${this.deeplinks.android}?${this.pairingURI}`
};
}
}
export {
b as WCWallet
};