diff --git a/lib/api.d.ts b/lib/api.d.ts index 0f34b7161..10241af84 100644 --- a/lib/api.d.ts +++ b/lib/api.d.ts @@ -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; @@ -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; diff --git a/lib/api.js b/lib/api.js index dff95d697..daa81062f 100644 --- a/lib/api.js +++ b/lib/api.js @@ -1,3 +1,4 @@ +import { PromiseResult } from "./types"; const U64_MAX = 2n ** 64n - 1n; const EVICTED_REGISTER = U64_MAX - 1n; export function log(...params) { @@ -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) { diff --git a/lib/types/account_id.d.ts b/lib/types/account_id.d.ts new file mode 100644 index 000000000..48ff674a1 --- /dev/null +++ b/lib/types/account_id.d.ts @@ -0,0 +1 @@ +export declare type AccountId = string; diff --git a/lib/types/account_id.js b/lib/types/account_id.js new file mode 100644 index 000000000..cb0ff5c3b --- /dev/null +++ b/lib/types/account_id.js @@ -0,0 +1 @@ +export {}; diff --git a/lib/types/gas.d.ts b/lib/types/gas.d.ts new file mode 100644 index 000000000..a69c177cc --- /dev/null +++ b/lib/types/gas.d.ts @@ -0,0 +1,2 @@ +export declare type Gas = bigint; +export declare const ONE_TERA_GAS: Gas; diff --git a/lib/types/gas.js b/lib/types/gas.js new file mode 100644 index 000000000..1708a5f41 --- /dev/null +++ b/lib/types/gas.js @@ -0,0 +1 @@ +export const ONE_TERA_GAS = 1000000000000n; diff --git a/lib/types/index.d.ts b/lib/types/index.d.ts new file mode 100644 index 000000000..a5234793e --- /dev/null +++ b/lib/types/index.d.ts @@ -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 }; diff --git a/lib/types/index.js b/lib/types/index.js new file mode 100644 index 000000000..8fbfa22dd --- /dev/null +++ b/lib/types/index.js @@ -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 }; diff --git a/lib/types/primitives.d.ts b/lib/types/primitives.d.ts new file mode 100644 index 000000000..b98e51910 --- /dev/null +++ b/lib/types/primitives.d.ts @@ -0,0 +1,4 @@ +export declare type StorageUsage = bigint; +export declare type BlockHeight = bigint; +export declare type EpochHeight = bigint; +export declare type Balance = bigint; diff --git a/lib/types/primitives.js b/lib/types/primitives.js new file mode 100644 index 000000000..cb0ff5c3b --- /dev/null +++ b/lib/types/primitives.js @@ -0,0 +1 @@ +export {}; diff --git a/lib/types/public_key.d.ts b/lib/types/public_key.d.ts new file mode 100644 index 000000000..c8d09442e --- /dev/null +++ b/lib/types/public_key.d.ts @@ -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; +} diff --git a/lib/types/public_key.js b/lib/types/public_key.js new file mode 100644 index 000000000..53b9ef22f --- /dev/null +++ b/lib/types/public_key.js @@ -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); + } +} diff --git a/lib/types/vm_types.d.ts b/lib/types/vm_types.d.ts new file mode 100644 index 000000000..900c8bb84 --- /dev/null +++ b/lib/types/vm_types.d.ts @@ -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 +} diff --git a/lib/types/vm_types.js b/lib/types/vm_types.js new file mode 100644 index 000000000..915bf5d27 --- /dev/null +++ b/lib/types/vm_types.js @@ -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 = {})); diff --git a/package.json b/package.json index f59146bb0..a17d29c7e 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/api.ts b/src/api.ts index 6998edee8..f3dbdc95d 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,4 +1,5 @@ import { Bytes } from "./utils"; +import { PromiseResult } from "./types"; const U64_MAX = 2n ** 64n - 1n; const EVICTED_REGISTER = U64_MAX - 1n; @@ -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 { diff --git a/src/promise.ts b/src/promise.ts new file mode 100644 index 000000000..f195811d3 --- /dev/null +++ b/src/promise.ts @@ -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"] diff --git a/src/types/account_id.ts b/src/types/account_id.ts new file mode 100644 index 000000000..67f3277f7 --- /dev/null +++ b/src/types/account_id.ts @@ -0,0 +1 @@ +export type AccountId = string; diff --git a/src/types/gas.ts b/src/types/gas.ts new file mode 100644 index 000000000..4586b181b --- /dev/null +++ b/src/types/gas.ts @@ -0,0 +1,2 @@ +export type Gas = bigint; +export const ONE_TERA_GAS: Gas = 1_000_000_000_000n; diff --git a/src/types/index.ts b/src/types/index.ts new file mode 100644 index 000000000..73601a20b --- /dev/null +++ b/src/types/index.ts @@ -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} diff --git a/src/types/primitives.ts b/src/types/primitives.ts new file mode 100644 index 000000000..4940813e4 --- /dev/null +++ b/src/types/primitives.ts @@ -0,0 +1,4 @@ +export type StorageUsage = bigint; +export type BlockHeight = bigint; +export type EpochHeight = bigint; +export type Balance = bigint; diff --git a/src/types/public_key.ts b/src/types/public_key.ts new file mode 100644 index 000000000..271690a26 --- /dev/null +++ b/src/types/public_key.ts @@ -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); + } +} \ No newline at end of file diff --git a/src/types/vm_types.ts b/src/types/vm_types.ts new file mode 100644 index 000000000..a37cc9247 --- /dev/null +++ b/src/types/vm_types.ts @@ -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, +} \ No newline at end of file diff --git a/tests/__tests__/test-public-key.ava.js b/tests/__tests__/test-public-key.ava.js new file mode 100644 index 000000000..29652f632 --- /dev/null +++ b/tests/__tests__/test-public-key.ava.js @@ -0,0 +1,95 @@ +import { Worker } from 'near-workspaces'; +import test from 'ava'; + + +test.before(async t => { + // Init the worker and start a Sandbox server + const worker = await Worker.init(); + + // Prepare sandbox for tests, create accounts, deploy contracts, etx. + const root = worker.rootAccount; + + // Create and deploy test contract + const pkContract = await root.createSubAccount('pk'); + await pkContract.deploy('build/public-key.wasm'); + + // Test users + const ali = await root.createSubAccount('ali'); + const bob = await root.createSubAccount('bob'); + const carl = await root.createSubAccount('carl'); + + + // Save state for test runs + t.context.worker = worker; + t.context.accounts = { root, pkContract, ali, bob, carl }; +}); + +test.after(async t => { + await t.context.worker.tearDown().catch(error => { + console.log('Failed to tear down the worker:', error); + }); +}); + +test('add signer key should success', async t => { + const { ali, pkContract } = t.context.accounts; + let r = await ali.callRaw(pkContract, 'test_add_signer_key', ''); + t.is(r.result.status.SuccessValue, ''); +}); + +test('add ed25519 key bytes should success', async t => { + const { ali, pkContract } = t.context.accounts; + let r = await ali.callRaw(pkContract, 'test_add_ed25519_key_bytes', ''); + t.is(r.result.status.SuccessValue, ''); +}); + +test('add ed25519 key string should success', async t => { + const { ali, pkContract } = t.context.accounts; + let r = await ali.callRaw(pkContract, 'test_add_ed25519_key_string', ''); + t.is(r.result.status.SuccessValue, ''); +}); + +test('add secp256k1 key bytes should success', async t => { + const { bob, pkContract } = t.context.accounts; + let r = await bob.callRaw(pkContract, 'test_add_secp256k1_key_bytes', ''); + t.is(r.result.status.SuccessValue, ''); +}); + +test('add secp256k1 key string should success', async t => { + const { bob, pkContract } = t.context.accounts; + let r = await bob.callRaw(pkContract, 'test_add_secp256k1_key_string', '', {gas: '100 Tgas'}); + t.is(r.result.status.SuccessValue, ''); +}); + +test('add invalid key should error', async t => { + const { bob, pkContract } = t.context.accounts; + let r = await bob.callRaw(pkContract, 'add_invalid_public_key', ''); + t.is(r.result.status.SuccessValue, undefined); + t.is(r.result.status.Failure.ActionError.kind.FunctionCallError.ExecutionError, 'VM Logic provided an invalid public key'); +}); + +test('curve type check should success', async t => { + const { carl, pkContract } = t.context.accounts; + let r = await carl.callRaw(pkContract, 'curve_type', ''); + t.is(r.result.status.SuccessValue, ''); +}) + +test('create invalid curve type should fail', async t => { + const { carl, pkContract } = t.context.accounts; + let r = await carl.callRaw(pkContract, 'create_invalid_curve_type', ''); + t.is(r.result.status.SuccessValue, undefined); + t.assert(r.result.status.Failure.ActionError.kind.FunctionCallError.ExecutionError.startsWith('Smart contract panicked: Unknown curve')); +}) + +test('create invalid length should fail', async t => { + const { carl, pkContract } = t.context.accounts; + let r = await carl.callRaw(pkContract, 'create_invalid_length', ''); + t.is(r.result.status.SuccessValue, undefined); + t.assert(r.result.status.Failure.ActionError.kind.FunctionCallError.ExecutionError.startsWith('Smart contract panicked: Invalid length')); +}) + +test('create invalid base58 should fail', async t => { + const { carl, pkContract } = t.context.accounts; + let r = await carl.callRaw(pkContract, 'create_from_invalid_base58', ''); + t.is(r.result.status.SuccessValue, undefined); + t.assert(r.result.status.Failure.ActionError.kind.FunctionCallError.ExecutionError.startsWith('Smart contract panicked: Base58 error')); +}) \ No newline at end of file diff --git a/tests/package.json b/tests/package.json index 0d1bc5b0c..f6c332758 100644 --- a/tests/package.json +++ b/tests/package.json @@ -6,7 +6,7 @@ "type": "module", "scripts": { "postinstall": "cd .. && yarn link && cd tests && yarn link near-sdk-js", - "build": "yarn build:context-api && yarn build:math-api && yarn build:storage-api && yarn build:log-panic-api && yarn build:promise-api && yarn build:promise-batch-api && yarn build:function-params && yarn build:lookup-map && yarn build:lookup-set && yarn build:unordered-map && yarn build:unordered-set && yarn build:vector && yarn build:bytes && yarn build:typescript", + "build": "yarn build:context-api && yarn build:math-api && yarn build:storage-api && yarn build:log-panic-api && yarn build:promise-api && yarn build:promise-batch-api && yarn build:function-params && yarn build:lookup-map && yarn build:lookup-set && yarn build:unordered-map && yarn build:unordered-set && yarn build:vector && yarn build:bytes && yarn build:typescript && yarn build:public-key", "build:context-api": "near-sdk-js build src/context_api.js build/context_api.wasm", "build:math-api": "near-sdk-js build src/math_api.js build/math_api.wasm", "build:storage-api": "near-sdk-js build src/storage_api.js build/storage_api.wasm", @@ -21,6 +21,7 @@ "build:vector": "near-sdk-js build src/vector.js build/vector.wasm", "build:bytes": "near-sdk-js build src/bytes.js build/bytes.wasm", "build:typescript": "near-sdk-js build src/typescript.ts build/typescript.wasm", + "build:public-key": "near-sdk-js build src/public-key.js build/public-key.wasm", "test": "ava", "test:context-api": "ava __tests__/test_context_api.ava.js", "test:math-api": "ava __tests__/test_math_api.ava.js", @@ -34,7 +35,8 @@ "test:unordered-map": "ava __tests__/unordered-map.ava.js", "test:vector": "ava __tests__/vector.ava.js", "test:bytes": "ava __tests__/bytes.ava.js", - "test:typescript": "ava __tests__/typescript.ava.js" + "test:typescript": "ava __tests__/typescript.ava.js", + "test:public-key": "ava __tests__/test-public-key.ava.js" }, "author": "Near Inc ", "license": "Apache-2.0", diff --git a/tests/src/public-key.js b/tests/src/public-key.js new file mode 100644 index 000000000..43f68d86e --- /dev/null +++ b/tests/src/public-key.js @@ -0,0 +1,76 @@ +import {near, bytes, types} from 'near-sdk-js' +import {CurveType, PublicKey} from 'near-sdk-js/lib/types' +import {assert} from 'near-sdk-js/lib/utils' + +function runtime_validate_public_key(prefix, public_key) { + let promiseId = near.promiseBatchCreate(prefix + '.pk.test.near') + near.promiseBatchActionCreateAccount(promiseId) + near.promiseBatchActionTransfer(promiseId, 10000000000000000000000000n) + near.promiseBatchActionAddKeyWithFullAccess(promiseId, public_key, 1n) + near.promiseReturn(promiseId) +} + +export function test_add_signer_key() { + runtime_validate_public_key('aa', near.signerAccountPk()) +} + +export function test_add_ed25519_key_bytes() { + let pk = new PublicKey(bytes(new Uint8Array([ + // CurveType.ED25519 = 0 + 0, + // ED25519 PublicKey data + 186, 44, 216, 49, 157, 48, 151, 47, + 23, 244, 137, 69, 78, 150, 54, 42, + 30, 248, 110, 26, 205, 18, 137, 154, + 10, 208, 26, 183, 65, 166, 223, 18 + ]))) + runtime_validate_public_key('a', pk.data) +} + +export function test_add_ed25519_key_string() { + let k = 'ed25519:DXkVZkHd7WUUejCK7i74uAoZWy1w9AZqshhTHxhmqHuB' + let pk = PublicKey.fromString(k) + runtime_validate_public_key('b', pk.data) +} + +export function test_add_secp256k1_key_bytes() { + let pk = new PublicKey(bytes(new Uint8Array([ + // CurveType.SECP256K1 = 1 + 1, + // SECP256K1 PublicKey data + 242, 86, 198, 230, 200, 11, 33, 63, 42, 160, 176, + 23, 68, 35, 93, 81, 92, 89, 68, 53, 190, 101, + 27, 21, 136, 58, 16, 221, 71, 47, 166, 70, 206, + 98, 234, 243, 103, 13, 197, 203, 145, 0, 160, 202, + 42, 85, 178, 193, 71, 193, 233, 163, 140, 228, 40, + 135, 142, 125, 70, 225, 251, 113, 74, 153 + ] + ))) + runtime_validate_public_key('c', pk.data) +} + +export function test_add_secp256k1_key_string() { + let k = 'secp256k1:5r22SrjrDvgY3wdQsnjgxkeAbU1VcM71FYvALEQWihjM3Xk4Be1CpETTqFccChQr4iJwDroSDVmgaWZv2AcXvYeL' + let pk = PublicKey.fromString(k) + runtime_validate_public_key('d', pk.data) +} + +export function add_invalid_public_key() { + runtime_validate_public_key('e', bytes(new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))) +} + +export function curve_type() { + assert(new PublicKey(near.signerAccountPk()).curveType() == CurveType.ED25519) +} + +export function create_invalid_curve_type() { + new PublicKey(bytes(new Uint8Array([2, 1]))) +} + +export function create_invalid_length() { + new PublicKey(bytes(new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))) +} + +export function create_from_invalid_base58() { + PublicKey.fromString('ed25519:!@#$%^&*') +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index b96e1c0ca..dfb64ce7f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -533,6 +533,11 @@ estree-walker "^1.0.1" picomatch "^2.2.2" +"@scure/base@^1.1.1": + version "1.1.1" + resolved "https://registry.yarnpkg.com/@scure/base/-/base-1.1.1.tgz#ebb651ee52ff84f420097055f4bf46cfba403938" + integrity sha512-ZxOhsSyxYwLJj3pLZCefNitxsj093tb2vq90mp2txoYeBqbcjDjqFhyM8eUjq/uFm6zJ+mUuqxlS2FkuSY1MTA== + "@types/estree@*", "@types/estree@0.0.39": version "0.0.39" resolved "https://registry.npmjs.org/@types/estree/-/estree-0.0.39.tgz" @@ -591,6 +596,11 @@ balanced-match@^1.0.0: resolved "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz" integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw== +base-x@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/base-x/-/base-x-4.0.0.tgz#d0e3b7753450c73f8ad2389b5c018a4af7b2224a" + integrity sha512-FuwxlW4H5kh37X/oW59pwTzzTKRzfrrQwhmyspRM7swOEZcHtDZSCt45U6oKgtuFE+WYPblePMVIPR4RZrh/hw== + brace-expansion@^1.1.7: version "1.1.11" resolved "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz" @@ -610,6 +620,13 @@ browserslist@^4.20.2: node-releases "^2.0.3" picocolors "^1.0.0" +bs58@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/bs58/-/bs58-5.0.0.tgz#865575b4d13c09ea2a84622df6c8cbeb54ffc279" + integrity sha512-r+ihvQJvahgYT50JD05dyJNKlmmSlMoOGwn1lCcEzanPglg7TxYjioQUYehQ9mAR/+hOSd2jRc/Z2y5UxBymvQ== + dependencies: + base-x "^4.0.0" + builtin-modules@^3.0.0: version "3.3.0" resolved "https://registry.npmjs.org/builtin-modules/-/builtin-modules-3.3.0.tgz"