forked from stellar/js-stellar-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_account.js
37 lines (34 loc) · 1.54 KB
/
create_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
import xdr from '../generated/stellar-xdr_generated';
import { Keypair } from '../keypair';
import { StrKey } from '../strkey';
/**
* Create and fund a non existent account.
* @function
* @alias Operation.createAccount
* @param {object} opts Options object
* @param {string} opts.destination - Destination account ID to create an account for.
* @param {string} opts.startingBalance - Amount in XLM the account should be funded for. Must be greater
* than the [reserve balance amount](https://www.stellar.org/developers/guides/concepts/fees.html).
* @param {string} [opts.source] - The source account for the payment. Defaults to the transaction's source account.
* @returns {xdr.CreateAccountOp} Create account operation
*/
export function createAccount(opts) {
if (!StrKey.isValidEd25519PublicKey(opts.destination)) {
throw new Error('destination is invalid');
}
if (!this.isValidAmount(opts.startingBalance, true)) {
throw new TypeError(
'startingBalance must be of type String, represent a non-negative number and have at most 7 digits after the decimal'
);
}
const attributes = {};
attributes.destination = Keypair.fromPublicKey(
opts.destination
).xdrAccountId();
attributes.startingBalance = this._toXDRAmount(opts.startingBalance);
const createAccountOp = new xdr.CreateAccountOp(attributes);
const opAttributes = {};
opAttributes.body = xdr.OperationBody.createAccount(createAccountOp);
this.setSourceAccount(opAttributes, opts);
return new xdr.Operation(opAttributes);
}