convert amout to satoshis
This commit is contained in:
parent
db4561f834
commit
9cd9eeba1f
11
lib/uri.js
11
lib/uri.js
@ -4,6 +4,7 @@ var _ = require('lodash');
|
|||||||
var URL = require('url');
|
var URL = require('url');
|
||||||
|
|
||||||
var Address = require('./address');
|
var Address = require('./address');
|
||||||
|
var Unit = require('./unit');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -36,6 +37,7 @@ var URI = function(data, knownParams) {
|
|||||||
|
|
||||||
if (typeof(data) == 'string') {
|
if (typeof(data) == 'string') {
|
||||||
var params = URI.parse(data);
|
var params = URI.parse(data);
|
||||||
|
if (params.amount) params.amount = this._parseAmount(params.amount);
|
||||||
this._fromObject(params);
|
this._fromObject(params);
|
||||||
} else if (typeof(data) == 'object') {
|
} else if (typeof(data) == 'object') {
|
||||||
this._fromObject(data);
|
this._fromObject(data);
|
||||||
@ -92,7 +94,7 @@ URI.parse = function(uri) {
|
|||||||
*
|
*
|
||||||
* Internal function to load the URI instance with an object.
|
* Internal function to load the URI instance with an object.
|
||||||
*
|
*
|
||||||
* @param {Object} amount - Amount BTC string
|
* @param {Object} obj - Object with the information
|
||||||
* @throws {TypeError} Invalid bitcoin address
|
* @throws {TypeError} Invalid bitcoin address
|
||||||
* @throws {TypeError} Invalid amount
|
* @throws {TypeError} Invalid amount
|
||||||
* @throws {Error} Unknown required argument
|
* @throws {Error} Unknown required argument
|
||||||
@ -104,8 +106,7 @@ URI.prototype._fromObject = function(obj) {
|
|||||||
|
|
||||||
this.address = new Address(obj.address);
|
this.address = new Address(obj.address);
|
||||||
this.network = this.address.network;
|
this.network = this.address.network;
|
||||||
|
this.amount = obj.amount;
|
||||||
if (obj.amount) this.amount = this._parseAmount(obj.amount);
|
|
||||||
|
|
||||||
for (var key in obj) {
|
for (var key in obj) {
|
||||||
if (key === 'address' || key === 'amount') continue;
|
if (key === 'address' || key === 'amount') continue;
|
||||||
@ -130,7 +131,7 @@ URI.prototype._fromObject = function(obj) {
|
|||||||
URI.prototype._parseAmount = function(amount) {
|
URI.prototype._parseAmount = function(amount) {
|
||||||
var amount = Number(amount);
|
var amount = Number(amount);
|
||||||
if (isNaN(amount)) throw new TypeError('Invalid amount');
|
if (isNaN(amount)) throw new TypeError('Invalid amount');
|
||||||
return amount; // TODO: Convert to Satoshis (yemel)
|
return Unit.fromBTC(amount).toSatoshis();
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -141,7 +142,7 @@ URI.prototype._parseAmount = function(amount) {
|
|||||||
*/
|
*/
|
||||||
URI.prototype.toString = function() {
|
URI.prototype.toString = function() {
|
||||||
var query = _.clone(this.extras);
|
var query = _.clone(this.extras);
|
||||||
if (this.amount) query.amount = this.amount; // TODO: Convert to BTC (yemel)
|
if (this.amount) query.amount = Unit.fromSatoshis(this.amount).toBTC();
|
||||||
if (this.message) query.message = this.message;
|
if (this.message) query.message = this.message;
|
||||||
|
|
||||||
return URL.format({
|
return URL.format({
|
||||||
|
|||||||
16
test/uri.js
16
test/uri.js
@ -70,7 +70,7 @@ describe('URI', function() {
|
|||||||
|
|
||||||
uri = new URI('bitcoin:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj?amount=1.2&other=param');
|
uri = new URI('bitcoin:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj?amount=1.2&other=param');
|
||||||
uri.address.should.be.instanceof(bitcore.Address);
|
uri.address.should.be.instanceof(bitcore.Address);
|
||||||
uri.amount.should.equal(1.2);
|
uri.amount.should.equal(120000000);
|
||||||
expect(uri.other).to.be.undefined;
|
expect(uri.other).to.be.undefined;
|
||||||
uri.extras.other.should.equal('param');
|
uri.extras.other.should.equal('param');
|
||||||
|
|
||||||
@ -80,7 +80,7 @@ describe('URI', function() {
|
|||||||
|
|
||||||
uri = new URI('bitcoin:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj?amount=1.2&other=param&req-required=param', ['req-required']);
|
uri = new URI('bitcoin:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj?amount=1.2&other=param&req-required=param', ['req-required']);
|
||||||
uri.address.should.be.instanceof(bitcore.Address);
|
uri.address.should.be.instanceof(bitcore.Address);
|
||||||
uri.amount.should.equal(1.2);
|
uri.amount.should.equal(120000000);
|
||||||
uri.extras.other.should.equal('param');
|
uri.extras.other.should.equal('param');
|
||||||
uri.extras['req-required'].should.equal('param');
|
uri.extras['req-required'].should.equal('param');
|
||||||
});
|
});
|
||||||
@ -103,11 +103,11 @@ describe('URI', function() {
|
|||||||
|
|
||||||
uri = new URI({
|
uri = new URI({
|
||||||
address: '1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj',
|
address: '1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj',
|
||||||
amount: '1.2',
|
amount: 120000000,
|
||||||
other: 'param'
|
other: 'param'
|
||||||
});
|
});
|
||||||
uri.address.should.be.instanceof(bitcore.Address);
|
uri.address.should.be.instanceof(bitcore.Address);
|
||||||
uri.amount.should.equal(1.2);
|
uri.amount.should.equal(120000000);
|
||||||
expect(uri.other).to.be.undefined;
|
expect(uri.other).to.be.undefined;
|
||||||
uri.extras.other.should.equal('param');
|
uri.extras.other.should.equal('param');
|
||||||
|
|
||||||
@ -120,12 +120,12 @@ describe('URI', function() {
|
|||||||
|
|
||||||
uri = new URI({
|
uri = new URI({
|
||||||
address: '1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj',
|
address: '1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj',
|
||||||
amount: 1.2,
|
amount: 120000000,
|
||||||
other: 'param',
|
other: 'param',
|
||||||
'req-required': 'param'
|
'req-required': 'param'
|
||||||
}, ['req-required']);
|
}, ['req-required']);
|
||||||
uri.address.should.be.instanceof(bitcore.Address);
|
uri.address.should.be.instanceof(bitcore.Address);
|
||||||
uri.amount.should.equal(1.2);
|
uri.amount.should.equal(120000000);
|
||||||
uri.extras.other.should.equal('param');
|
uri.extras.other.should.equal('param');
|
||||||
uri.extras['req-required'].should.equal('param');
|
uri.extras['req-required'].should.equal('param');
|
||||||
});
|
});
|
||||||
@ -137,7 +137,7 @@ describe('URI', function() {
|
|||||||
|
|
||||||
it('should support numeric amounts', function() {
|
it('should support numeric amounts', function() {
|
||||||
var uri = new URI('bitcoin:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj?amount=12.10001');
|
var uri = new URI('bitcoin:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj?amount=12.10001');
|
||||||
expect(uri.amount).to.be.equal(12.10001);
|
expect(uri.amount).to.be.equal(1210001000);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should support extra arguments', function() {
|
it('should support extra arguments', function() {
|
||||||
@ -162,7 +162,7 @@ describe('URI', function() {
|
|||||||
|
|
||||||
new URI({
|
new URI({
|
||||||
address: '1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj',
|
address: '1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj',
|
||||||
amount: 1.10001,
|
amount: 110001000,
|
||||||
message: 'Hello World',
|
message: 'Hello World',
|
||||||
something: 'else'
|
something: 'else'
|
||||||
}).toString().should.equal(
|
}).toString().should.equal(
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user