import * as env from "./env"; import { Encoder, Decoder, Packer } from "./serializer"; import { Name } from "./name"; import { calcPackedVarUint32Length, VarUint32 } from "./varint"; import { Checksum256 } from "./crypto"; export function getSender(): Name { return new Name(env.get_sender()); } export function readActionData(): u8[] { let size = env.action_data_size(); var arr = new Array(size); let ptr = arr.dataStart; env.read_action_data(ptr, size); return arr; } export function unpackActionData(): T { let data = readActionData() let ret = instantiate() ret.unpack(data) return ret } export function actionDataSize(): u32 { return env.action_data_size(); } export function requireRecipient(name: Name): void { env.require_recipient(name.N); } export function requireAuth(name: Name): void { env.require_auth(name.N); } export function hasAuth(name: Name): bool { return env.has_auth(name.N); } export function requireAuth2(permissionLevel: PermissionLevel): void { env.require_auth2(permissionLevel.actor.N, permissionLevel.permission.N); } export function isAccount(name: Name): bool { return env.is_account(name.N); } export function publicationTime(): u64 { return env.publication_time(); } export function currentReceiver(): Name { return new Name(env.current_receiver()); } export function setActionReturnValue(returnValue: u8[]): void { env.set_action_return_value(returnValue.dataStart, returnValue.length); } export class PermissionLevel implements Packer { constructor( public actor: Name = new Name(), public permission: Name = Name.fromString("active")) { } pack(enc: Encoder): usize { enc.packName(this.actor); enc.packName(this.permission); return this.getSize(); } unpack(data: u8[]): usize { let dec = new Decoder(data); this.actor = dec.unpackName(); this.permission = dec.unpackName(); return dec.getPos(); } getSize(): usize { return 8*2; } } export class Action implements Packer{ constructor( public account: Name = new Name(), public name: Name = new Name(), public authorization: PermissionLevel[] = [], public data: u8[] = [], ) {} static new( account: Name, name: Name, authorization: PermissionLevel[], packer: Packer): Action { return new Action(account, name, authorization, Encoder.pack(packer)); } send(): void { let data = Encoder.pack(this); env.send_inline(data.dataStart, data.length); } pack(enc: Encoder): usize { let oldPos = enc.getPos(); enc.packName(this.account); enc.packName(this.name); enc.packLength(this.authorization.length); for (let i = 0; i(this.data); return enc.getPos() - oldPos; } unpack(data: u8[]): usize { let dec = new Decoder(data); this.account = dec.unpackName(); this.name = dec.unpackName(); let length = dec.unpackLength(); this.authorization = new Array(length); for (let i=0; i(); return dec.getPos(); } getSize(): usize { let size: usize = 0; size += calcPackedVarUint32Length(this.authorization.length); size += this.authorization.length * 16; size += 16; size += calcPackedVarUint32Length(this.data.length); size += this.data.length; return size; } } export class GetCodeHashResult implements Packer { constructor( public structVersion: VarUint32 = new VarUint32(0), public codeSequence: u64 = 0, public codeHash: Checksum256 = new Checksum256(), public vmType: u8 = 0, public vmVersion: u8 = 0 ) {} pack(enc: Encoder): usize { let oldPos = enc.getPos() this.structVersion.pack(enc); enc.packNumber(this.codeSequence); this.codeHash.pack(enc); enc.packNumber(this.vmType); enc.packNumber(this.vmVersion); return enc.getPos() - oldPos; } unpack(data: u8[]): usize { let dec = new Decoder(data); this.structVersion = new VarUint32(); dec.unpack(this.structVersion); this.codeSequence = dec.unpackNumber(); this.codeHash = new Checksum256(); dec.unpack(this.codeHash); this.vmType = dec.unpackNumber(); this.vmVersion = dec.unpackNumber(); return dec.getPos(); } getSize(): usize { let size: usize = 0; size += this.structVersion.getSize(); size += sizeof(); size += this.codeHash.getSize(); size += sizeof(); size += sizeof(); return size; } } export function getCodeHash(account: Name): GetCodeHashResult { const result = new GetCodeHashResult(); const rawResult = new Array((result.getSize())); env.get_code_hash(account.N, 0, rawResult.dataStart); result.unpack(rawResult); return result; }