@akashicpay/sdk
Version:
SDK to interact with the Akashic ecosystem
268 lines • 11 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AkashicChain = exports.TestNetContracts = exports.AkashicChainContracts = exports.nitr0genNativeCoin = exports.L2Regex = void 0;
exports.checkForAkashicChainError = checkForAkashicChainError;
exports.getACSymbol = getACSymbol;
exports.getACNetwork = getACNetwork;
const sdk_1 = require("@activeledger/sdk");
const constants_1 = require("./constants");
const error_1 = require("./error");
const l1Network_1 = require("./l1Network");
const types_1 = require("./types");
const currency_1 = require("./utils/currency");
exports.L2Regex = /^AS[A-Fa-f\d]{64}$/;
exports.nitr0genNativeCoin = '#native';
var AkashicChainContracts;
(function (AkashicChainContracts) {
AkashicChainContracts["Namespace"] = "akashicchain";
AkashicChainContracts["Create"] = "50e1372f0d3805dac4a51299bb0e99960862d7d01f247e85725d99011682b8ac@1";
AkashicChainContracts["CryptoTransfer"] = "2bae6ea681826c0307ee047ef68eb0cf53487a257c498de7d081d66de119d666@1";
AkashicChainContracts["DiffConsensus"] = "94479927cbe0860a3f51cbd36230faef7d1b69974323a83c8abcc78e3d0e8dd9@1";
AkashicChainContracts["Onboard"] = "a456ddc07da6d46a6897d24de188e767b87a9d9f2f3c617d858aaf819e0e5bce@1";
AkashicChainContracts["AssignKey"] = "7afea15e8028af9f5aeafb6db6c0d2e8969c0c0492360ab15a6bc3754b818e19@1";
})(AkashicChainContracts || (exports.AkashicChainContracts = AkashicChainContracts = {}));
var TestNetContracts;
(function (TestNetContracts) {
TestNetContracts["Namespace"] = "akashicchain";
TestNetContracts["Create"] = "ad171259a7c628ba6993c6bd555f07111525128194aa4226662e48a0b0a93116@1";
TestNetContracts["CreateSecondaryOtk"] = "3030f7c2bda3a02330ef3bfd9a1a1f66fec4a96c0c04c019b64255a4e8ba31ca@1";
TestNetContracts["CryptoTransfer"] = "a32a8bc21ceaeeaa671573126a246c15ec4dc3a5c825e3cffc9441636019acb1@1";
TestNetContracts["DiffConsensus"] = "17be1db84dbf81c1ff1b2f5aebd4ba4e95d81338daf98d7c2bc7b54ad8994d1c@1";
TestNetContracts["Onboard"] = "c19c6f4d3c443ae7abb14d17d33b29d134df8d11bdabc568bd23f7023ee991fd@1";
TestNetContracts["AssignKey"] = "a6e95e2f563bdac69bfa265b1c215bf2125e1c50048f68f9c0b52982e320d675@1";
})(TestNetContracts || (exports.TestNetContracts = TestNetContracts = {}));
function checkForAkashicChainError(response) {
var _a, _b;
if (response.$summary.commit) {
return;
}
const errorMessage = convertChainErrorToAkashicError(((_b = (_a = response.$summary) === null || _a === void 0 ? void 0 : _a.errors) === null || _b === void 0 ? void 0 : _b[0]) || '');
if (Object.values(error_1.AkashicErrorCode).includes(errorMessage))
return errorMessage;
throw new error_1.AkashicError(error_1.AkashicErrorCode.UnknownError, `AkashicChain Failure: ${errorMessage}`);
}
function getACSymbol(coinSymbol) {
if (Object.values(types_1.TronSymbol).includes(coinSymbol)) {
return 'trx';
}
else if (Object.values(types_1.EthereumSymbol).includes(coinSymbol)) {
return 'eth';
}
return coinSymbol;
}
function getACNetwork(coinSymbol) {
switch (coinSymbol) {
case types_1.NetworkSymbol.Ethereum_Mainnet:
return 'ETH';
case types_1.NetworkSymbol.Ethereum_Sepolia:
return 'SEP';
case types_1.NetworkSymbol.Tron:
return 'trx';
case types_1.NetworkSymbol.Tron_Shasta:
return 'shasta';
default:
return coinSymbol;
}
}
function convertChainErrorToAkashicError(error) {
switch (true) {
case isChainErrorSavingsExceeded(error):
return error_1.AkashicErrorCode.SavingsExceeded;
case String(error).includes('Stream(s) not found'):
return error_1.AkashicErrorCode.L2AddressNotFound;
default:
return error;
}
}
function isChainErrorSavingsExceeded(error) {
return [
'balance is not sufficient',
"Couldn't parse integer",
'Part-Balance to low',
].some((msg) => String(error).includes(msg));
}
class AkashicChain {
constructor(env) {
this.contracts =
env === constants_1.Environment.Production ? AkashicChainContracts : TestNetContracts;
this.dbIndex = env === constants_1.Environment.Production ? 0 : 15;
this.fxMultiSignIdentity =
env === constants_1.Environment.Production
? 'ASad1414566948845b404e8b6ac91639cc3643129d0ef8b7828ede7a0ac1044d6e'
: 'ASeffcb8790aff2439522ef4bd834cca5233dc1670e5fa1c93fa19305323937a17';
}
async keyCreateTransaction(coinSymbol, otk) {
const txBody = {
$tx: {
$namespace: this.contracts.Namespace,
$contract: this.contracts.Create,
$i: {
owner: {
$stream: otk.identity,
symbol: getACSymbol(coinSymbol),
network: getACNetwork(coinSymbol),
business: true,
},
},
_dbIndex: this.dbIndex,
},
$sigs: {},
};
this.addExpireToTxBody(txBody);
const txHandler = new sdk_1.TransactionHandler();
return await txHandler.signTransaction(txBody, otk);
}
async differentialConsensusTransaction(otk, key, identifier) {
const txBody = {
$tx: {
$namespace: this.contracts.Namespace,
$contract: this.contracts.DiffConsensus,
$i: {
owner: {
publicKey: otk.key.pub.pkcs8pem,
type: otk.type,
address: key.address,
hashes: key.hashes,
},
},
$o: {
key: {
$stream: key.id,
},
},
_dbIndex: this.dbIndex,
metadata: { identifier },
},
$sigs: {},
$selfsign: true,
$unanimous: true,
};
this.addExpireToTxBody(txBody);
const txHandler = new sdk_1.TransactionHandler();
return await txHandler.signTransaction(txBody, Object.assign(Object.assign({}, otk), { identity: 'owner' }));
}
async onboardOtkTransaction(otk) {
const txBody = {
$tx: {
$namespace: this.contracts.Namespace,
$contract: this.contracts.Onboard,
$i: {
otk: {
publicKey: otk.key.pub.pkcs8pem,
type: otk.type,
},
},
_dbIndex: this.dbIndex,
},
$sigs: {},
$selfsign: true,
};
this.addExpireToTxBody(txBody);
const txHandler = new sdk_1.TransactionHandler();
return await txHandler.signTransaction(txBody, otk);
}
async l2Transaction({ otk, coinSymbol, amount, toAddress, tokenSymbol, initiatedToNonL2, identifier, }, isFxbp = false) {
const metadata = { identifier };
if (initiatedToNonL2) {
metadata.initiatedToNonL2 = initiatedToNonL2;
}
const txBody = {
$tx: {
$namespace: this.contracts.Namespace,
$contract: this.contracts.CryptoTransfer,
$entry: 'transfer',
$i: Object.assign({ owner: {
$stream: otk.identity,
network: coinSymbol,
token: tokenSymbol !== null && tokenSymbol !== void 0 ? tokenSymbol : exports.nitr0genNativeCoin,
amount: amount,
} }, (isFxbp && {
afx: {
$stream: this.fxMultiSignIdentity,
$sigOnly: true,
},
})),
$o: {
to: { $stream: toAddress },
},
_dbIndex: this.dbIndex,
metadata,
},
$sigs: {},
};
this.addExpireToTxBody(txBody);
const txHandler = new sdk_1.TransactionHandler();
return await txHandler.signTransaction(txBody, otk);
}
async buildPayoutTransaction({ otk, keyLedgerId, coinSymbol, amount, toAddress, tokenSymbol, identifier, feesEstimate, ethGasPrice, }) {
var _a;
const contractAddress = (_a = l1Network_1.NetworkDictionary[coinSymbol].tokens.find((t) => t.symbol === tokenSymbol)) === null || _a === void 0 ? void 0 : _a.contract;
const $o = (0, currency_1.isNetworkSymbol)(coinSymbol, types_1.EthereumSymbol)
? { [keyLedgerId]: {} }
: undefined;
return {
$tx: {
$namespace: this.contracts.Namespace,
$contract: this.contracts.CryptoTransfer,
$entry: 'sign',
$i: {
owner: {
$stream: otk.identity,
network: coinSymbol,
token: tokenSymbol !== null && tokenSymbol !== void 0 ? tokenSymbol : exports.nitr0genNativeCoin,
amount: amount,
to: toAddress,
contractAddress,
delegated: true,
gas: ethGasPrice,
},
},
$o,
$r: {
wallet: keyLedgerId,
},
_dbIndex: this.dbIndex,
metadata: { identifier, feesEstimate },
},
$sigs: {},
};
}
async signPayoutTransaction(txn, otk) {
this.addExpireToTxBody(txn);
const txHandler = new sdk_1.TransactionHandler();
return await txHandler.signTransaction(txn, otk);
}
addExpireToTxBody(txBody) {
if (!txBody.$tx.$expire)
txBody.$tx.$expire = new Date(Date.now() + 60 * 1000).toISOString();
return txBody;
}
async assign(ledgerId, otk, identifier) {
const txBody = {
$tx: {
$namespace: this.contracts.Namespace,
$contract: this.contracts.AssignKey,
$i: {
owner: {
$stream: otk.identity,
},
},
$o: {
key: {
$stream: ledgerId,
},
},
metadata: {
identifier,
},
_dbIndex: this.dbIndex,
},
$sigs: {},
};
this.addExpireToTxBody(txBody);
const txHandler = new sdk_1.TransactionHandler();
return await txHandler.signTransaction(txBody, otk);
}
}
exports.AkashicChain = AkashicChain;
//# sourceMappingURL=akashicChain.js.map