-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathaccount.js
64 lines (58 loc) · 1.73 KB
/
account.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { StrKey } from './strkey';
/**
* Create a new Account object.
*
* `Account` represents a single account in the Stellar network and its sequence
* number. Account tracks the sequence number as it is used by {@link
* TransactionBuilder}. See
* [Accounts](https://developers.stellar.org/docs/glossary/accounts/) for
* more information about how accounts work in Stellar.
*
* @constructor
*
* @param {string} accountId - ID of the account (ex.
* `GB3KJPLFUYN5VL6R3GU3EGCGVCKFDSD7BEDX42HWG5BWFKB3KQGJJRMA`). If you
* provide a muxed account address, this will throw; use {@link
* MuxedAccount} instead.
* @param {string} sequence - current sequence number of the account
*/
export class Account {
constructor(accountId, sequence) {
if (StrKey.isValidMed25519PublicKey(accountId)) {
throw new Error('accountId is an M-address; use MuxedAccount instead');
}
if (!StrKey.isValidEd25519PublicKey(accountId)) {
throw new Error('accountId is invalid');
}
if (!(typeof sequence === 'string')) {
throw new Error('sequence must be of type string');
}
this._accountId = accountId;
try {
this.sequence = BigInt(sequence);
} catch (e) {
throw new Error(`sequence is not a number: ${sequence}`);
}
}
/**
* Returns Stellar account ID, ex.
* `GB3KJPLFUYN5VL6R3GU3EGCGVCKFDSD7BEDX42HWG5BWFKB3KQGJJRMA`.
* @returns {string}
*/
accountId() {
return this._accountId;
}
/**
* @returns {string} sequence number for the account as a string
*/
sequenceNumber() {
return this.sequence.toString();
}
/**
* Increments sequence number in this object by one.
* @returns {void}
*/
incrementSequenceNumber() {
this.sequence += 1n;
}
}