@civic/sol-did-client
Version:
A powerful DID-method on Solana
196 lines • 7.7 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SentDataTransaction = exports.SentTransaction = exports.SendableDataTransaction = exports.SendableTransaction = exports.getConnection = exports.getConnectionByCluster = exports.getClusterUrl = exports.clusterFromString = exports.LOCALNET_CLUSTER_URL = exports.CIVICNET_CLUSTER_URL = void 0;
exports.addHashOrNonce = addHashOrNonce;
const web3_js_1 = require("@solana/web3.js");
const const_1 = require("./const");
exports.CIVICNET_CLUSTER_URL = 'https://d3ab7dlfud2b5u.cloudfront.net';
exports.LOCALNET_CLUSTER_URL = 'http://localhost:8899';
/**
* Try to map a generic (optional) string to a ExtendedCluster string
* @param cluster optional cluster string
*/
const clusterFromString = (cluster) => {
switch (cluster) {
case 'localnet':
return 'localnet';
case 'civicnet':
return 'civicnet';
case 'devnet':
return 'devnet';
case 'testnet':
return 'testnet';
case 'mainnet-beta':
return 'mainnet-beta';
}
};
exports.clusterFromString = clusterFromString;
const getClusterUrl = (cluster, customConfig) => {
// return custom cluster url if it exists
if (customConfig && customConfig[cluster]) {
return customConfig[cluster];
}
switch (cluster) {
case 'localnet':
return exports.LOCALNET_CLUSTER_URL;
case 'civicnet':
return exports.CIVICNET_CLUSTER_URL;
default:
return (0, web3_js_1.clusterApiUrl)(cluster);
}
};
exports.getClusterUrl = getClusterUrl;
const getConnectionByCluster = (cluster = 'localnet', preflightCommitment = const_1.SOLANA_COMMITMENT, customConfig) => (0, exports.getConnection)((0, exports.getClusterUrl)(cluster, customConfig), preflightCommitment);
exports.getConnectionByCluster = getConnectionByCluster;
const getConnection = (clusterUrl, preflightCommitment = const_1.SOLANA_COMMITMENT) => new web3_js_1.Connection(clusterUrl, preflightCommitment);
exports.getConnection = getConnection;
function addHashOrNonce(transaction, hashOrNonce) {
return __awaiter(this, void 0, void 0, function* () {
if (hashOrNonce === 'find') {
transaction.transaction.recentBlockhash = yield transaction.connection
.getLatestBlockhash()
.then((rbh) => rbh.blockhash);
}
else if ('recentBlockhash' in hashOrNonce) {
transaction.transaction.recentBlockhash = hashOrNonce.recentBlockhash;
}
else {
transaction.transaction.nonceInfo = hashOrNonce.nonce;
}
});
}
/**
* from `@solana/web3.js`
*/
class SendableTransaction {
constructor(connection, transaction) {
this.connection = connection;
this.transaction = transaction;
}
withData(data) {
return new SendableDataTransaction(this, normalizeDataCallback(data));
}
send() {
return __awaiter(this, arguments, void 0, function* (options = {}, ...extraSigners) {
if (extraSigners.length) {
this.partialSign(...extraSigners);
}
const fullOptions = Object.assign({ preflightCommitment: const_1.SOLANA_COMMITMENT }, options);
const txSig = yield this.connection.sendRawTransaction(this.transaction.serialize(), fullOptions);
return new SentTransaction(this.connection, txSig);
});
}
static fromSerialized(connection, message) {
return new SendableTransaction(connection, web3_js_1.Transaction.from(message));
}
partialSign(...signers) {
this.transaction.partialSign(...signers);
return this;
}
addHashOrNonce(hashOrNonce) {
return __awaiter(this, void 0, void 0, function* () {
yield addHashOrNonce(this, hashOrNonce);
return this;
});
}
feePayer(feePayer) {
this.transaction.feePayer = feePayer;
return this;
}
}
exports.SendableTransaction = SendableTransaction;
class SendableDataTransaction {
constructor(sendableTransaction, data) {
this.sendableTransaction = sendableTransaction;
this.data = data;
}
get connection() {
return this.sendableTransaction.connection;
}
get transaction() {
return this.sendableTransaction.transaction;
}
send() {
return __awaiter(this, arguments, void 0, function* (options = {}, ...extraSigners) {
return this.sendableTransaction
.send(options, ...extraSigners)
.then((t) => t.withData(this.data));
});
}
addHashOrNonce(hashOrNonce) {
return __awaiter(this, void 0, void 0, function* () {
yield addHashOrNonce(this, hashOrNonce);
return this;
});
}
partialSign(...signers) {
this.transaction.partialSign(...signers);
return this;
}
feePayer(feePayer) {
this.transaction.feePayer = feePayer;
return this;
}
}
exports.SendableDataTransaction = SendableDataTransaction;
// checks if the input is a data callback or purely data.
const isGeneralDataCallback = (data) => typeof data === 'function';
// Convert the input callback into a standardised function that returns a promise of data
const normalizeDataCallback = (d) => {
if (isGeneralDataCallback(d)) {
return () => Promise.resolve(d());
}
else {
return () => Promise.resolve(d);
}
};
class SentTransaction {
constructor(connection, signature) {
this.connection = connection;
this.signature = signature;
}
withData(data) {
return new SentDataTransaction(this, normalizeDataCallback(data));
}
confirm(commitment, errorCallback) {
return __awaiter(this, void 0, void 0, function* () {
const blockhash = yield this.connection.getLatestBlockhash();
const result = yield this.connection.confirmTransaction(Object.assign({ signature: this.signature }, blockhash));
if (result.value.err) {
if (errorCallback) {
errorCallback(result.value.err);
}
else {
throw new Error(`Error confirming transaction: ${result.value.err}`);
}
}
});
}
}
exports.SentTransaction = SentTransaction;
class SentDataTransaction {
constructor(sentTransaction, data) {
this.sentTransaction = sentTransaction;
this.data = data;
}
get signature() {
return this.sentTransaction.signature;
}
confirm(commitment, errorCallback) {
return __awaiter(this, void 0, void 0, function* () {
yield this.sentTransaction.confirm(commitment, errorCallback);
return this.data instanceof Function ? yield this.data() : this.data;
});
}
}
exports.SentDataTransaction = SentDataTransaction;
//# sourceMappingURL=connection.js.map