UNPKG

chaingate

Version:

A complete TypeScript library for connecting to and making transactions on different blockchains

150 lines 5.85 kB
import { generateNewPhrase } from './Wallet/implementations/PhraseWallet/PhraseGenerator'; import { Phrase } from './Wallet/entities/Secret/implementations/Phrase'; import { PhraseWallet } from './Wallet'; import { Wallet } from './Wallet/Wallet'; import { SeedWallet } from './Wallet'; import { PrivateKeyWallet, } from './Wallet/implementations/PrivateKeyWallet/PrivateKeyWallet'; import { LegacyKeystore } from './Wallet/entities/Keystore/LegacyKeystore'; import { Web3Keystore } from './Wallet/entities/Keystore/Web3Keystore'; import { IncorrectPassword } from './Wallet/entities/Keystore/errors'; import { EncodingError } from './Wallet/entities/WalletEncryption/WalletEncryption'; import { Seed } from './Wallet/entities/Secret/implementations/Seed'; import { PrivateKey } from './Wallet/entities/Secret/implementations/PrivateKey'; import { createClient, createConfig } from '@hey-api/client-fetch'; import { globalMarkets } from './Client'; import { TtlCache } from './InternalUtils/TtlCache'; export function createChainGateContext(apiKey) { const client = createClient(createConfig({ baseUrl: 'https://api.chaingate.dev', headers: { 'x-api-key': apiKey }, throwOnError: true, })); const markets = new TtlCache(() => globalMarkets({ client }).then((r) => { if (r.error || !r.data) throw r.error ?? new Error('No data'); return r.data; }), 30); const extra = new Map(); return { client, markets, extra }; } export async function create({ apiKey = '', phraseLanguage = 'english', phraseNumOfWords = 12, encrypt, warnAboutUnencrypted = true, } = {}) { const context = createChainGateContext(apiKey); const phrase = generateNewPhrase(phraseLanguage, phraseNumOfWords); let wallet; if (encrypt) wallet = await PhraseWallet.new(context, phrase, warnAboutUnencrypted, encrypt); else wallet = await PhraseWallet.new(context, phrase, warnAboutUnencrypted); return { phrase, wallet }; } export async function fromPhrase({ apiKey = '', phrase, encrypt, warnAboutUnencrypted = true, }) { const context = createChainGateContext(apiKey); return PhraseWallet.new(context, phrase, warnAboutUnencrypted, encrypt); } export async function checkPhrase(phrase) { try { Phrase.isValidPhrase(phrase); return true; } catch (_ex) { return false; } } export async function fromSeed({ apiKey = '', seed, encrypt, warnAboutUnencrypted = true, }) { const context = createChainGateContext(apiKey); return SeedWallet.new(context, seed, warnAboutUnencrypted, encrypt); } export async function checkSeed(seed) { try { new Seed(seed); return true; } catch (_ex) { return false; } } export async function fromPrivateKey({ apiKey = '', privateKey, encrypt, warnAboutUnencrypted = true, }) { const context = createChainGateContext(apiKey); return await PrivateKeyWallet.new(context, privateKey, warnAboutUnencrypted, encrypt); } export async function checkPrivateKey(privateKey) { try { new PrivateKey(privateKey); return true; } catch (_ex) { return false; } } export async function fromKeystore({ apiKey = '', keystore, password, encrypt, warnAboutUnencrypted = true, }) { const context = createChainGateContext(apiKey); try { const obj = JSON.parse(keystore); let decrypted; if (LegacyKeystore.isKeystore(obj)) decrypted = await new LegacyKeystore(obj).decrypt(password); else if (Web3Keystore.isKeystore(obj)) decrypted = await new Web3Keystore(obj).decrypt(password); else throw new EncodingError('Invalid json'); try { const phraseText = new TextDecoder().decode(decrypted); if (!Phrase.isValidPhrase(phraseText)) return PrivateKeyWallet.new(context, decrypted, warnAboutUnencrypted, encrypt); return PhraseWallet.new(context, phraseText, warnAboutUnencrypted, encrypt); } catch (_ex) { return PrivateKeyWallet.new(context, decrypted, warnAboutUnencrypted, encrypt); } } catch (ex) { if (ex instanceof IncorrectPassword) throw ex; throw new EncodingError('Invalid json'); } } export async function checkKeystore(keystore) { try { const keystoreObj = JSON.parse(keystore); return LegacyKeystore.isKeystore(keystoreObj) || Web3Keystore.isKeystore(keystoreObj); } catch (_ex) { return false; } } export async function deserialize({ apiKey = '', serialized, askForPassword, }) { const context = createChainGateContext(apiKey); try { const serializedParsed = JSON.parse(serialized); if (!Wallet.isSerializedWallet(serializedParsed)) throw new EncodingError('Invalid serialized'); const serializedWallet = serializedParsed; switch (serializedWallet.walletType) { case 'privateKey': return await PrivateKeyWallet.import(context, serializedWallet, askForPassword); case 'phrase': return await PhraseWallet.import(context, serializedWallet, askForPassword); case 'seed': return await SeedWallet.import(context, serializedWallet, askForPassword); } throw new EncodingError('Invalid serialized'); } catch (ex) { throw new EncodingError('Invalid serialized'); } } export async function checkSerialized(serialized) { try { await deserialize({ serialized, askForPassword: async () => { return ''; }, }); return true; } catch (_ex) { return false; } } //# sourceMappingURL=InitializeWallet.js.map