63 lines
1.9 KiB
JavaScript
63 lines
1.9 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.Connection = void 0;
|
|
const signers_1 = require("@near-js/signers");
|
|
const providers_1 = require("@near-js/providers");
|
|
/**
|
|
* @param config Contains connection info details
|
|
* @returns {Provider}
|
|
*/
|
|
function getProvider(config) {
|
|
switch (config.type) {
|
|
case undefined:
|
|
return config;
|
|
case 'JsonRpcProvider': return new providers_1.JsonRpcProvider({ ...config.args });
|
|
case 'FailoverRpcProvider': {
|
|
const providers = (config?.args || []).map((arg) => new providers_1.JsonRpcProvider(arg));
|
|
return new providers_1.FailoverRpcProvider(providers);
|
|
}
|
|
default: throw new Error(`Unknown provider type ${config.type}`);
|
|
}
|
|
}
|
|
/**
|
|
* @param config Contains connection info details
|
|
* @returns {Signer}
|
|
*/
|
|
function getSigner(config) {
|
|
switch (config.type) {
|
|
case undefined:
|
|
return config;
|
|
case 'InMemorySigner': {
|
|
return new signers_1.InMemorySigner(config.keyStore);
|
|
}
|
|
default: throw new Error(`Unknown signer type ${config.type}`);
|
|
}
|
|
}
|
|
/**
|
|
* Connects an account to a given network via a given provider
|
|
*/
|
|
class Connection {
|
|
networkId;
|
|
provider;
|
|
signer;
|
|
jsvmAccountId;
|
|
constructor(networkId, provider, signer, jsvmAccountId) {
|
|
this.networkId = networkId;
|
|
this.provider = provider;
|
|
this.signer = signer;
|
|
this.jsvmAccountId = jsvmAccountId;
|
|
}
|
|
getConnection() {
|
|
return this;
|
|
}
|
|
/**
|
|
* @param config Contains connection info details
|
|
*/
|
|
static fromConfig(config) {
|
|
const provider = getProvider(config.provider);
|
|
const signer = getSigner(config.signer);
|
|
return new Connection(config.networkId, provider, signer, config.jsvmAccountId);
|
|
}
|
|
}
|
|
exports.Connection = Connection;
|