-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
250 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default function binary_to_hex(buf) { | ||
return Array.prototype.map | ||
.call(new Uint8Array(buf), (x) => ('00' + x.toString(16)).slice(-2)) | ||
.join('') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { decode_nano_base32 } from './nano-base-32.mjs' | ||
import binary_to_hex from './binary-to-hex.mjs' | ||
|
||
export default function decode_nano_address({ address }) { | ||
const cleaned_address = address.replace('nano_', '').replace('xrb_', '') | ||
const decoded = decode_nano_base32(cleaned_address) | ||
const public_key = binary_to_hex(decoded.subarray(0, 32)) | ||
const checksum = binary_to_hex(decoded.subarray(32, 32 + 5)) | ||
return { | ||
public_key, | ||
checksum | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import ed25519 from '@trashman/ed25519-blake2b' | ||
|
||
import { encode_nano_base32 } from './nano-base-32.mjs' | ||
|
||
export default function encode_nano_address({ | ||
public_key_buf, | ||
prefix = 'nano_' | ||
}) { | ||
const encoded_public_key = encode_nano_base32(public_key_buf) | ||
const checksum = ed25519.hash(public_key_buf, 5).reverse() | ||
const encoded_checksum = encode_nano_base32(checksum) | ||
return prefix + encoded_public_key + encoded_checksum | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import ed25519 from '@trashman/ed25519-blake2b' | ||
|
||
import decode_nano_address from './decode-nano-address.mjs' | ||
|
||
const NANO_ADDRESS_RE = /^(xrb_|nano_)[13][13-9a-km-uw-z]{59}$/ | ||
|
||
const compare_arrays = (a, b) => { | ||
if (a.length !== b.length) { | ||
return false | ||
} | ||
|
||
return a.every((byte, index) => byte === b[index]) | ||
} | ||
|
||
export default function is_nano_address_valid(address) { | ||
if (typeof address !== 'string') { | ||
return false | ||
} | ||
|
||
if (!NANO_ADDRESS_RE.test(address)) { | ||
return false | ||
} | ||
|
||
const { public_key, checksum } = decode_nano_address({ address }) | ||
|
||
const public_key_bytes = Buffer.from(public_key, 'hex') | ||
const checksum_bytes = Buffer.from(checksum, 'hex') | ||
const computed_checksum_bytes = ed25519.hash(public_key_bytes, 5).reverse() | ||
const valid = compare_arrays(checksum_bytes, computed_checksum_bytes) | ||
|
||
return valid | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2018 Gray Olson | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
const alphabet = '13456789abcdefghijkmnopqrstuwxyz' | ||
|
||
/** | ||
* Encode provided Uint8Array using the Nano-specific Base-32 implementeation. | ||
* @param view Input buffer formatted as a Uint8Array | ||
* @returns | ||
* @hidden | ||
*/ | ||
export function encode_nano_base32(view) { | ||
const length = view.length | ||
const leftover = (length * 8) % 5 | ||
const offset = leftover === 0 ? 0 : 5 - leftover | ||
|
||
let value = 0 | ||
let output = '' | ||
let bits = 0 | ||
|
||
for (let i = 0; i < length; i++) { | ||
value = (value << 8) | view[i] | ||
bits += 8 | ||
|
||
while (bits >= 5) { | ||
output += alphabet[(value >>> (bits + offset - 5)) & 31] | ||
bits -= 5 | ||
} | ||
} | ||
|
||
if (bits > 0) { | ||
output += alphabet[(value << (5 - (bits + offset))) & 31] | ||
} | ||
|
||
return output | ||
} | ||
|
||
function readChar(char) { | ||
const idx = alphabet.indexOf(char) | ||
|
||
if (idx === -1) { | ||
throw new Error(`Invalid character found: ${char}`) | ||
} | ||
|
||
return idx | ||
} | ||
|
||
/** | ||
* Decodes a Nano-implementation Base32 encoded string into a Uint8Array | ||
* @param input A Nano-Base32 encoded string | ||
* @returns | ||
* @hidden | ||
*/ | ||
export function decode_nano_base32(input) { | ||
const length = input.length | ||
const leftover = (length * 5) % 8 | ||
const offset = leftover === 0 ? 0 : 8 - leftover | ||
|
||
let bits = 0 | ||
let value = 0 | ||
|
||
let index = 0 | ||
let output = new Uint8Array(Math.ceil((length * 5) / 8)) | ||
|
||
for (let i = 0; i < length; i++) { | ||
value = (value << 5) | readChar(input[i]) | ||
bits += 5 | ||
|
||
if (bits >= 8) { | ||
output[index++] = (value >>> (bits + offset - 8)) & 255 | ||
bits -= 8 | ||
} | ||
} | ||
if (bits > 0) { | ||
output[index++] = (value << (bits + offset - 8)) & 255 | ||
} | ||
|
||
if (leftover !== 0) { | ||
output = output.slice(1) | ||
} | ||
return output | ||
} |
Oops, something went wrong.