@neo-one/smart-contract-compiler
Version:
NEO•ONE TypeScript smart contract compiler.
79 lines (77 loc) • 3.02 kB
JavaScript
import { tsUtils } from '@neo-one/ts-utils';
import { Types } from '../../constants';
import { BuiltinInterface } from '../BuiltinInterface';
import { BuiltinMemberCall } from '../BuiltinMemberCall';
import { BuiltinValueObject } from '../BuiltinValueObject';
class CryptoInterface extends BuiltinInterface {
}
class CryptoValue extends BuiltinValueObject {
constructor() {
super(...arguments);
this.type = 'CryptoConstructor';
}
}
class HashOp extends BuiltinMemberCall {
constructor(op) {
super();
this.op = op;
}
emitCall(sb, _func, node, optionsIn) {
const options = sb.pushValueOptions(optionsIn);
const args = tsUtils.argumented.getArguments(node);
if (args.length === 0) {
return;
}
const arg = args[0];
const throwTypeError = (innerOptions) => {
sb.emitOp(arg, 'DROP');
sb.emitHelper(arg, innerOptions, sb.helpers.throwTypeError);
};
const unwrap = (type) => (innerOptions) => {
sb.emitHelper(arg, innerOptions, sb.helpers.unwrapVal({ type }));
};
sb.visit(arg, options);
sb.emitHelper(node, options, sb.helpers.forBuiltinType({
type: sb.context.analysis.getType(arg),
array: throwTypeError,
map: throwTypeError,
set: throwTypeError,
boolean: unwrap(Types.Boolean),
buffer: unwrap(Types.Buffer),
null: throwTypeError,
number: unwrap(Types.Number),
object: throwTypeError,
string: unwrap(Types.String),
symbol: throwTypeError,
undefined: throwTypeError,
arrayStorage: throwTypeError,
mapStorage: throwTypeError,
setStorage: throwTypeError,
error: throwTypeError,
forwardValue: throwTypeError,
iteratorResult: throwTypeError,
iterable: throwTypeError,
iterableIterator: throwTypeError,
transaction: throwTypeError,
output: throwTypeError,
attribute: throwTypeError,
input: throwTypeError,
account: throwTypeError,
asset: throwTypeError,
contract: throwTypeError,
header: throwTypeError,
block: throwTypeError,
}));
sb.emitOp(node, this.op);
sb.emitHelper(node, optionsIn, sb.helpers.wrapBuffer);
}
}
export const add = (builtins) => {
builtins.addContractInterface('CryptoConstructor', new CryptoInterface());
builtins.addContractMember('CryptoConstructor', 'sha1', new HashOp('SHA1'));
builtins.addContractMember('CryptoConstructor', 'sha256', new HashOp('SHA256'));
builtins.addContractMember('CryptoConstructor', 'hash160', new HashOp('HASH160'));
builtins.addContractMember('CryptoConstructor', 'hash256', new HashOp('HASH256'));
builtins.addContractValue('crypto', new CryptoValue());
};
//# sourceMappingURL=crypto.js.map