Skip to content

Commit

Permalink
fix: add KeyPairString type
Browse files Browse the repository at this point in the history
  • Loading branch information
esaminu committed Sep 27, 2022
1 parent 180f966 commit 6648797
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/near-api-js/src/key_stores/in_memory_key_store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { KeyStore } from './keystore';
import { KeyPair } from '../utils/key_pair';
import { KeyPair, KeyPairString } from '../utils/key_pair';

/**
* Simple in-memory keystore for mainly for testing purposes.
Expand Down Expand Up @@ -30,7 +30,7 @@ import { KeyPair } from '../utils/key_pair';
*/
export class InMemoryKeyStore extends KeyStore {
/** @hidden */
private keys: { [key: string]: string };
private keys: { [key: string]: KeyPairString };

constructor() {
super();
Expand Down
10 changes: 6 additions & 4 deletions packages/near-api-js/src/utils/key_pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ export class PublicKey extends Assignable {
}
}

export type KeyPairString = `ed25519:${string}` | `secp256k1:${string}`;

export abstract class KeyPair {
abstract sign(message: Uint8Array): Signature;
abstract verify(message: Uint8Array, signature: Uint8Array): boolean;
abstract toString(): string;
abstract toString(): KeyPairString;
abstract getPublicKey(): PublicKey;

/**
Expand All @@ -113,7 +115,7 @@ export abstract class KeyPair {
}
}

static fromString(encodedKey: `${'secp256k1' | 'ed25519'}:${string}`): KeyPair {
static fromString(encodedKey: KeyPairString): KeyPair {
const parts = encodedKey.split(':');
//TODO: clarify what we must do here
if (parts.length === 2) {
Expand Down Expand Up @@ -172,7 +174,7 @@ export class KeyPairEd25519 extends KeyPair {
return this.publicKey.verify(message, signature);
}

toString(): string {
toString(): KeyPairString {
return `ed25519:${this.secretKey}`;
}

Expand Down Expand Up @@ -236,7 +238,7 @@ export class KeyPairSecp256k1 extends KeyPair {
return this.publicKey.verify(message, signature);
}

toString(): string {
toString(): KeyPairString {
return `secp256k1:${this.secretKey}`;
}

Expand Down

0 comments on commit 6648797

Please sign in to comment.