Skip to content

Commit

Permalink
add all rust sdk non-deprecated types (#181)
Browse files Browse the repository at this point in the history
* add all rust sdk non-deprecated types
* fix build by using a typescript esm base58 library
* revert promiseResult
* add public key tests
  • Loading branch information
ailisp authored Aug 22, 2022
1 parent bb02965 commit 663354e
Show file tree
Hide file tree
Showing 27 changed files with 477 additions and 19 deletions.
6 changes: 1 addition & 5 deletions lib/api.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Bytes } from "./utils";
import { PromiseResult } from "./types";
export declare function log(...params: any[]): void;
export declare function signerAccountId(): string;
export declare function signerAccountPk(): Bytes;
Expand Down Expand Up @@ -48,11 +49,6 @@ export declare function promiseBatchActionAddKeyWithFunctionCall(promiseIndex: n
export declare function promiseBatchActionDeleteKey(promiseIndex: number | BigInt, publicKey: Bytes): void;
export declare function promiseBatchActionDeleteAccount(promiseIndex: number | BigInt, beneficiaryId: string): void;
export declare function promiseResultsCount(): BigInt;
export declare enum PromiseResult {
NotReady = 0,
Successful = 1,
Failed = 2
}
export declare function promiseResult(resultIdx: number | BigInt): Bytes | PromiseResult.NotReady | PromiseResult.Failed;
export declare function promiseReturn(promiseIdx: number | BigInt): void;
export declare function storageWrite(key: Bytes, value: Bytes): boolean;
Expand Down
7 changes: 1 addition & 6 deletions lib/api.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PromiseResult } from "./types";
const U64_MAX = 2n ** 64n - 1n;
const EVICTED_REGISTER = U64_MAX - 1n;
export function log(...params) {
Expand Down Expand Up @@ -186,12 +187,6 @@ export function promiseBatchActionDeleteAccount(promiseIndex, beneficiaryId) {
export function promiseResultsCount() {
return env.promise_results_count();
}
export var PromiseResult;
(function (PromiseResult) {
PromiseResult[PromiseResult["NotReady"] = 0] = "NotReady";
PromiseResult[PromiseResult["Successful"] = 1] = "Successful";
PromiseResult[PromiseResult["Failed"] = 2] = "Failed";
})(PromiseResult || (PromiseResult = {}));
export function promiseResult(resultIdx) {
let status = env.promise_result(resultIdx, 0);
if (status == PromiseResult.Successful) {
Expand Down
1 change: 1 addition & 0 deletions lib/types/account_id.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare type AccountId = string;
1 change: 1 addition & 0 deletions lib/types/account_id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
2 changes: 2 additions & 0 deletions lib/types/gas.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export declare type Gas = bigint;
export declare const ONE_TERA_GAS: Gas;
1 change: 1 addition & 0 deletions lib/types/gas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ONE_TERA_GAS = 1000000000000n;
6 changes: 6 additions & 0 deletions lib/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { AccountId } from "./account_id";
import { BlockHeight, EpochHeight, Balance, StorageUsage } from './primitives';
import { PromiseResult, PromiseError, PromiseIndex, ReceiptIndex, IteratorIndex } from "./vm_types";
import { Gas, ONE_TERA_GAS } from "./gas";
import { PublicKey, CurveType, curveTypeFromStr, ParsePublicKeyError, InvalidLengthError, Base58Error, UnknownCurve } from "./public_key";
export { AccountId, BlockHeight, EpochHeight, Balance, StorageUsage, PromiseResult, PromiseError, PromiseIndex, ReceiptIndex, IteratorIndex, Gas, ONE_TERA_GAS, PublicKey, CurveType, curveTypeFromStr, ParsePublicKeyError, InvalidLengthError, Base58Error, UnknownCurve };
4 changes: 4 additions & 0 deletions lib/types/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PromiseResult, PromiseError } from "./vm_types";
import { ONE_TERA_GAS } from "./gas";
import { PublicKey, CurveType, curveTypeFromStr, ParsePublicKeyError, InvalidLengthError, Base58Error, UnknownCurve } from "./public_key";
export { PromiseResult, PromiseError, ONE_TERA_GAS, PublicKey, CurveType, curveTypeFromStr, ParsePublicKeyError, InvalidLengthError, Base58Error, UnknownCurve };
4 changes: 4 additions & 0 deletions lib/types/primitives.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export declare type StorageUsage = bigint;
export declare type BlockHeight = bigint;
export declare type EpochHeight = bigint;
export declare type Balance = bigint;
1 change: 1 addition & 0 deletions lib/types/primitives.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
25 changes: 25 additions & 0 deletions lib/types/public_key.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Bytes } from "../utils";
export declare enum CurveType {
ED25519 = 0,
SECP256K1 = 1
}
export declare function curveTypeFromStr(value: string): CurveType;
export declare class ParsePublicKeyError extends Error {
}
export declare class InvalidLengthError extends ParsePublicKeyError {
length: number;
constructor(length: number);
}
export declare class Base58Error extends ParsePublicKeyError {
error: string;
constructor(error: string);
}
export declare class UnknownCurve extends ParsePublicKeyError {
constructor();
}
export declare class PublicKey {
data: Bytes;
constructor(data: Bytes);
curveType(): CurveType;
static fromString(s: string): PublicKey;
}
80 changes: 80 additions & 0 deletions lib/types/public_key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { bytes } from "../utils";
import { base58 } from '@scure/base';
export var CurveType;
(function (CurveType) {
CurveType[CurveType["ED25519"] = 0] = "ED25519";
CurveType[CurveType["SECP256K1"] = 1] = "SECP256K1";
})(CurveType || (CurveType = {}));
function data_len(c) {
switch (c) {
case CurveType.ED25519:
return 32;
case CurveType.SECP256K1:
return 64;
default:
throw new UnknownCurve();
}
}
function split_key_type_data(value) {
let idx = value.indexOf(":");
if (idx >= 0) {
return [curveTypeFromStr(value.substring(0, idx)), value.substring(idx + 1)];
}
else {
return [CurveType.ED25519, value];
}
}
export function curveTypeFromStr(value) {
switch (value) {
case "ed25519":
return CurveType.ED25519;
case "secp256k1":
return CurveType.SECP256K1;
default:
throw new UnknownCurve();
}
}
export class ParsePublicKeyError extends Error {
}
export class InvalidLengthError extends ParsePublicKeyError {
constructor(length) {
super(`Invalid length: ${length}`);
this.length = length;
}
}
export class Base58Error extends ParsePublicKeyError {
constructor(error) {
super(`Base58 error: ${error}`);
this.error = error;
}
}
export class UnknownCurve extends ParsePublicKeyError {
constructor() {
super("Unknown curve");
}
}
export class PublicKey {
constructor(data) {
this.data = data;
let curve_type = data.charCodeAt(0);
let curve_len = data_len(curve_type);
if (data.length != curve_len + 1) {
throw new InvalidLengthError(data.length);
}
this.data = data;
}
curveType() {
return this.data.charCodeAt(0);
}
static fromString(s) {
let [curve, key_data] = split_key_type_data(s);
let data;
try {
data = bytes(base58.decode(key_data));
}
catch (err) {
throw new Base58Error(err.message);
}
return new PublicKey(String.fromCharCode(curve) + data);
}
}
12 changes: 12 additions & 0 deletions lib/types/vm_types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export declare type PromiseIndex = bigint;
export declare type ReceiptIndex = bigint;
export declare type IteratorIndex = bigint;
export declare enum PromiseResult {
NotReady = 0,
Successful = 1,
Failed = 2
}
export declare enum PromiseError {
Failed = 0,
NotReady = 1
}
11 changes: 11 additions & 0 deletions lib/types/vm_types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export var PromiseResult;
(function (PromiseResult) {
PromiseResult[PromiseResult["NotReady"] = 0] = "NotReady";
PromiseResult[PromiseResult["Successful"] = 1] = "Successful";
PromiseResult[PromiseResult["Failed"] = 2] = "Failed";
})(PromiseResult || (PromiseResult = {}));
export var PromiseError;
(function (PromiseError) {
PromiseError[PromiseError["Failed"] = 0] = "Failed";
PromiseError[PromiseError["NotReady"] = 1] = "NotReady";
})(PromiseError || (PromiseError = {}));
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
"@rollup/plugin-babel": "^5.3.1",
"@rollup/plugin-commonjs": "^21.0.1",
"@rollup/plugin-node-resolve": "^13.1.1",
"@scure/base": "^1.1.1",
"bs58": "^5.0.0",
"rollup": "^2.61.1",
"rollup-plugin-sourcemaps": "^0.6.3",
"yargs": "^17.5.1"
Expand Down
7 changes: 1 addition & 6 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Bytes } from "./utils";
import { PromiseResult } from "./types";

const U64_MAX = 2n ** 64n - 1n;
const EVICTED_REGISTER = U64_MAX - 1n;
Expand Down Expand Up @@ -322,12 +323,6 @@ export function promiseResultsCount(): BigInt {
return env.promise_results_count();
}

export enum PromiseResult {
NotReady = 0,
Successful = 1,
Failed = 2,
}

export function promiseResult(
resultIdx: number | BigInt
): Bytes | PromiseResult.NotReady | PromiseResult.Failed {
Expand Down
22 changes: 22 additions & 0 deletions src/promise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Bytes } from "..";
import { Balance } from "./types";

export class CreateAccount {}
export class DeployContract {
constructor(public code: Bytes) {}
}
export class FunctionCall {
constructor(public function_name: string, public args: Bytes, public amount: Balance) {}
}
// TODO add FunctionCallWeight after add that in api.ts
export class Transfer {
constructor(public amount: Balance) {}
}
export class Stake {
constructor(public amount: Balance, public public_key: PublicKey) {}
}

export type PromiseAction = [string]
export type A = [string]
let a: A = ["a"]
let b: PromiseAction = ["a"]
1 change: 1 addition & 0 deletions src/types/account_id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type AccountId = string;
2 changes: 2 additions & 0 deletions src/types/gas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type Gas = bigint;
export const ONE_TERA_GAS: Gas = 1_000_000_000_000n;
8 changes: 8 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { AccountId } from "./account_id";
import {BlockHeight, EpochHeight, Balance, StorageUsage} from './primitives'
import { PromiseResult, PromiseError, PromiseIndex, ReceiptIndex, IteratorIndex } from "./vm_types";
import { Gas, ONE_TERA_GAS } from "./gas";
import { PublicKey, CurveType, curveTypeFromStr, ParsePublicKeyError, InvalidLengthError, Base58Error, UnknownCurve } from "./public_key";

export {AccountId, BlockHeight, EpochHeight, Balance, StorageUsage, PromiseResult, PromiseError, PromiseIndex, ReceiptIndex, IteratorIndex, Gas, ONE_TERA_GAS,
PublicKey, CurveType, curveTypeFromStr, ParsePublicKeyError, InvalidLengthError, Base58Error, UnknownCurve}
4 changes: 4 additions & 0 deletions src/types/primitives.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type StorageUsage = bigint;
export type BlockHeight = bigint;
export type EpochHeight = bigint;
export type Balance = bigint;
81 changes: 81 additions & 0 deletions src/types/public_key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Bytes, bytes } from "../utils";
import { base58 } from '@scure/base';

export enum CurveType {
ED25519 = 0,
SECP256K1 = 1,
}

function data_len(c: CurveType): number {
switch (c) {
case CurveType.ED25519:
return 32;
case CurveType.SECP256K1:
return 64;
default:
throw new UnknownCurve()
}
}

function split_key_type_data(value: string): [CurveType, string] {
let idx = value.indexOf(":");
if (idx >= 0) {
return [curveTypeFromStr(value.substring(0, idx)), value.substring(idx + 1)];
} else {
return [CurveType.ED25519, value];
}
}

export function curveTypeFromStr(value: string): CurveType {
switch (value) {
case "ed25519":
return CurveType.ED25519;
case "secp256k1":
return CurveType.SECP256K1;
default:
throw new UnknownCurve();
}
}

export class ParsePublicKeyError extends Error {}
export class InvalidLengthError extends ParsePublicKeyError {
constructor(public length: number) {
super(`Invalid length: ${length}`);
}
}
export class Base58Error extends ParsePublicKeyError {
constructor(public error: string) {
super(`Base58 error: ${error}`);
}
}
export class UnknownCurve extends ParsePublicKeyError {
constructor() {
super("Unknown curve");
}
}

export class PublicKey {
constructor(public data: Bytes) {
let curve_type = data.charCodeAt(0) as CurveType
let curve_len = data_len(curve_type)
if (data.length != curve_len + 1) {
throw new InvalidLengthError(data.length)
}
this.data = data
}

curveType(): CurveType {
return this.data.charCodeAt(0) as CurveType
}

static fromString(s: string) {
let [curve, key_data] = split_key_type_data(s);
let data: Bytes;
try {
data = bytes(base58.decode(key_data));
} catch (err) {
throw new Base58Error(err.message);
}
return new PublicKey(String.fromCharCode(curve) + data);
}
}
14 changes: 14 additions & 0 deletions src/types/vm_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export type PromiseIndex = bigint;
export type ReceiptIndex = bigint;
export type IteratorIndex = bigint;

export enum PromiseResult {
NotReady = 0,
Successful = 1,
Failed = 2,
}

export enum PromiseError {
Failed,
NotReady,
}
Loading

0 comments on commit 663354e

Please sign in to comment.