Issues (18)

lib/request_builder.js (1 issue)

Labels
Severity
1 1
var ProtoBuf = require('./protobuf');
2
3
/**
4
 *
5
 * @param txOut
6
 */
7
function checkOutput(txOut) {
8 5
    if (typeof txOut.amount === "undefined") {
9
        throw new Error("Missing Output `value`");
10
    }
11 5
    if (typeof txOut.script === "undefined") {
12
        throw new Error("Missing Output `script`");
13
    }
14 5
    if (!(txOut.script instanceof Buffer)) {
0 ignored issues
show
The variable Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
15
        throw new Error("Output `script` must be a Buffer");
16
    }
17
}
18
19
/**
20
 *
21
 * @constructor
22
 */
23
function RequestBuilder() {
24 9
    this.network = null;
25 9
    this.outputs = [];
26 9
    this.time = null;
27 9
    this.expires = null;
28 9
    this.memo = null;
29 9
    this.paymentUrl = null;
30 9
    this.merchantData = null;
31
}
32
33
/**
34
 *
35
 * @param {string} memo
36
 */
37 1
RequestBuilder.prototype.setMemo = function(memo) {
38 1
    this.memo = memo;
39
};
40
41
/**
42
 *
43
 * @param {string} network
44
 */
45 1
RequestBuilder.prototype.setNetwork = function(network) {
46 3
    this.network = network;
47
};
48
49
/**
50
 *
51
 * @param {array} txOutArray
52
 */
53 1
RequestBuilder.prototype.setOutputs = function(txOutArray) {
54 1
    txOutArray.map(checkOutput);
55 1
    this.outputs = txOutArray;
56
};
57
58
/**
59
 *
60
 * @param {object} txOut
61
 */
62 1
RequestBuilder.prototype.addOutput = function(txOut) {
63 3
    checkOutput(txOut);
64 3
    this.outputs.push(txOut);
65
};
66
67
/**
68
 *
69
 * @param {number} time
70
 */
71 1
RequestBuilder.prototype.setTime = function(time) {
72 9
    this.time = time;
73
};
74
75
/**
76
 *
77
 * @param {number} expireTime
78
 */
79 1
RequestBuilder.prototype.setExpires = function(expireTime) {
80 1
    this.expires = expireTime;
81
};
82
83
/**
84
 *
85
 * @param {string} url
86
 */
87 1
RequestBuilder.prototype.setPaymentUrl = function(url) {
88 1
    this.paymentUrl = url;
89
};
90
91
/**
92
 *
93
 * @param {string} merchantData
94
 */
95 1
RequestBuilder.prototype.setMerchantData = function(merchantData) {
96 1
    this.merchantData = merchantData;
97
};
98
99
/**
100
 * @return ProtoBuf.PaymentDetails
101
 */
102 1
RequestBuilder.prototype.buildDetails = function() {
103 3
    if (null === this.time) {
104
        throw new Error("Missing `time` for PaymentDetails");
105
    }
106 3
    if (this.outputs.length < 1) {
107
        throw new Error("Missing `outputs` for PaymentDetails");
108
    }
109
110 3
    return ProtoBuf.PaymentDetails.create(this);
111
};
112
113
/**
114
 *
115
 */
116 1
RequestBuilder.prototype.buildRequest = function() {
117 1
    var details = this.buildDetails();
118
119 1
    return ProtoBuf.PaymentRequest.create({
120
        serialized_payment_details: ProtoBuf.PaymentDetails.encode(details).finish()
121
    });
122
};
123
124
exports = module.exports = RequestBuilder;
125