@pocinnovation/alumi
Version:
Dynamic pulumi provider for Aleph
214 lines (213 loc) • 8.07 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.zipPath = exports.getRawFileUrl = exports.getAlephExplorerUrl = exports.hashData = exports.getAccount = void 0;
const avalanche_1 = require("@aleph-sdk/avalanche");
// import { importAccountFromPrivateKey as accSolanaPk } from '@aleph-sdk/solana';
const tezos_1 = require("@aleph-sdk/tezos");
const ethereum_1 = require("@aleph-sdk/ethereum");
const cosmos_1 = require("@aleph-sdk/cosmos");
const nuls2_1 = require("@aleph-sdk/nuls2");
const substrate_1 = require("@aleph-sdk/substrate");
const crypto = __importStar(require("crypto"));
const core_1 = require("@aleph-sdk/core");
const fs_1 = require("fs");
const os_1 = require("os");
const path_1 = require("path");
const archiver_1 = __importDefault(require("archiver"));
const getAccount = (envName) => __awaiter(void 0, void 0, void 0, function* () {
if (envName === undefined) {
throw new Error('Missing env');
}
const envVal = process.env[envName];
if (envVal === undefined || envVal === '') {
throw new Error(`Missing env ${envName} / or empty`);
}
const splits = envVal.split(':');
if (splits.length !== 3) {
throw new Error(`Invalid env ${envName}: ${envVal}`);
}
const chain = splits[0];
const method = splits[1];
const val = splits[2];
switch (chain) {
case 'AVALANCHE': {
switch (method) {
case 'PRIVATEKEY': {
return yield (0, avalanche_1.importAccountFromPrivateKey)(val);
}
default: {
throw new Error(`invalid method ${method} for ${chain}`);
}
}
}
// case 'SOLANA': {
// switch (method) {
// case 'PRIVATEKEY': {
// return await accSolanaPk(val);
// }
// default: {
// throw new Error(`invalid method ${method} for ${chain}`);
// }
// }
// }
case 'TEZOS': {
switch (method) {
case 'PRIVATEKEY': {
return yield (0, tezos_1.importAccountFromPrivateKey)(val);
}
default: {
throw new Error(`invalid method ${method} for ${chain}`);
}
}
}
case 'ETHERUM': {
switch (method) {
case 'MNEMONIK': {
return (0, ethereum_1.importAccountFromMnemonic)(val);
}
case 'PRIVATEKEY': {
return (0, ethereum_1.importAccountFromPrivateKey)(val);
}
default: {
throw new Error(`invalid method ${method} for ${chain}`);
}
}
}
case 'COSMOS': {
switch (method) {
case 'MNEMONIK': {
return yield (0, cosmos_1.importAccountFromMnemonic)(val);
}
case 'PRIVATEKEY': {
return yield (0, cosmos_1.importAccountFromPrivateKey)(val);
}
default: {
throw new Error(`invalid method ${method} for ${chain}`);
}
}
}
case 'NULS2': {
switch (method) {
case 'MNEMONIK': {
return yield (0, nuls2_1.importAccountFromMnemonic)(val);
}
case 'PRIVATEKEY': {
return yield (0, nuls2_1.importAccountFromPrivateKey)(val);
}
default: {
throw new Error(`invalid method ${method} for ${chain}`);
}
}
}
case 'SUBSTRATE': {
switch (method) {
case 'MNEMONIK': {
return yield (0, substrate_1.importAccountFromMnemonic)(val);
}
case 'PRIVATEKEY': {
return yield (0, substrate_1.importAccountFromPrivateKey)(val);
}
default: {
throw new Error(`invalid method ${method} for ${chain}`);
}
}
}
default: {
throw new Error(`invalid chain ${chain}`);
}
}
});
exports.getAccount = getAccount;
const hashData = (s) => {
return crypto.createHash('sha512').update(s).digest('hex');
};
exports.hashData = hashData;
const getAlephExplorerUrl = (chain, sender, type, item_hash) => {
return ('https://explorer.aleph.im/address/' +
encodeURIComponent(chain) +
'/' +
encodeURIComponent(sender) +
'/message/' +
encodeURIComponent(type) +
'/' +
encodeURIComponent(item_hash));
};
exports.getAlephExplorerUrl = getAlephExplorerUrl;
const getRawFileUrl = (content_item_hash) => {
return core_1.DEFAULT_API_V2 + encodeURIComponent(content_item_hash);
};
exports.getRawFileUrl = getRawFileUrl;
const zipPath = (key, path) => __awaiter(void 0, void 0, void 0, function* () {
if (path.endsWith('.zip')) {
return path;
}
const outputPath = (0, path_1.join)((0, os_1.tmpdir)(), 'alumi', `${key}.zip`);
(0, fs_1.mkdirSync)((0, path_1.dirname)(outputPath), { recursive: true });
const output = (0, fs_1.createWriteStream)(outputPath);
const archive = (0, archiver_1.default)('zip', {
zlib: { level: 8 },
});
archive.on('warning', function (err) {
if (err.code === 'ENOENT') {
console.log(err);
}
else {
throw err;
}
});
archive.on('error', function (err) {
throw err;
});
archive.pipe(output);
const fileStat = (0, fs_1.lstatSync)(path);
if (fileStat.isDirectory()) {
archive.directory(path, '/', { prefix: '/' });
}
else if (fileStat.isFile()) {
archive.file(path, { name: (0, path_1.basename)(path) });
}
else {
throw new Error(`invalid path ${path}`);
}
yield archive.finalize();
while (!output.closed) {
yield new Promise(r => setTimeout(r, 100));
}
return outputPath;
});
exports.zipPath = zipPath;