Add support for Account Public Key data type (#35)

Per https://xrpl.org/base58-encodings.html
This commit is contained in:
Elliot Lee
2020-01-22 10:42:07 -08:00
parent 5be66ab646
commit a57c71beb8
4 changed files with 36 additions and 6 deletions

View File

@@ -1,5 +1,14 @@
# ripple-address-codec Release History
## 4.1.0 (UNRELEASED)
* Throwable 'unexpected_payload_length' error: The message has been expanded with ' Ensure that the bytes are a Buffer.'
### New Features
* `encodeAccountPublic` - Encode a public key, as for payment channels
* `decodeAccountPublic` - Decode a public key, as for payment channels
## 4.0.0 (2019-10-08)
### Breaking Changes
@@ -14,4 +23,3 @@
* `xAddressToClassicAddress` - Decode an X-address to account ID, tag, and network ID
* `decodeXAddress` - Convert X-address to classic address, tag, and network ID
* `isValidXAddress` - Check whether an X-address (X...) is valid

View File

@@ -6,6 +6,8 @@ import {
decodeAccountID,
encodeNodePublic,
decodeNodePublic,
encodeAccountPublic,
decodeAccountPublic,
isValidClassicAddress
} from './xrp-codec'
import * as assert from 'assert'
@@ -123,6 +125,8 @@ export {
decodeAccountID, // Decode a classic address to its raw bytes
encodeNodePublic, // Encode bytes to XRP Ledger node public key format
decodeNodePublic, // Decode an XRP Ledger node public key into its raw bytes
encodeAccountPublic, // Encode a public key, as for payment channels
decodeAccountPublic, // Decode a public key, as for payment channels
isValidClassicAddress, // Check whether a classic address (r...) is valid
classicAddressToXAddress, // Derive X-address from classic address, tag, and network ID
encodeXAddress, // Encode account ID, tag, and network ID to X-address

View File

@@ -5,7 +5,7 @@ function toHex(bytes: Buffer) {
}
function toBytes(hex: string) {
return Buffer.from(hex, 'hex').toJSON().data
return Buffer.from(hex, 'hex')
}
/**
@@ -34,6 +34,10 @@ makeEncodeDecodeTest(api.encodeNodePublic, api.decodeNodePublic,
'n9MXXueo837zYH36DvMc13BwHcqtfAWNJY5czWVbp7uYTj7x17TH',
'0388E5BA87A000CB807240DF8C848EB0B5FFA5C8E5A521BC8E105C0F0A44217828')
makeEncodeDecodeTest(api.encodeAccountPublic, api.decodeAccountPublic,
'aB44YfzW24VDEJQ2UuLPV2PvqcPCSoLnL7y5M1EzhdW4LnK5xMS3',
'023693F15967AE357D0327974AD46FE3C127113B1110D6044FD41E723689F81CC6')
test('can decode arbitrary seeds', function() {
const decoded = api.decodeSeed('sEdTM1uX8pu2do5XvTnutH6HsouMaM2')
expect(toHex(decoded.bytes)).toBe('4C3A1D213FBDFB14C7C28D609469B341')

View File

@@ -37,7 +37,8 @@ class Codec {
encodeVersioned(bytes: Buffer, versions: number[], expectedLength: number): string {
if (expectedLength && bytes.length !== expectedLength) {
throw new Error('unexpected_payload_length: bytes.length does not match expectedLength')
throw new Error('unexpected_payload_length: bytes.length does not match expectedLength.' +
' Ensure that the bytes are a Buffer.')
}
return this.encodeChecked(Buffer.from(concatArgs(versions, bytes)))
}
@@ -122,9 +123,12 @@ class Codec {
// Pure JavaScript hash functions in the browser, native hash functions in Node.js
const createHash = require('create-hash')
const NODE_PUBLIC = 28
const ACCOUNT_ID = 0
const FAMILY_SEED = 0x21 // 33
// base58 encodings: https://xrpl.org/base58-encodings.html
const ACCOUNT_ID = 0 // Account address (20 bytes)
const ACCOUNT_PUBLIC_KEY = 0x23 // Account public key (33 bytes)
const FAMILY_SEED = 0x21 // 33; Seed value (for secret keys) (16 bytes)
const NODE_PUBLIC = 0x1C // 28; Validation public key (33 bytes)
const ED25519_SEED = [0x01, 0xE1, 0x4B] // [1, 225, 75]
const codecOptions = {
@@ -191,6 +195,16 @@ export function encodeNodePublic(bytes: Buffer): string {
return codecWithXrpAlphabet.encode(bytes, opts)
}
export function encodeAccountPublic(bytes: Buffer): string {
const opts = {versions: [ACCOUNT_PUBLIC_KEY], expectedLength: 33}
return codecWithXrpAlphabet.encode(bytes, opts)
}
export function decodeAccountPublic(base58string: string): Buffer {
const opts = {versions: [ACCOUNT_PUBLIC_KEY], expectedLength: 33}
return codecWithXrpAlphabet.decode(base58string, opts).bytes
}
export function isValidClassicAddress(address: string): boolean {
try {
decodeAccountID(address)