2022-08-28 12:11:34 -07:00
|
|
|
import {Buffer} from 'buffer';
|
2018-08-22 17:03:50 -07:00
|
|
|
|
2022-08-28 12:11:34 -07:00
|
|
|
import {generatePrivateKey, getPublicKey} from './utils/ed25519';
|
2022-08-11 02:10:11 -07:00
|
|
|
import {toBuffer} from './utils/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).
|
2021-05-07 01:59:51 -07:00
|
|
|
*
|
|
|
|
* @deprecated since v1.10.0, please use {@link Keypair} instead.
|
2018-08-24 09:05:23 -07:00
|
|
|
*/
|
2018-08-22 17:03:50 -07:00
|
|
|
export class Account {
|
2021-03-14 20:01:35 -07:00
|
|
|
/** @internal */
|
2022-08-28 12:11:34 -07:00
|
|
|
private _publicKey: Buffer;
|
|
|
|
/** @internal */
|
|
|
|
private _secretKey: Buffer;
|
2018-08-23 10:07:30 -07:00
|
|
|
|
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
|
|
|
|
*/
|
2022-11-28 23:42:58 -08:00
|
|
|
constructor(secretKey?: Uint8Array | Array<number>) {
|
2018-08-22 17:03:50 -07:00
|
|
|
if (secretKey) {
|
2022-08-28 12:11:34 -07:00
|
|
|
const secretKeyBuffer = toBuffer(secretKey);
|
|
|
|
if (secretKey.length !== 64) {
|
|
|
|
throw new Error('bad secret key size');
|
|
|
|
}
|
|
|
|
this._publicKey = secretKeyBuffer.slice(32, 64);
|
|
|
|
this._secretKey = secretKeyBuffer.slice(0, 32);
|
2018-08-22 17:03:50 -07:00
|
|
|
} else {
|
2022-08-28 12:11:34 -07:00
|
|
|
this._secretKey = toBuffer(generatePrivateKey());
|
|
|
|
this._publicKey = toBuffer(getPublicKey(this._secretKey));
|
2018-08-22 17:03:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-08-28 12:11:34 -07:00
|
|
|
return new PublicKey(this._publicKey);
|
2018-08-22 17:03:50 -07:00
|
|
|
}
|
|
|
|
|
2018-08-24 09:05:23 -07:00
|
|
|
/**
|
2022-08-28 12:11:34 -07:00
|
|
|
* The **unencrypted** secret key for this account. The first 32 bytes
|
|
|
|
* is the private scalar and the last 32 bytes is the public key.
|
|
|
|
* Read more: https://blog.mozilla.org/warner/2011/11/29/ed25519-keys/
|
2018-08-24 09:05:23 -07:00
|
|
|
*/
|
2018-08-23 10:07:30 -07:00
|
|
|
get secretKey(): Buffer {
|
2022-08-28 12:11:34 -07:00
|
|
|
return Buffer.concat([this._secretKey, this._publicKey], 64);
|
2018-08-22 17:03:50 -07:00
|
|
|
}
|
|
|
|
}
|