UNPKG

7.17 kBMarkdownView Raw
1# ripple-address-codec
2
3[![NPM Version][npm-version-image]][npm-url]
4[![NPM Downloads][npm-downloads-image]][npm-url]
5[![Build Status][travis-image]][travis-url]
6[![Test Coverage][coveralls-image]][coveralls-url]
7
8Functions for encoding and decoding XRP Ledger addresses and seeds.
9
10Also includes support for encoding/decoding [rippled validator (node) public keys](https://xrpl.org/run-rippled-as-a-validator.html).
11
12[![NPM](https://nodei.co/npm/ripple-address-codec.png)](https://www.npmjs.org/package/ripple-address-codec)
13
14## X-address Conversion
15
16All tools and apps in the XRP Ledger ecosystem are encouraged to adopt support for the X-address format. The X-address format is a single Base58 string that encodes an 'Account ID', a (destination) tag, and whether the address is intended for a test network. This prevents users from unintentionally omitting the destination tag when sending and receiving payments and other transactions.
17
18## API
19
20### classicAddressToXAddress(classicAddress: string, tag: number | false, test: boolean): string
21
22Convert a classic address and (optional) tag to an X-address. If `tag` is `false`, the returned X-address explicitly indicates that the recipient does not want a tag to be used. If `test` is `true`, consumers of the address will know that the address is intended for use on test network(s) and the address will start with `T`.
23
24```js
25> const api = require('ripple-address-codec')
26> api.classicAddressToXAddress('rGWrZyQqhTp9Xu7G5Pkayo7bXjH4k4QYpf', 4294967295)
27'XVLhHMPHU98es4dbozjVtdWzVrDjtV18pX8yuPT7y4xaEHi'
28```
29
30Encode a test address e.g. for use with [Testnet or Devnet](https://xrpl.org/xrp-testnet-faucet.html):
31
32```js
33> const api = require('ripple-address-codec')
34> api.classicAddressToXAddress('r3SVzk8ApofDJuVBPKdmbbLjWGCCXpBQ2g', 123, true)
35'T7oKJ3q7s94kDH6tpkBowhetT1JKfcfdSCmAXbS75iATyLD'
36```
37
38### xAddressToClassicAddress(xAddress: string): {classicAddress: string, tag: number | false, test: boolean}
39
40Convert an X-address to a classic address and tag. If the X-address did not have a tag, the returned object's `tag` will be `false`. (Since `0` is a valid tag, instead of `if (tag)`, use `if (tag !== false)` if you want to check for a tag.) If the X-address is intended for use on test network(s), `test` will be `true`; if it is intended for use on the main network (mainnet), `test` will be `false`.
41
42```js
43> const api = require('ripple-address-codec')
44> api.xAddressToClassicAddress('XVLhHMPHU98es4dbozjVtdWzVrDjtV18pX8yuPT7y4xaEHi')
45{
46 classicAddress: 'rGWrZyQqhTp9Xu7G5Pkayo7bXjH4k4QYpf',
47 tag: 4294967295,
48 test: false
49}
50```
51
52### isValidXAddress(xAddress: string): boolean
53
54Returns `true` if the provided X-address is valid, or `false` otherwise.
55
56```js
57> const api = require('ripple-address-codec')
58> api.isValidXAddress('XVLhHMPHU98es4dbozjVtdWzVrDjtV18pX8yuPT7y4xaEHi')
59true
60```
61
62Returns `false` for classic addresses (starting with `r`). To validate a classic address, use `isValidClassicAddress`.
63
64### isValidClassicAddress(address: string): boolean
65
66Check whether a classic address (starting with `r`...) is valid.
67
68Returns `false` for X-addresses (extended addresses). To validate an X-address, use `isValidXAddress`.
69
70### encodeSeed(entropy: Buffer, type: 'ed25519' | 'secp256k1'): string
71
72Encode the given entropy as an XRP Ledger seed (secret). The entropy must be exactly 16 bytes (128 bits). The encoding includes which elliptic curve digital signature algorithm (ECDSA) the seed is intended to be used with. The seed is used to produce the private key.
73
74### decodeSeed(seed: string): object
75
76Decode a seed into an object with its version, type, and bytes.
77
78Return object type:
79```
80{
81 version: number[],
82 bytes: Buffer,
83 type: string | null
84}
85```
86
87### encodeAccountID(bytes: Buffer): string
88
89Encode bytes as a classic address (starting with `r`...).
90
91### decodeAccountID(accountId: string): Buffer
92
93Decode a classic address (starting with `r`...) to its raw bytes.
94
95### encodeNodePublic(bytes: Buffer): string
96
97Encode bytes to the XRP Ledger "node public key" format (base58).
98
99This is useful for rippled validators.
100
101### decodeNodePublic(base58string: string): Buffer
102
103Decode an XRP Ledger "node public key" (in base58 format) into its raw bytes.
104
105### encodeAccountPublic(bytes: Buffer): string
106
107Encode a public key, as for payment channels.
108
109### decodeAccountPublic(base58string: string): Buffer
110
111Decode a public key, as for payment channels.
112
113### encodeXAddress(accountId: Buffer, tag: number | false, test: boolean): string
114
115Encode account ID, tag, and network ID to X-address.
116
117`accountId` must be 20 bytes because it is a RIPEMD160 hash, which is 160 bits (160 bits = 20 bytes).
118
119At this time, `tag` must be <= MAX_32_BIT_UNSIGNED_INT (4294967295) as the XRP Ledger only supports 32-bit tags.
120
121If `test` is `true`, this address is intended for use with a test network such as Testnet or Devnet.
122
123### decodeXAddress(xAddress: string): {accountId: Buffer, tag: number | false, test: boolean}
124
125Convert an X-address to its classic address, tag, and network ID.
126
127### Other functions
128
129```js
130> var api = require('ripple-address-codec');
131> api.decodeSeed('sEdTM1uX8pu2do5XvTnutH6HsouMaM2')
132{ version: [ 1, 225, 75 ],
133 bytes: [ 76, 58, 29, 33, 63, 189, 251, 20, 199, 194, 141, 96, 148, 105, 179, 65 ],
134 type: 'ed25519' }
135> api.decodeSeed('sn259rEFXrQrWyx3Q7XneWcwV6dfL')
136{ version: 33,
137 bytes: [ 207, 45, 227, 120, 251, 221, 126, 46, 232, 125, 72, 109, 251, 90, 123, 255 ],
138 type: 'secp256k1' }
139> api.decodeAccountID('rJrRMgiRgrU6hDF4pgu5DXQdWyPbY35ErN')
140[ 186,
141 142,
142 120,
143 98,
144 110,
145 228,
146 44,
147 65,
148 180,
149 109,
150 70,
151 195,
152 4,
153 141,
154 243,
155 161,
156 195,
157 200,
158 112,
159 114 ]
160```
161
162## Tests
163
164Run unit tests with:
165
166 npm test
167
168Use `--watch` to run in watch mode, so that when you modify the tests, they are automatically re-run:
169
170 npm test -- --watch
171
172Use `--coverage` to generate and display code coverage information:
173
174 npm test -- --coverage
175
176This tells jest to output code coverage info in the `./coverage` directory, in addition to showing it on the command line.
177
178## Releases
179
180Use the [xrpl.js release process](https://github.com/XRPLF/xrpl.js/blob/main/CONTRIBUTING.md#release).
181
182## Acknowledgements
183
184This library references and adopts code and standards from the following sources:
185
186- [XLS-5d Standard for Tagged Addresses](https://github.com/xrp-community/standards-drafts/issues/6) by @nbougalis
187- [XRPL Tagged Address Codec](https://github.com/xrp-community/xrpl-tagged-address-codec) by @WietseWind
188- [X-Address transaction functions](https://github.com/codetsunami/xrpl-tools/tree/master/xaddress-functions) by @codetsunami
189
190[coveralls-image]: https://badgen.net/coveralls/c/github/ripple/ripple-address-codec/master
191[coveralls-url]: https://coveralls.io/r/ripple/ripple-address/codec?branch=master
192[npm-downloads-image]: https://badgen.net/npm/dm/ripple-address-codec
193[npm-url]: https://npmjs.org/package/ripple-address-codec
194[npm-version-image]: https://badgen.net/npm/v/ripple-address-codec
195[travis-image]: https://badgen.net/travis/ripple/ripple-address-codec/master
196[travis-url]: https://travis-ci.org/github/ripple/ripple-address-codec
197
\No newline at end of file