Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates #2

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,44 @@ Here is an example of how to use the `Payto-RL` package:
```typescript
import Payto from 'payto-rl';

const paytoString = 'payto://btc/1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa?amount=0.1&fiat=usd';
const paytoString = 'payto://xcb/cb7147879011ea207df5b35a24ca6f0859dcfb145999?amount=ctn:10.01&fiat=eur';
const payto = new Payto(paytoString);

console.log(payto.address); // Outputs: 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa
console.log(payto.amount); // Outputs: 0.1
console.log(payto.currency); // Outputs: ['btc', 'usd']
console.log(payto.address); // Outputs: cb7147879011ea207df5b35a24ca6f0859dcfb145999
console.log(payto.amount); // Outputs: ctn:10.01
console.log(payto.value); // Outputs: 10.01
console.log(payto.network); // Outputs: xcb
console.log(payto.currency); // Outputs: ['ctn', 'eur']

payto.amount = '0.2';
console.log(payto.amount); // Outputs: 0.2
payto.value = 20.02;
console.log(payto.amount); // Outputs: ctn:20.02
console.log(payto.value); // Outputs: 20.02
console.log(payto.fiat); // Outputs: eur
// Then it should be payed 20.02 EUR in CTN to the address cb7147879011ea207df5b35a24ca6f0859dcfb145999 on the XCB network.

console.log(payto.toJSONObject());

/*
Outputs:
{
address: '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa',
amount: '0.2',
currency: 'btc',
fiat: 'usd',
host: 'btc',
hostname: 'btc',
href: 'payto://btc/1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa?amount=0.2&fiat=usd',
network: 'btc',
pathname: '/1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa',
address: 'cb7147879011ea207df5b35a24ca6f0859dcfb145999',
amount: 'ctn:20.02',
asset: 'ctn',
fiat: 'eur',
host: 'xcb',
hostname: 'xcb',
href: 'payto://xcb/cb7147879011ea207df5b35a24ca6f0859dcfb145999?amount=ctn:10.01&fiat=eur',
network: 'xcb',
pathname: '/cb7147879011ea207df5b35a24ca6f0859dcfb145999',
protocol: 'payto:',
search: '?amount=0.2&fiat=usd'
search: '?amount=ctn:10.01&fiat=eur',
value: 20.02
}
*/

payto.currency = ['btc', 'usd', 12];
console.log(payto.currency); // Outputs: ['btc', 'usd']
console.log(payto.value); // Outputs: 12
```

## API
Expand All @@ -80,6 +90,10 @@ Gets or sets the address component of the PRL.

Gets or sets the amount component of the PRL. Amount consists of the number of units and the currency delimited by `:`.

##### `asset: string | null`

Gets or sets the asset component of the PRL. Asset is the currency in the amount field. Can be ticker or smart contract address.

##### `bic: string | null`

Gets or sets the BIC component of the PRL.
Expand Down
8 changes: 5 additions & 3 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ declare class Payto {
set address(value: string | null);
get amount(): string | null;
set amount(value: string | null);
get asset(): string | null;
set asset(value: string | undefined);
get bic(): string | null;
set bic(value: string | null);
get currency(): [string | null, string | null];
set currency(value: [number?, string?, string?]);
set currency(value: [string?, string?, number?]);
get deadline(): string | null;
set deadline(value: string | null);
get donate(): string | null;
Expand Down Expand Up @@ -55,10 +57,10 @@ declare class Payto {
get searchParams(): URLSearchParams;
get split(): [string, string, boolean] | null;
set split(value: [string, string, boolean] | null);
get value(): number | null;
set value(value: number | null);
get username(): string;
set username(value: string);
get value(): number | null;
set value(value: number | null);
toString(): string;
toJSON(): string;
toJSONObject(): object;
Expand Down
41 changes: 28 additions & 13 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ class Payto {
this.searchParams.delete('amount');
}
}
get asset() {
return this.currency[0];
}
set asset(value) {
this.currency = [value];
}
get bic() {
return this.getHostnameParts(this.pathname.split('/'), 'bic', 2);
}
Expand All @@ -60,14 +66,26 @@ class Payto {
return result;
}
set currency(value) {
const [amount, token, fiat] = value;
const [token, fiat, amount] = value;
const amountValue = this.searchParams.get('amount');
let oldToken, oldValue;
if (amountValue) {
const amountArray = amountValue.split(':');
if (amountArray[1]) {
oldToken = amountArray[0];
oldValue = amountArray[1];
}
else {
oldValue = amountArray[0];
}
}
if (fiat)
this.fiat = fiat.toLowerCase();
if (token) {
this.amount = `${token}:${amount ?? ''}`;
this.amount = `${token}:${amount || (oldValue || '')}`;
}
else if (amount) {
this.amount = `${amount}`;
this.amount = `${oldToken ? oldToken + ':' : ''}${amount}`;
}
}
get deadline() {
Expand Down Expand Up @@ -258,6 +276,12 @@ class Payto {
this.searchParams.delete('split');
}
}
get username() {
return this.url.username;
}
set username(value) {
this.url.username = value;
}
get value() {
const amount = this.searchParams.get('amount');
if (amount) {
Expand Down Expand Up @@ -288,15 +312,6 @@ class Payto {
}
}
}
else {
this.searchParams.delete('amount');
}
}
get username() {
return this.url.username;
}
set username(value) {
this.url.username = value;
}
toString() {
return this.url.toString();
Expand All @@ -309,7 +324,7 @@ class Payto {
if (this.port)
obj.port = this.port;
if (this.currency[0])
obj.currency = this.currency[0];
obj.asset = this.currency[0];
if (this.currency[1])
obj.fiat = this.currency[1];
if (this.amount)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "payto-rl",
"version": "1.0.0",
"version": "1.0.1",
"description": "Payto resource locator",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
47 changes: 32 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ class Payto {
}
}

get asset(): string | null {
return this.currency[0];
}

set asset(value: string | undefined) {
this.currency = [value];
}

get bic(): string | null {
return this.getHostnameParts(this.pathname.split('/'), 'bic', 2);
}
Expand All @@ -69,13 +77,24 @@ class Payto {
return result;
}

set currency(value: [number?, string?, string?]) {
const [amount, token, fiat] = value;
set currency(value: [string?, string?, number?]) {
const [token, fiat, amount] = value;
const amountValue = this.searchParams.get('amount');
let oldToken, oldValue;
if (amountValue) {
const amountArray = amountValue.split(':');
if (amountArray[1]) {
oldToken = amountArray[0];
oldValue = amountArray[1];
} else {
oldValue = amountArray[0];
}
}
if (fiat) this.fiat = fiat.toLowerCase();
if (token) {
this.amount = `${token}:${amount ?? ''}`;
this.amount = `${token}:${amount || (oldValue || '')}`;
} else if (amount) {
this.amount = `${amount}`;
this.amount = `${oldToken ? oldToken + ':' : ''}${amount}`;
}
}

Expand Down Expand Up @@ -302,6 +321,14 @@ class Payto {
}
}

get username(): string {
return this.url.username;
}

set username(value: string) {
this.url.username = value;
}

get value(): number | null {
const amount = this.searchParams.get('amount');
if (amount) {
Expand Down Expand Up @@ -330,19 +357,9 @@ class Payto {
this.searchParams.set('amount', value.toString());
}
}
} else {
this.searchParams.delete('amount');
}
}

get username(): string {
return this.url.username;
}

set username(value: string) {
this.url.username = value;
}

toString(): string {
return this.url.toString();
}
Expand All @@ -354,7 +371,7 @@ class Payto {
toJSONObject(): object {
const obj: { [key: string]: any } = {};
if (this.port) obj.port = this.port;
if (this.currency[0]) obj.currency = this.currency[0];
if (this.currency[0]) obj.asset = this.currency[0];
if (this.currency[1]) obj.fiat = this.currency[1];
if (this.amount) obj.amount = this.amount;
if (this.address) obj.address = this.address;
Expand Down
77 changes: 51 additions & 26 deletions test/paytoRl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,48 @@ import Payto from 'payto-rl';
const test = suite('Payto');

test('constructor with valid payto string', () => {
const payto = new Payto('payto://btc/1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa?amount=0.1');
assert.is(payto.href, 'payto://btc/1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa?amount=0.1');
const payto = new Payto('payto://xcb/cb7147879011ea207df5b35a24ca6f0859dcfb145999?amount=ctn:10.01&fiat=eur');
assert.is(payto.href, 'payto://xcb/cb7147879011ea207df5b35a24ca6f0859dcfb145999?amount=ctn:10.01&fiat=eur');
});

test('constructor with invalid protocol', () => {
assert.throws(() => {
new Payto('http://btc/1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa?amount=0.1');
new Payto('http://xcb/cb7147879011ea207df5b35a24ca6f0859dcfb145999?amount=ctn:10.01&fiat=eur');
}, /Invalid protocol, must be payto:/);
});

test('get and set address', () => {
const payto = new Payto('payto://btc/1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa');
assert.is(payto.address, '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa');
payto.address = '1BoatSLRHtKNngkdXEeobR76b53LETtpyT';
assert.is(payto.address, '1BoatSLRHtKNngkdXEeobR76b53LETtpyT');
const payto = new Payto('payto://xcb/cb7147879011ea207df5b35a24ca6f0859dcfb145999');
assert.is(payto.address, 'cb7147879011ea207df5b35a24ca6f0859dcfb145999');
payto.address = 'cb7147879011ea207df5b35a24ca6f0859dcfb145000';
assert.is(payto.address, 'cb7147879011ea207df5b35a24ca6f0859dcfb145000');
});

test('get and set amount', () => {
const payto = new Payto('payto://btc/1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa?amount=0.1');
assert.is(payto.amount, '0.1');
payto.amount = '0.2';
assert.is(payto.amount, '0.2');
const payto = new Payto('payto://xcb/cb7147879011ea207df5b35a24ca6f0859dcfb145999?amount=ctn:10.01');
assert.is(payto.amount, 'ctn:10.01');
payto.amount = 'ctn:20.02';
assert.is(payto.amount, 'ctn:20.02');
});

test('get and set currency', () => {
const payto = new Payto('payto://btc/1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa?amount=btc:0.1&fiat=usd');
assert.equal(payto.currency, ['btc', 'usd']);
payto.currency = [0.2, 'eth', 'eur'];
assert.equal(payto.currency, ['eth', 'eur']);
const payto = new Payto('payto://xcb/cb7147879011ea207df5b35a24ca6f0859dcfb145999?amount=ctn:10.01&fiat=eur');
assert.equal(payto.currency, ['ctn', 'eur']);
payto.currency = ['xxx', 'usd', 20.02];
assert.equal(payto.currency, ['xxx', 'usd']);
payto.currency = ['yyy', 'czk'];
assert.equal(payto.currency, ['yyy', 'czk']);
// check if value is consistent
assert.is(payto.value, 20.02);
});

test('get and set value', () => {
const payto = new Payto('payto://xcb/cb7147879011ea207df5b35a24ca6f0859dcfb145999?amount=ctn:10.01&fiat=eur');
assert.is(payto.value, 10.01);
payto.value = 20.02;
assert.is(payto.value, 20.02);
// check if currency is consistent
assert.equal(payto.currency, ['ctn', 'eur']);
});

test('get and set deadline', () => {
Expand All @@ -52,6 +65,17 @@ test('get and set donate', () => {
assert.is(payto.donate, null);
});

test('get and set asset', () => {
const payto = new Payto('payto://xcb/cb7147879011ea207df5b35a24ca6f0859dcfb145999?amount=ctn:10.01&fiat=eur');
assert.is(payto.asset, 'ctn');
payto.asset = 'xxx';
assert.is(payto.asset, 'xxx');
// check if amount is consistent
assert.is(payto.amount, 'xxx:10.01');
// check if currency is consistent
assert.equal(payto.currency, ['xxx', 'eur']);
});

test('get and set fiat', () => {
const payto = new Payto('payto://btc/1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa?fiat=usd');
assert.is(payto.fiat, 'usd');
Expand All @@ -60,20 +84,21 @@ test('get and set fiat', () => {
});

test('toJSONObject', () => {
const payto = new Payto('payto://btc/1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa?amount=0.1&fiat=usd');
const payto = new Payto('payto://xcb/cb7147879011ea207df5b35a24ca6f0859dcfb145999?amount=ctn:10.01&fiat=eur');
const jsonObject = payto.toJSONObject();
assert.equal(jsonObject, {
address: '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa',
amount: '0.1',
fiat: 'usd',
host: 'btc',
hostname: 'btc',
href: 'payto://btc/1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa?amount=0.1&fiat=usd',
network: 'btc',
pathname: '/1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa',
address: 'cb7147879011ea207df5b35a24ca6f0859dcfb145999',
amount: 'ctn:10.01',
asset: 'ctn',
fiat: 'eur',
host: 'xcb',
hostname: 'xcb',
href: 'payto://xcb/cb7147879011ea207df5b35a24ca6f0859dcfb145999?amount=ctn:10.01&fiat=eur',
network: 'xcb',
pathname: '/cb7147879011ea207df5b35a24ca6f0859dcfb145999',
protocol: 'payto:',
search: '?amount=0.1&fiat=usd',
value: 0.1
search: '?amount=ctn:10.01&fiat=eur',
value: 10.01
});
});

Expand Down
Loading
Loading