@xrplkit/xls26 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +12 -0
- package/readme.md +55 -0
- package/xls26.js +118 -0
package/package.json
ADDED
package/readme.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# XLS-26 Parsing Library
|
|
2
|
+
|
|
3
|
+
This is an implementation of the [XLS-26 Standard](https://github.com/XRPLF/XRPL-Standards/discussions/71).
|
|
4
|
+
This package exports one single function called `parse` that converts a string of a `xrp-ledger.toml` file to a JavaScript object.
|
|
5
|
+
|
|
6
|
+
## Example
|
|
7
|
+
Assuming you have a file named `xrp-ledger.toml` in the current working directory. [Example File here](http://xrpl.works/.well-known/xrp-ledger.toml)
|
|
8
|
+
|
|
9
|
+
import fs from 'fs'
|
|
10
|
+
import { parse } from '@xrplkit/xls26'
|
|
11
|
+
|
|
12
|
+
const tomlString = fs.readFileSync('./xrp-ledger.toml', 'utf-8')
|
|
13
|
+
const xls26Data = parse(tomlString)
|
|
14
|
+
|
|
15
|
+
console.log(xls26Data)
|
|
16
|
+
|
|
17
|
+
### Example Input
|
|
18
|
+
|
|
19
|
+
[[ACCOUNTS]]
|
|
20
|
+
address = "rxworksy7717V3w1nSQhUpaGNqydGYCaS"
|
|
21
|
+
name = "XRPL Works"
|
|
22
|
+
websites = ["https://xrpl.works"]
|
|
23
|
+
description = "XRPL Works is a non-profit organization. Our goal is to simplify the ledger."
|
|
24
|
+
|
|
25
|
+
[[CURRENCIES]]
|
|
26
|
+
code = "58574F524B530000000000000000000000000000"
|
|
27
|
+
issuer = "rxworksy7717V3w1nSQhUpaGNqydGYCaS"
|
|
28
|
+
name = "XWORKS"
|
|
29
|
+
icon = "https://xrpl.works/token/icon.png"
|
|
30
|
+
description = "This token serves the purpose of demonstrating the benefits of the XLS-26 standard."
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
### Example Output
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
{
|
|
37
|
+
"accounts": [
|
|
38
|
+
{
|
|
39
|
+
"address": "rxworksy7717V3w1nSQhUpaGNqydGYCaS",
|
|
40
|
+
"name": "XRPL Works",
|
|
41
|
+
"description": "XRPL Works is a non-profit organization. Our goal is to simplify the ledger."
|
|
42
|
+
}
|
|
43
|
+
],
|
|
44
|
+
"currencies": [
|
|
45
|
+
{
|
|
46
|
+
"code": "58574F524B530000000000000000000000000000",
|
|
47
|
+
"issuer": "rxworksy7717V3w1nSQhUpaGNqydGYCaS",
|
|
48
|
+
"name": "XWORKS",
|
|
49
|
+
"description": "This token serves the purpose of demonstrating the benefits of the XLS-26 standard.",
|
|
50
|
+
"icon": "https://xrpl.works/token/icon.png"
|
|
51
|
+
}
|
|
52
|
+
]
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
```
|
package/xls26.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// The XLS-26 standard adds additional asset metadata fields to the existing xrp-ledger.toml standard,
|
|
2
|
+
// https://github.com/XRPLF/XRPL-Standards/discussions/71
|
|
3
|
+
// This package provides an implementation of a parser according to this standard.
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
import { parse as parseToml } from '@xrplkit/toml'
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
const accountFields = [
|
|
10
|
+
{
|
|
11
|
+
key: 'address',
|
|
12
|
+
validate: v => /^[rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz]{25,35}$/.test(v)
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
key: 'name',
|
|
16
|
+
validate: v => typeof v === 'string' && v.length > 0
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
key: 'description',
|
|
20
|
+
alternativeKeys: ['desc'],
|
|
21
|
+
validate: v => typeof v === 'string' && v.length > 0
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
key: 'icon',
|
|
25
|
+
validate: v => /^https?:\/\/.*$/.test(v)
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
key: 'links',
|
|
29
|
+
type: 'array',
|
|
30
|
+
validate: v => Array.isArray(v) && v.every(v => typeof v === 'string'),
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
key: 'trusted',
|
|
34
|
+
validate: v => typeof v === 'boolean'
|
|
35
|
+
}
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
const currencyFields = [
|
|
40
|
+
{
|
|
41
|
+
key: 'code',
|
|
42
|
+
validate: v => /^[0-9A-F]{40}$/
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
key: 'issuer',
|
|
46
|
+
validate: v => /^[rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz]{25,35}$/.test(v)
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
key: 'name',
|
|
50
|
+
validate: v => typeof v === 'string' && v.length > 0
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
key: 'description',
|
|
54
|
+
alternativeKeys: ['desc'],
|
|
55
|
+
validate: v => typeof v === 'string' && v.length > 0
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
key: 'icon',
|
|
59
|
+
validate: v => /^https?:\/\/.*$/.test(v)
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
key: 'links',
|
|
63
|
+
type: 'array',
|
|
64
|
+
validate: v => Array.isArray(v) && v.every(v => typeof v === 'string'),
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
key: 'trusted',
|
|
68
|
+
validate: v => typeof v === 'boolean'
|
|
69
|
+
}
|
|
70
|
+
]
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
export function parse(str){
|
|
74
|
+
let toml = parseToml(str, 'camelCase')
|
|
75
|
+
let accounts = []
|
|
76
|
+
let currencies = []
|
|
77
|
+
|
|
78
|
+
if(toml.accounts){
|
|
79
|
+
accounts = toml.accounts
|
|
80
|
+
.map(account => parseStanza(account, accountFields))
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if(toml.currencies){
|
|
84
|
+
currencies = toml.currencies
|
|
85
|
+
.map(currency => parseStanza(currency, currencyFields))
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
accounts,
|
|
90
|
+
currencies
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function parseStanza(stanza, schemas){
|
|
95
|
+
let parsed = {}
|
|
96
|
+
|
|
97
|
+
for(let { key, alternativeKeys, validate } of schemas){
|
|
98
|
+
let keys = [key]
|
|
99
|
+
|
|
100
|
+
if(alternativeKeys)
|
|
101
|
+
keys.push(...alternativeKeys)
|
|
102
|
+
|
|
103
|
+
for(let k of keys){
|
|
104
|
+
if(!stanza.hasOwnProperty(k))
|
|
105
|
+
continue
|
|
106
|
+
|
|
107
|
+
let value = stanza[k]
|
|
108
|
+
|
|
109
|
+
if(validate && !validate(value))
|
|
110
|
+
continue
|
|
111
|
+
|
|
112
|
+
parsed[k] = value
|
|
113
|
+
break
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return parsed
|
|
118
|
+
}
|