-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add all rust sdk non-deprecated types (#181)
* 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
Showing
27 changed files
with
477 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export declare type AccountId = string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const ONE_TERA_GAS = 1000000000000n; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = {})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type AccountId = string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
Oops, something went wrong.