-
Notifications
You must be signed in to change notification settings - Fork 297
/
transaction.js
73 lines (65 loc) · 2.6 KB
/
transaction.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
65
66
67
68
69
70
71
72
73
import {xdr, hash, encodeBase58Check} from "stellar-base";
import {Account} from "./account";
import {Operation} from "./operation";
let MAX_FEE = 1000;
let MIN_LEDGER = 0;
let MAX_LEDGER = 0xFFFFFFFF; // max uint32
export class Transaction {
/**
* A new Transaction object is created from a transaction envelope (or via TransactionBuilder).
* One a Transaction has been created from an envelope, its attributes and operations
* should not be changed. You should only add signers to a Transaction object before
* submitting to the network or forwarding on to additional signers.
* @constructor
* @param {string|xdr.TransactionEnvelope} envelope - The transaction envelope object or
* hex encoded string.
*/
constructor(envelope) {
if (typeof envelope === "string") {
let buffer = new Buffer(envelope, "hex");
envelope = xdr.TransactionEnvelope.fromXdr(buffer);
}
// since this transaction is immutable, save the tx
this.tx = envelope._attributes.tx;
this.source = encodeBase58Check("accountId", envelope._attributes.tx._attributes.sourceAccount);
this.fee = envelope._attributes.tx._attributes.fee;
this.sequence = envelope._attributes.tx._attributes.seqNum.toString();
this.minLedger = envelope._attributes.tx._attributes.minLedger;
this.maxLedger = envelope._attributes.tx._attributes.maxLedger;
let operations = envelope._attributes.tx._attributes.operations;
this.operations = [];
for (let i = 0; i < operations.length; i++) {
this.operations[i] = Operation.operationToObject(operations[i]._attributes);
}
let signatures = envelope._attributes.signatures;
this.signatures = [];
for (let i = 0; i < signatures.length; i++) {
this.signatures[i] = signatures[i];
}
}
/**
* Adds a signature to this transaction.
* @param signature
*/
addSignature(signature) {
this.signatures.push(signature);
}
/**
* Signs the transaction with the given Keypair.
* @param {Keypair} keypair
*/
sign(keypair) {
let tx_raw = this.tx.toXDR();
let tx_hash = hash(tx_raw);
return keypair.signDecorated(tx_hash);
}
/**
* To envelope returns a xdr.TransactionEnvelope which can be submitted to the network.
*/
toEnvelope() {
let tx = this.tx;
let signatures = this.signatures;
let envelope = new xdr.TransactionEnvelope({tx, signatures});
return envelope;
}
}