Commit 94d52f5f authored by Zhenzhen Zhan's avatar Zhenzhen Zhan
Browse files

Address conversion

parent dcd53086
......@@ -21,19 +21,19 @@ describe('URI', function() {
expect(uri.amount).to.be.undefined();
expect(uri.otherParam).to.be.undefined();
uri = URI.parse('crown:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj');
uri.address.should.equal('1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj');
uri = URI.parse('crown:CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr');
uri.address.should.equal('CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr');
expect(uri.amount).to.be.undefined();
expect(uri.otherParam).to.be.undefined();
uri = URI.parse('crown:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj?amount=123.22');
uri.address.should.equal('1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj');
uri = URI.parse('crown:CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr?amount=123.22');
uri.address.should.equal('CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr');
uri.amount.should.equal('123.22');
expect(uri.otherParam).to.be.undefined();
uri = URI.parse('crown:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj?amount=123.22' +
uri = URI.parse('crown:CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr?amount=123.22' +
'&other-param=something&req-extra=param');
uri.address.should.equal('1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj');
uri.address.should.equal('CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr');
uri.amount.should.equal('123.22');
uri['other-param'].should.equal('something');
uri['req-extra'].should.equal('param');
......@@ -41,22 +41,22 @@ describe('URI', function() {
// TODO: Split this and explain tests
it('URIs can be validated statically (test vector)', function() {
URI.isValid('crown:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj').should.equal(true);
URI.isValid('crown:mkYY5NRvikVBY1EPtaq9fAFgquesdjqECw').should.equal(true);
URI.isValid('crown:CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr').should.equal(true);
URI.isValid('crown:tCRWJ1xtvg45BPWPry9gQXH5dcwotsZ8nTgwC').should.equal(true);
URI.isValid('crown:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj?amount=1.2')
URI.isValid('crown:CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr?amount=1.2')
.should.equal(true);
URI.isValid('crown:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj?amount=1.2&other=param')
URI.isValid('crown:CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr?amount=1.2&other=param')
.should.equal(true);
URI.isValid('crown:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj?amount=1.2&req-other=param',
URI.isValid('crown:CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr?amount=1.2&req-other=param',
['req-other']).should.equal(true);
URI.isValid('crown:mmrqEBJxUCf42vdb3oozZtyz5mKr3Vb2Em?amount=0.1&' +
URI.isValid('crown:tCRWKLG3jZ5pdZNtnNLqdW7zNMF3kYXPLUhSH?amount=0.1&' +
'r=https%3A%2F%2Ftest.bitpay.com%2Fi%2F6DKgf8cnJC388irbXk5hHu').should.equal(true);
URI.isValid('crown:').should.equal(false);
URI.isValid('crown:badUri').should.equal(false);
URI.isValid('crown:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfk?amount=bad').should.equal(false);
URI.isValid('crown:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfk?amount=1.2&req-other=param')
URI.isValid('crown:CRWEvBLjAdz9RFdedy74EQx2xQFx6Up9LrV6?amount=bad').should.equal(false);
URI.isValid('crown:CRWEvBLjAdz9RFdedy74EQx2xQFx6Up9LrV6?amount=1.2&req-other=param')
.should.equal(false);
URI.isValid('crown:?r=https%3A%2F%2Ftest.bitpay.com%2Fi%2F6DKgf8cnJC388irbXk5hHu')
.should.equal(false);
......@@ -69,7 +69,7 @@ describe('URI', function() {
});
it('do not need new keyword', function() {
var uri = URI('crown:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj');
var uri = URI('crown:CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr');
uri.should.be.instanceof(URI);
});
......@@ -78,26 +78,26 @@ describe('URI', function() {
var uri;
it('parses address', function() {
uri = new URI('crown:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj');
uri = new URI('crown:CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr');
uri.address.should.be.instanceof(bitcore.Address);
uri.network.should.equal(Networks.livenet);
});
it('parses amount', function() {
uri = URI.fromString('crown:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj?amount=123.22');
uri.address.toString().should.equal('1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj');
uri = URI.fromString('crown:CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr?amount=123.22');
uri.address.toString().should.equal('CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr');
uri.amount.should.equal(12322000000);
expect(uri.otherParam).to.be.undefined();
});
it('parses a testnet address', function() {
uri = new URI('crown:mkYY5NRvikVBY1EPtaq9fAFgquesdjqECw');
uri = new URI('crown:tCRWJ1xtvg45BPWPry9gQXH5dcwotsZ8nTgwC');
uri.address.should.be.instanceof(bitcore.Address);
uri.network.should.equal(Networks.testnet);
});
it('stores unknown parameters as "extras"', function() {
uri = new URI('crown:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj?amount=1.2&other=param');
uri = new URI('crown:CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr?amount=1.2&other=param');
uri.address.should.be.instanceof(bitcore.Address);
expect(uri.other).to.be.undefined();
uri.extras.other.should.equal('param');
......@@ -105,12 +105,12 @@ describe('URI', function() {
it('throws error when a required feature is not supported', function() {
(function() {
return new URI('crown:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj?amount=1.2&other=param&req-required=param');
return new URI('crown:CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr?amount=1.2&other=param&req-required=param');
}).should.throw(Error);
});
it('has no false negative when checking supported features', function() {
uri = new URI('crown:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj?amount=1.2&other=param&' +
uri = new URI('crown:CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr?amount=1.2&other=param&' +
'req-required=param', ['req-required']);
uri.address.should.be.instanceof(bitcore.Address);
uri.amount.should.equal(120000000);
......@@ -125,19 +125,19 @@ describe('URI', function() {
var uri;
uri = new URI({
address: '1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj'
address: 'CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr'
});
uri.address.should.be.instanceof(bitcore.Address);
uri.network.should.equal(Networks.livenet);
uri = new URI({
address: 'mkYY5NRvikVBY1EPtaq9fAFgquesdjqECw'
address: 'tCRWJ1xtvg45BPWPry9gQXH5dcwotsZ8nTgwC'
});
uri.address.should.be.instanceof(bitcore.Address);
uri.network.should.equal(Networks.testnet);
uri = new URI({
address: '1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj',
address: 'CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr',
amount: 120000000,
other: 'param'
});
......@@ -148,13 +148,13 @@ describe('URI', function() {
(function() {
return new URI({
address: '1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj',
address: 'CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr',
'req-required': 'param'
});
}).should.throw(Error);
uri = new URI({
address: '1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj',
address: 'CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr',
amount: 120000000,
other: 'param',
'req-required': 'param'
......@@ -166,19 +166,19 @@ describe('URI', function() {
});
it('should support double slash scheme', function() {
var uri = new URI('crown://1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj');
uri.address.toString().should.equal('1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj');
var uri = new URI('crown://CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr');
uri.address.toString().should.equal('CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr');
});
it('should input/output String', function() {
var str = 'crown:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj?' +
var str = 'crown:CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr?' +
'message=Donation%20for%20project%20xyz&label=myLabel&other=xD';
URI.fromString(str).toString().should.equal(str);
});
it('should input/output JSON', function() {
var json = JSON.stringify({
address: '1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj',
address: 'CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr',
message: 'Donation for project xyz',
label: 'myLabel',
other: 'xD'
......@@ -187,12 +187,12 @@ describe('URI', function() {
});
it('should support numeric amounts', function() {
var uri = new URI('crown:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj?amount=12.10001');
var uri = new URI('crown:CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr?amount=12.10001');
expect(uri.amount).to.be.equal(1210001000);
});
it('should support extra arguments', function() {
var uri = new URI('crown:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj?' +
var uri = new URI('crown:CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr?' +
'message=Donation%20for%20project%20xyz&label=myLabel&other=xD');
should.exist(uri.message);
......@@ -207,38 +207,38 @@ describe('URI', function() {
it('should generate a valid URI', function() {
new URI({
address: '1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj',
address: 'CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr',
}).toString().should.equal(
'crown:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj'
'crown:CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr'
);
new URI({
address: '1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj',
address: 'CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr',
amount: 110001000,
message: 'Hello World',
something: 'else'
}).toString().should.equal(
'crown:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj?amount=1.10001&message=Hello%20World&something=else'
'crown:CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr?amount=1.10001&message=Hello%20World&something=else'
);
});
it('should be case insensitive to protocol', function() {
var uri1 = new URI('cROwn:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj');
var uri2 = new URI('crown:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj');
var uri1 = new URI('cROwn:CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr');
var uri2 = new URI('crown:CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr');
uri1.address.toString().should.equal(uri2.address.toString());
});
it('writes correctly the "r" parameter on string serialization', function() {
var originalString = 'crown:mmrqEBJxUCf42vdb3oozZtyz5mKr3Vb2Em?amount=0.1&' +
var originalString = 'crown:tCRWKLG3jZ5pdZNtnNLqdW7zNMF3kYXPLUhSH?amount=0.1&' +
'r=https%3A%2F%2Ftest.bitpay.com%2Fi%2F6DKgf8cnJC388irbXk5hHu';
var uri = new URI(originalString);
uri.toString().should.equal(originalString);
});
it('displays nicely on the console (#inspect)', function() {
var uri = 'crown:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj';
var uri = 'crown:CRWQM5UEcp33bLDo9b6He1mdY7yAnFvAxZGr';
var instance = new URI(uri);
instance.inspect().should.equal('<URI: ' + uri + '>');
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment