@cosmos-kit/galaxy-station-extension
Version:
@cosmos-kit/galaxy-station-extension is the Galaxy Station integration for CosmosKit
84 lines (83 loc) • 3.07 kB
JavaScript
import Long from 'long';
export class GalaxyStationClient {
client;
_defaultSignOptions = {
preferNoSetFee: false,
preferNoSetMemo: true,
disableBalanceCheck: true,
};
get defaultSignOptions() {
return this._defaultSignOptions;
}
constructor(client) {
this.client = client;
}
async disconnect() {
return;
}
async getSimpleAccount(chainId) {
const { name, addresses } = await this.client.connect();
const address = addresses[chainId];
if (!address)
throw new Error(`Requested chainId (${chainId}) is not available, try to switch network on the Galaxy Station extension.`);
return {
namespace: 'cosmos',
chainId,
address,
username: name,
};
}
async getAccount(chainId) {
const info = (await this.client.info())[chainId];
if (!info)
throw new Error(`The requested chainID (${chainId}) is not available, try to switch network on the Galaxy Station extension.`);
let { name, addresses, pubkey: pubkeys } = await this.client.connect();
if (!pubkeys) {
pubkeys = (await this.client.getPublicKey()).pubkey;
}
const pubkey = pubkeys?.[info.coinType];
const address = addresses[chainId];
if (!address || !pubkey)
throw new Error('The requested account is not available, try to use a different wallet on the Galaxy Station extension or to import it again.');
return {
address,
pubkey: Buffer.from(pubkey, 'base64'),
username: name,
isNanoLedger: true,
algo: 'secp256k1',
};
}
async signAmino(chainId, signer, signDoc, _signOptions) {
return await this.client.keplr.signAmino(chainId, signer, signDoc);
}
getOfflineSigner(chainId, preferredSignType) {
switch (preferredSignType) {
case 'amino':
return this.getOfflineSignerAmino(chainId);
case 'direct':
return this.getOfflineSignerDirect(chainId);
default:
return this.getOfflineSignerAmino(chainId);
}
}
getOfflineSignerAmino(chainId) {
return this.client.keplr.getOfflineSignerOnlyAmino(chainId);
}
getOfflineSignerDirect(chainId) {
return {
getAccounts: async () => {
return [await this.getAccount(chainId)];
},
signDirect: (signerAddress, signDoc) => this.signDirect(chainId, signerAddress, signDoc),
};
}
async signArbitrary(chainId, signer, data) {
return await this.client.keplr.signArbitrary(chainId, signer, data);
}
async signDirect(chainId, signer, signDoc, signOptions) {
return await this.client.keplr.signDirect(chainId, signer, {
...signDoc,
accountNumber: Long.fromString(signDoc.accountNumber.toString()),
}, signOptions || this.defaultSignOptions);
}
}