@ocap/wallet
Version:
Utility function to create and use an forge compatible crypto wallet
94 lines (93 loc) • 4.28 kB
text/typescript
import { DIDType, DIDTypeArg, DIDTypeStr, DidType } from "@arcblock/did";
import { BytesType, EncodingType } from "@ocap/util";
//#region src/index.d.ts
type KeyPairType<T extends BytesType = string> = {
sk?: T;
pk?: T;
address?: string;
};
type SerializedWallet = {
type: DIDTypeStr;
pk: string;
sk: string;
address: string;
};
interface WalletObject<T extends BytesType = string> {
type: DIDType;
secretKey: T;
publicKey: T;
address: string;
hash(data: BytesType, round?: number, encoding?: 'hex'): string;
hash(data: BytesType, round?: number, encoding?: 'base16'): string;
hash(data: BytesType, round?: number, encoding?: 'base58'): string;
hash(data: BytesType, round?: number, encoding?: 'base64'): string;
hash(data: BytesType, round?: number, encoding?: 'buffer'): Buffer;
hash(data: BytesType, round?: number, encoding?: 'Uint8Array'): Uint8Array;
hash(data: BytesType, round?: number, encoding?: EncodingType): BytesType;
sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'hex'): Promise<string>;
sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'base16'): Promise<string>;
sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'base58'): Promise<string>;
sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'base64'): Promise<string>;
sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'buffer'): Promise<Buffer>;
sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'Uint8Array'): Promise<Uint8Array>;
sign(data: BytesType, hashBeforeSign?: boolean, encoding?: EncodingType): Promise<BytesType>;
verify(data: BytesType, signature: BytesType, hashBeforeVerify?: boolean, extra?: any): Promise<boolean>;
ethHash(data: string): string;
ethSign(data: string, hashBeforeSign?: boolean): Promise<string>;
ethVerify(data: string, signature: string, hashBeforeVerify?: boolean): Promise<boolean>;
signETH(data: string, hashBeforeSign?: boolean): Promise<string>;
signJWT(payload?: any, doSign?: boolean, version?: string): Promise<string>;
toJSON(): SerializedWallet;
/**
* @deprecated ES6: use `wallet.address` instead
*/
toAddress(): string;
}
declare const WalletType: typeof DidType;
/**
* Generate an wallet instance that can be used to sign a message or verify a signature
*/
declare function Wallet<T extends BytesType = string>(keyPair: KeyPairType<T>, t?: DIDTypeArg): WalletObject<T>;
/**
* Generate a wallet from secretKey
*
* @example
* const assert = require('assert');
* const { fromSecretKey } = require('@ocap/wallet');
*
* const sk =
* '0xD67C071B6F51D2B61180B9B1AA9BE0DD0704619F0E30453AB4A592B036EDE644E4852B7091317E3622068E62A5127D1FB0D4AE2FC50213295E10652D2F0ABFC7';
* const sig =
* '0x08a102851c38c072e42756c1cc70938b5499c8e9358dfe5f383823f56cdb282ffda60fcd581a02c6c673069e5afc0bf09abbe3639b61b84d64fd58ef9f083003';
*
* const wallet = fromSecretKey(sk, type);
* const message = 'data to sign';
* const signature = wallet.sign(message);
* assert.equal(signature, sig, "signature should match");
* assert.ok(wallet.verify(message, signature), "signature should be verified");
*/
declare function fromSecretKey<T extends BytesType = string>(sk: T, _type?: DIDTypeArg): WalletObject<T>;
/**
* Generate a wallet from publicKey
*/
declare function fromPublicKey<T extends BytesType = string>(pk: T, _type?: DIDTypeArg): WalletObject<T>;
/**
* Generate a wallet from address (did)
*
* Since we do not know the publicKey and secretKey, this kind of wallet cannot be used for signing and verifying
*/
declare function fromAddress<T extends BytesType = string>(address: string): WalletObject<T>;
/**
* Generate a wallet by generating a random secretKey
*/
declare function fromRandom<T extends BytesType = string>(_type?: DIDTypeArg): WalletObject<T>;
/**
* Generating a wallet from a serialized json presentation of another wallet
*/
declare function fromJSON<T extends BytesType = string>(json: SerializedWallet): WalletObject<T>;
/**
* Check if an object is valid wallet object
*/
declare function isValid(wallet: WalletObject, canSign?: boolean): boolean;
//#endregion
export { SerializedWallet, Wallet, WalletObject, WalletType, fromAddress, fromJSON, fromPublicKey, fromRandom, fromSecretKey, isValid };