chaingate
Version:
A complete TypeScript library for connecting to and making transactions on different blockchains
163 lines • 7.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createClientAndMarkets = createClientAndMarkets;
exports.create = create;
exports.fromPhrase = fromPhrase;
exports.checkPhrase = checkPhrase;
exports.fromSeed = fromSeed;
exports.checkSeed = checkSeed;
exports.fromPrivateKey = fromPrivateKey;
exports.checkPrivateKey = checkPrivateKey;
exports.fromKeystore = fromKeystore;
exports.checkKeystore = checkKeystore;
exports.deserialize = deserialize;
exports.checkSerialized = checkSerialized;
const PhraseGenerator_1 = require("./Wallet/implementations/PhraseWallet/PhraseGenerator");
const Phrase_1 = require("./Wallet/entities/Secret/implementations/Phrase");
const PhraseWallet_1 = require("./Wallet/implementations/PhraseWallet/PhraseWallet");
const Wallet_1 = require("./Wallet/Wallet");
const SeedWallet_1 = require("./Wallet/implementations/SeedWallet/SeedWallet");
const PrivateKeyWallet_1 = require("./Wallet/implementations/PrivateKeyWallet/PrivateKeyWallet");
const LegacyKeystore_1 = require("./Wallet/entities/Keystore/LegacyKeystore");
const Web3Keystore_1 = require("./Wallet/entities/Keystore/Web3Keystore");
const errors_1 = require("./Wallet/entities/Keystore/errors");
const WalletEncryption_1 = require("./Wallet/entities/WalletEncryption/WalletEncryption");
const Seed_1 = require("./Wallet/entities/Secret/implementations/Seed");
const PrivateKey_1 = require("./Wallet/entities/Secret/implementations/PrivateKey");
const client_fetch_1 = require("@hey-api/client-fetch");
const Client_1 = require("./Client");
const TtlCache_1 = require("./InternalUtils/TtlCache");
function createClientAndMarkets(apiKey) {
const client = (0, client_fetch_1.createClient)((0, client_fetch_1.createConfig)({
baseUrl: 'https://api.chaingate.dev',
headers: { 'x-api-key': apiKey },
throwOnError: true,
}));
const markets = new TtlCache_1.TtlCache(() => (0, Client_1.globalMarkets)({ client }).then((r) => {
if (r.error || !r.data)
throw r.error ?? new Error('No data');
return r.data;
}), 30);
return { client, markets };
}
async function create({ apiKey = '', phraseLanguage = 'english', phraseNumOfWords = 12, encrypt, warnAboutUnencrypted = true, } = {}) {
const client = createClientAndMarkets(apiKey);
const phrase = (0, PhraseGenerator_1.generateNewPhrase)(phraseLanguage, phraseNumOfWords);
let wallet;
if (encrypt)
wallet = await PhraseWallet_1.PhraseWallet.new(client.client, client.markets, phrase, warnAboutUnencrypted, encrypt);
else
wallet = await PhraseWallet_1.PhraseWallet.new(client.client, client.markets, phrase, warnAboutUnencrypted);
return { phrase, wallet };
}
async function fromPhrase({ apiKey = '', phrase, encrypt, warnAboutUnencrypted = true, }) {
const client = createClientAndMarkets(apiKey);
return PhraseWallet_1.PhraseWallet.new(client.client, client.markets, phrase, warnAboutUnencrypted, encrypt);
}
async function checkPhrase(phrase) {
try {
Phrase_1.Phrase.isValidPhrase(phrase);
return true;
}
catch (_ex) {
return false;
}
}
async function fromSeed({ apiKey = '', seed, encrypt, warnAboutUnencrypted = true, }) {
const client = createClientAndMarkets(apiKey);
return SeedWallet_1.SeedWallet.new(client.client, client.markets, seed, warnAboutUnencrypted, encrypt);
}
async function checkSeed(seed) {
try {
new Seed_1.Seed(seed);
return true;
}
catch (_ex) {
return false;
}
}
async function fromPrivateKey({ apiKey = '', privateKey, encrypt, warnAboutUnencrypted = true, }) {
const client = createClientAndMarkets(apiKey);
return await PrivateKeyWallet_1.PrivateKeyWallet.new(client.client, client.markets, privateKey, warnAboutUnencrypted, encrypt);
}
async function checkPrivateKey(privateKey) {
try {
new PrivateKey_1.PrivateKey(privateKey);
return true;
}
catch (_ex) {
return false;
}
}
async function fromKeystore({ apiKey = '', keystore, password, encrypt, warnAboutUnencrypted = true, }) {
const client = createClientAndMarkets(apiKey);
try {
const obj = JSON.parse(keystore);
let decrypted;
if (LegacyKeystore_1.LegacyKeystore.isKeystore(obj))
decrypted = await new LegacyKeystore_1.LegacyKeystore(obj).decrypt(password);
else if (Web3Keystore_1.Web3Keystore.isKeystore(obj))
decrypted = await new Web3Keystore_1.Web3Keystore(obj).decrypt(password);
else
throw new WalletEncryption_1.EncodingError('Invalid json');
try {
const phraseText = new TextDecoder().decode(decrypted);
if (!Phrase_1.Phrase.isValidPhrase(phraseText))
return PrivateKeyWallet_1.PrivateKeyWallet.new(client.client, client.markets, decrypted, warnAboutUnencrypted, encrypt);
return PhraseWallet_1.PhraseWallet.new(client.client, client.markets, phraseText, warnAboutUnencrypted, encrypt);
}
catch (_ex) {
return PrivateKeyWallet_1.PrivateKeyWallet.new(client.client, client.markets, decrypted, warnAboutUnencrypted, encrypt);
}
}
catch (ex) {
if (ex instanceof errors_1.IncorrectPassword)
throw ex;
throw new WalletEncryption_1.EncodingError('Invalid json');
}
}
async function checkKeystore(keystore) {
try {
const keystoreObj = JSON.parse(keystore);
return LegacyKeystore_1.LegacyKeystore.isKeystore(keystoreObj) || Web3Keystore_1.Web3Keystore.isKeystore(keystoreObj);
}
catch (_ex) {
return false;
}
}
async function deserialize({ apiKey = '', serialized, askForPassword, }) {
const client = createClientAndMarkets(apiKey);
try {
const serializedParsed = JSON.parse(serialized);
if (!Wallet_1.Wallet.isSerializedWallet(serializedParsed))
throw new WalletEncryption_1.EncodingError('Invalid serialized');
const serializedWallet = serializedParsed;
switch (serializedWallet.walletType) {
case 'privateKey':
return await PrivateKeyWallet_1.PrivateKeyWallet.import(client.client, client.markets, serializedWallet, askForPassword);
case 'phrase':
return await PhraseWallet_1.PhraseWallet.import(client.client, client.markets, serializedWallet, askForPassword);
case 'seed':
return await SeedWallet_1.SeedWallet.import(client.client, client.markets, serializedWallet, askForPassword);
}
throw new WalletEncryption_1.EncodingError('Invalid serialized');
}
catch (ex) {
throw new WalletEncryption_1.EncodingError('Invalid serialized');
}
}
async function checkSerialized(serialized) {
try {
await deserialize({
serialized,
askForPassword: async () => {
return '';
},
});
return true;
}
catch (_ex) {
return false;
}
}
//# sourceMappingURL=InitializeWallet.js.map