Comparing version 0.1.0-rc.13 to 0.1.0-rc.14
@@ -15,3 +15,3 @@ /// <reference types="node" /> | ||
*/ | ||
export declare type Schema = 'void' | 'u8' | 'i8' | 'u16' | 'i16' | 'u32' | 'i32' | 'u64' | 'i64' | 'u128' | 'i128' | 'f32' | 'f64' | 'boolean' | 'string' | [Schema, number] | Schema[] | ['Option', Schema] | ['Set', Schema] | ['Map', Schema, Schema] | AbiEncodable | AbiDecodable; | ||
export declare type Schema = 'void' | 'u8' | 'i8' | 'u16' | 'i16' | 'u32' | 'i32' | 'u64' | 'i64' | 'u128' | 'i128' | 'f32' | 'f64' | 'boolean' | 'string' | [Schema, number] | Schema[] | ['Set', Schema] | ['Map', Schema, Schema] | ['Option', Schema] | ['Result', Schema, Schema] | AbiEncodable | AbiDecodable; | ||
/** | ||
@@ -18,0 +18,0 @@ * ABI encodes an object according to the provided schema. |
@@ -5,3 +5,37 @@ "use strict"; | ||
const js_sha3_1 = require("js-sha3"); | ||
const types_1 = require("./types"); | ||
const utils_1 = require("./utils"); | ||
function stringifySchema(schema) { | ||
if (typeof schema === 'string') { | ||
return schema; | ||
} | ||
if (Array.isArray(schema)) { | ||
if (schema[0] === 'Set') { | ||
return `Set<${stringifySchema(schema[1])}>`; | ||
} | ||
if (schema[0] === 'Map') { | ||
const kTy = stringifySchema(schema[1]); | ||
const vTy = stringifySchema(schema[2]); | ||
return `Map<${kTy}, ${vTy}>`; | ||
} | ||
if (schema[0] === 'Option') { | ||
return `(${stringifySchema(schema[1])} | undefined)`; | ||
} | ||
if (schema[0] === 'Result') { | ||
const okTy = stringifySchema(schema[1]); | ||
const errTy = stringifySchema(schema[2]); | ||
return `Result<${okTy}, ${errTy}>`; | ||
} | ||
if (schema.length === 2 && typeof schema[1] === 'number') { | ||
// array | ||
const count = schema[1]; | ||
if (count === Number.POSITIVE_INFINITY) { | ||
return `${stringifySchema(schema[0])}[]`; | ||
} | ||
return `${stringifySchema(schema[0])}[${count}]`; | ||
} | ||
return `(${schema.map(stringifySchema).join(', ')})`; // tuple | ||
} | ||
return schema.constructor.name; | ||
} | ||
/** | ||
@@ -17,7 +51,8 @@ * ABI encodes an object according to the provided schema. | ||
exports.abiEncode = abiEncode; | ||
function doAbiEncode(schema, obj, encoder, errTy) { | ||
var _a, _b, _c, _d, _e, _f; | ||
const throwUnexpectedType = (ty) => { | ||
function doAbiEncode(schema, obj, encoder, parentTy) { | ||
var _a, _b, _c, _d, _e, _f, _g, _h; | ||
const throwUnexpectedType = () => { | ||
const got = obj === null ? 'null' : obj.constructor.name; | ||
const expected = typeof errTy !== 'undefined' ? errTy(ty) : ty; | ||
const schemaStr = stringifySchema(schema); | ||
const expected = typeof parentTy !== 'undefined' ? parentTy(schemaStr) : schemaStr; | ||
throw new Error(`unexpected type \`${got}\`. expected \`${expected}\``); | ||
@@ -31,3 +66,3 @@ }; | ||
} | ||
throwUnexpectedType(ty); | ||
throwUnexpectedType(); | ||
} | ||
@@ -39,3 +74,3 @@ if (ty === 'boolean') { | ||
else { | ||
throwUnexpectedType(ty); | ||
throwUnexpectedType(); | ||
} | ||
@@ -48,7 +83,7 @@ } | ||
else { | ||
throwUnexpectedType(ty); | ||
throwUnexpectedType(); | ||
} | ||
} | ||
if (typeof obj !== 'number' && typeof obj !== 'bigint') { | ||
throwUnexpectedType(ty); | ||
throwUnexpectedType(); | ||
} | ||
@@ -65,3 +100,3 @@ if (ty === 'u64') | ||
/* istanbul ignore next */ | ||
throwUnexpectedType(ty); | ||
throwUnexpectedType(); | ||
} | ||
@@ -109,3 +144,3 @@ if (ty === 'u8') | ||
if (!(obj instanceof Set)) { | ||
throwUnexpectedType(`Set<${ty}>`); | ||
throwUnexpectedType(); | ||
} | ||
@@ -130,2 +165,17 @@ const entries = []; | ||
} | ||
else if (schema[0] === 'Result') { | ||
const [_, okTy, errTy] = schema; | ||
if (typeof ((_a = obj) === null || _a === void 0 ? void 0 : _a.isOk) !== 'function' && | ||
typeof ((_b = obj) === null || _b === void 0 ? void 0 : _b.isErr) !== 'function') { | ||
throwUnexpectedType(); | ||
} | ||
if (obj.isErr()) { | ||
encoder.writeU8(0); | ||
abiEncode(errTy, obj.unwrapErr(), encoder); | ||
} | ||
else if (obj.isOk()) { | ||
encoder.writeU8(1); | ||
abiEncode(okTy, obj.unwrap(), encoder); | ||
} | ||
} | ||
else if (schema.length === 2 && typeof schema[1] === 'number') { | ||
@@ -140,3 +190,3 @@ // array | ||
else if (obj.length !== count) { | ||
throw new Error(`expected \`${ty}[]\` of length ${count}. got ${obj.length}.`); | ||
throw new Error(`expected \`${stringifySchema(schema)}\`, but had length ${obj.length}.`); | ||
} | ||
@@ -150,3 +200,3 @@ if (Array.isArray(obj)) { | ||
if (!buffer_1.Buffer.isBuffer(obj) && typedArraySchemaType(obj) !== ty) { | ||
throwUnexpectedType(ty + '[]'); | ||
throwUnexpectedType(); | ||
} | ||
@@ -184,3 +234,3 @@ if (ty == 'u8') { | ||
/* istanbul ignore next */ | ||
throwUnexpectedType(ty + '[]'); | ||
throwUnexpectedType(); | ||
} | ||
@@ -191,3 +241,3 @@ } | ||
if (!Array.isArray(obj)) { | ||
throwUnexpectedType(`tuple(${JSON.stringify(schema)})`); | ||
throwUnexpectedType(); | ||
} | ||
@@ -202,3 +252,3 @@ else if (obj.length !== schema.length) { | ||
} | ||
else if (typeof ((_a = obj) === null || _a === void 0 ? void 0 : _a.abiEncode) === 'function') { | ||
else if (typeof ((_c = obj) === null || _c === void 0 ? void 0 : _c.abiEncode) === 'function') { | ||
return obj.abiEncode(encoder); | ||
@@ -208,3 +258,3 @@ } | ||
/* istanbul ignore next */ | ||
throw new Error(`invalid object for schema: expected \`${_f = (_c = (_b = schema) === null || _b === void 0 ? void 0 : _b.name, (_c !== null && _c !== void 0 ? _c : (_e = (_d = schema) === null || _d === void 0 ? void 0 : _d.constructor) === null || _e === void 0 ? void 0 : _e.name)), (_f !== null && _f !== void 0 ? _f : JSON.stringify(schema))}\`, but got \`${JSON.stringify(obj)}\`.`); | ||
throw new Error(`invalid object for schema: expected \`${_h = (_e = (_d = schema) === null || _d === void 0 ? void 0 : _d.name, (_e !== null && _e !== void 0 ? _e : (_g = (_f = schema) === null || _f === void 0 ? void 0 : _f.constructor) === null || _g === void 0 ? void 0 : _g.name)), (_h !== null && _h !== void 0 ? _h : JSON.stringify(schema))}\`, but got \`${JSON.stringify(obj)}\`.`); | ||
} | ||
@@ -288,2 +338,7 @@ } | ||
} | ||
if (schema[0] === 'Result') { | ||
return decoder.readBoolean() | ||
? new types_1.Result.Ok(doAbiDecode(schema[1], decoder)) | ||
: new types_1.Result.Err(doAbiDecode(schema[2], decoder)); | ||
} | ||
// array | ||
@@ -290,0 +345,0 @@ if (schema.length === 2 && typeof schema[1] === 'number') { |
@@ -180,2 +180,3 @@ "use strict"; | ||
yield __await(yield* __asyncDelegator(__asyncValues(this.events))); | ||
this.events = []; | ||
} | ||
@@ -182,0 +183,0 @@ yield __await(this.hasResults); |
export { AbiDecodable, AbiEncodable, Decoder, Encoder, Schema, abiDecode, abiEncode, encodeEventTopic, } from './abi'; | ||
export * from './gateway'; | ||
export { Address, Balance, ExecutionError } from './types'; | ||
export { Address, Balance, ExecutionError, Result } from './types'; | ||
export { decodeHex, encodeHex, fetchBytecode } from './utils'; |
@@ -17,2 +17,3 @@ "use strict"; | ||
exports.ExecutionError = types_1.ExecutionError; | ||
exports.Result = types_1.Result; | ||
var utils_1 = require("./utils"); | ||
@@ -19,0 +20,0 @@ exports.decodeHex = utils_1.decodeHex; |
@@ -42,2 +42,29 @@ import { AbiEncodable, Encoder, Decoder } from './abi'; | ||
} | ||
export declare namespace Result { | ||
interface Result<T, E> { | ||
isOk(): boolean; | ||
isErr(): boolean; | ||
unwrap(): T; | ||
unwrapErr(): E; | ||
} | ||
export class Ok<T, E> implements Result<T, E> { | ||
private val; | ||
constructor(val: T); | ||
isOk(): boolean; | ||
isErr(): boolean; | ||
unwrap(): T; | ||
unwrapErr(): E; | ||
} | ||
export class Err<T, E> implements Result<T, E> { | ||
private val; | ||
constructor(val: E); | ||
isOk(): boolean; | ||
isErr(): boolean; | ||
unwrap(): T; | ||
unwrapErr(): E; | ||
static abiDecode(decoder: Decoder): Address; | ||
} | ||
export {}; | ||
} | ||
export declare type Result<T, E> = Result.Ok<T, E> | Result.Err<T, E>; | ||
export {}; |
@@ -44,3 +44,4 @@ "use strict"; | ||
super(utils_1.decodeHex(repr)); | ||
this._hex = (repr.startsWith('0x') ? '' : '0x') + repr; | ||
this._hex = | ||
(repr.startsWith('0x') ? '' : '0x') + repr.toLowerCase(); | ||
} | ||
@@ -85,3 +86,3 @@ else { | ||
repr = repr.length % 2 == 0 ? repr : '0' + repr; | ||
this._hex = '0x' + repr; | ||
this._hex = '0x' + repr.toLowerCase(); | ||
} | ||
@@ -124,2 +125,45 @@ else { | ||
exports.ExecutionError = ExecutionError; | ||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
var Result; | ||
(function (Result) { | ||
class Ok { | ||
constructor(val) { | ||
this.val = val; | ||
} | ||
isOk() { | ||
return true; | ||
} | ||
isErr() { | ||
return false; | ||
} | ||
unwrap() { | ||
return this.val; | ||
} | ||
unwrapErr() { | ||
throw new Error('tried to unwrapErr Ok'); | ||
} | ||
} | ||
Result.Ok = Ok; | ||
class Err { | ||
constructor(val) { | ||
this.val = val; | ||
} | ||
isOk() { | ||
return false; | ||
} | ||
isErr() { | ||
return true; | ||
} | ||
unwrap() { | ||
throw new Error('tried to unwrap Err'); | ||
} | ||
unwrapErr() { | ||
return this.val; | ||
} | ||
static abiDecode(decoder) { | ||
return new Address(decoder.readU8Array(Address.LENGTH)); | ||
} | ||
} | ||
Result.Err = Err; | ||
})(Result = exports.Result || (exports.Result = {})); | ||
//# sourceMappingURL=types.js.map |
{ | ||
"name": "oasis-std", | ||
"version": "0.1.0-rc.13", | ||
"version": "0.1.0-rc.14", | ||
"description": "Oasis platform standard library", | ||
@@ -30,3 +30,3 @@ "license": "Apache-2.0", | ||
}, | ||
"gitHead": "1abe3a0cabceba3c5fe5ea0bf19286b5317bfa49" | ||
"gitHead": "3940060c222701a5a6add6560b1523c77073007b" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1399
254746