Snippets
Created by
Riccardo Sibani
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | import {
ActionInfo, ActionMatch, ContractDefinition, ContractInstance, EsplixContext, FieldInfo, MultiSigState,
ParameterInfo, Parameters, ProcessedMessage, PValue, PValueArray, RType
} from "esplix/types/index";
import { isArray } from "util";
export class ContextManager {
public static context?: EsplixContext;
public static _init(context: EsplixContext) {
ContextManager.context = context;
}
public static createContract(contractDefinition: ContractDefinition,
parameters: Parameters): Promise<ContractInstance> {
return this.context!.contractInstanceManager.createContractInstance(contractDefinition, parameters);
}
public static getContracts(): ContractInstance[] {
if (this.context) {
return this.context.contractInstanceManager.getContractInstances();
}
return [];
}
public static getContractActions(contract: ContractInstance, pubkeys: any): ActionInfo[] {
return contract.getApplicableActions().map(
(match) => contract.getActionInfo(match.name)
);
}
public static contractPerformAction(contract: ContractInstance, action: string, params: Parameters): any {
return contract.performAction(action, params);
}
public static getContractDefinitions(): ContractDefinition[] {
if (this.context) {
return this.context.contractDefinitionManager.getAllDefinitions();
}
return [];
}
}
export class AddressBook {
public addresses: AddressBookEntry[] = [];
public addAddress(ae: AddressBookEntry) {
this.addresses.push(ae);
}
public getAddresses(): AddressBookEntry[] {
return this.addresses;
}
public searchAddressByPubkey(pubkey: string): AddressBookEntry | null {
for (let i = 0; i < this.addresses.length; i++) {
if (pubkey === this.addresses[i].pubkey) {
return this.addresses[i];
}
}
return null;
}
}
export interface AddressBookEntry {
name: string;
pubkey: string;
}
export interface EsplixKeyPair { // TODO: move to Esplix
pubKey: string;
privKey: string;
}
export interface ParameterSimpleProps {
actionInfo: ActionInfo | null;
paramInfo: ParameterInfo;
name: string;
setParameterValue: (key: string, value: any) => null;
}
export interface ParameterCompositeProps extends ParameterSimpleProps {
context: EsplixContext; // Esplix context of app
contract: ContractInstance; // Current contract
}
export interface FileSource {
fieldName: string;
action: ActionInfo;
}
export class Helper {
public static typeIsArray(type: RType): boolean {
return (!type.isPrimitive) && (isArray(type.typeSpecifier)) && type.typeSpecifier![0] === "ARRAY";
}
public static valueIsPubkey(pubkey: string): boolean {
const pk: Buffer = Buffer.from(pubkey, "hex");
if (pk && pk.length === 33) {
return true;
} else {
return false;
}
}
public static resolvePubkey(pubkey: Buffer, addressBook: AddressBook): string {
const entry = addressBook.searchAddressByPubkey(pubkey.toString("hex"));
if (entry) {
return entry.name;
} else {
return pubkey.toString("hex");
}
}
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.