2018-08-23 10:07:30 -07:00
|
|
|
// @flow
|
2018-08-22 17:03:50 -07:00
|
|
|
import nacl from 'tweetnacl';
|
2018-08-23 10:07:30 -07:00
|
|
|
import type {KeyPair} from 'tweetnacl';
|
2018-08-22 17:03:50 -07:00
|
|
|
|
2020-02-13 08:25:22 +08:00
|
|
|
import {toBuffer} from './util/to-buffer';
|
2018-09-30 18:42:45 -07:00
|
|
|
import {PublicKey} from './publickey';
|
2018-08-23 10:52:48 -07:00
|
|
|
|
2018-08-24 09:05:23 -07:00
|
|
|
/**
|
2018-08-24 09:11:39 -07:00
|
|
|
* An account key pair (public and secret keys).
|
2018-08-24 09:05:23 -07:00
|
|
|
*/
|
2018-08-22 17:03:50 -07:00
|
|
|
export class Account {
|
2018-08-23 10:07:30 -07:00
|
|
|
_keypair: KeyPair;
|
|
|
|
|
2018-08-24 09:05:23 -07:00
|
|
|
/**
|
|
|
|
* Create a new Account object
|
|
|
|
*
|
|
|
|
* If the secretKey parameter is not provided a new key pair is randomly
|
|
|
|
* created for the account
|
|
|
|
*
|
|
|
|
* @param secretKey Secret key for the account
|
|
|
|
*/
|
2020-02-13 08:25:22 +08:00
|
|
|
constructor(secretKey?: Buffer | Uint8Array | Array<number>) {
|
2018-08-22 17:03:50 -07:00
|
|
|
if (secretKey) {
|
2020-02-13 08:25:22 +08:00
|
|
|
this._keypair = nacl.sign.keyPair.fromSecretKey(toBuffer(secretKey));
|
2018-08-22 17:03:50 -07:00
|
|
|
} else {
|
|
|
|
this._keypair = nacl.sign.keyPair();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 09:05:23 -07:00
|
|
|
/**
|
|
|
|
* The public key for this account
|
|
|
|
*/
|
2018-08-23 10:52:48 -07:00
|
|
|
get publicKey(): PublicKey {
|
2018-09-30 18:42:45 -07:00
|
|
|
return new PublicKey(this._keypair.publicKey);
|
2018-08-22 17:03:50 -07:00
|
|
|
}
|
|
|
|
|
2018-08-24 09:05:23 -07:00
|
|
|
/**
|
|
|
|
* The **unencrypted** secret key for this account
|
|
|
|
*/
|
2018-08-23 10:07:30 -07:00
|
|
|
get secretKey(): Buffer {
|
2018-08-22 17:03:50 -07:00
|
|
|
return this._keypair.secretKey;
|
|
|
|
}
|
|
|
|
}
|