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

Stx dev #297

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ This library currently supports the following cryptocurrencies and address forma
- SRM (base58, no check)
- STEEM (base58 + ripemd160-checksum)
- STRAT (base58check P2PKH and P2SH)
- STX (c32)
- SYS (base58check P2PKH and P2SH, and bech32 segwit)
- TFUEL (checksummed-hex)
- THETA (base58check)
Expand Down
10 changes: 10 additions & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,16 @@ const vectors: Array<TestVector> = [
{ text: 'hs1qd42hrldu5yqee58se4uj6xctm7nk28r70e84vx', hex: '6d5571fdbca1019cd0f0cd792d1b0bdfa7651c7e' },
],
},
{
name: 'STX',
coinType: 5757,
passingVectors: [
{
text: 'SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7',
hex: 'a46ff88886c2ef9762d970b4d2c63678835bd39d',
},
],
},
{
name: 'GO',
coinType: 6060,
Expand Down
167 changes: 167 additions & 0 deletions src/blockstacks/encoding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
export const c32 = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'
const hex = '0123456789abcdef'

/**
* Encode a hex string as a c32 string. Note that the hex string is assumed
* to be big-endian (and the resulting c32 string will be as well).
* @param {string} inputHex - the input to encode
* @param {number} minLength - the minimum length of the c32 string
* @returns {string} the c32check-encoded representation of the data, as a string
*/
export function c32encode(inputHex : string, minLength?: number) : string {
// must be hex
if (!inputHex.match(/^[0-9a-fA-F]*$/)) {
throw new Error('Not a hex-encoded string')
}

if ((inputHex.length) % 2 !== 0) {
inputHex = `0${inputHex}`
}

inputHex = inputHex.toLowerCase()

let res = []
let carry = 0
for (let i = inputHex.length - 1; i >= 0; i--) {
if (carry < 4) {
const currentCode = hex.indexOf(inputHex[i]) >> carry
let nextCode = 0
if (i !== 0) {
nextCode = hex.indexOf(inputHex[i - 1])
}
// carry = 0, nextBits is 1, carry = 1, nextBits is 2
const nextBits = 1 + carry
const nextLowBits = (nextCode % (1<<nextBits)) << (5-nextBits)
const curC32Digit = c32[currentCode + nextLowBits]
carry = nextBits
res.unshift(curC32Digit)
} else {
carry = 0
}
}

let C32leadingZeros = 0
for (let i = 0; i < res.length; i++) {
if (res[i] !== '0') {
break
} else {
C32leadingZeros++
}
}

res = res.slice(C32leadingZeros)

let zeroPrefixCount = 0;
let found = false;
while (!found)
{
if (inputHex[zeroPrefixCount] == '0')
{
++zeroPrefixCount;
}
else
{
found = true;
}
}

const numLeadingZeroBytesInHex = zeroPrefixCount

for (let i = 0; i < numLeadingZeroBytesInHex; i++) {
res.unshift(c32[0])
}

if (minLength) {
const count = minLength - res.length
for (let i = 0; i < count; i++) {
res.unshift(c32[0])
}
}

return res.join('')
}

/*
* Normalize a c32 string
* @param {string} c32input - the c32-encoded input string
* @returns {string} the canonical representation of the c32 input string
*/
export function c32normalize(c32input: string) : string {
// must be upper-case
// replace all O's with 0's
// replace all I's and L's with 1's
return c32input.toUpperCase()
.replace(/O/g, '0')
.replace(/L|I/g, '1')
}

/*
* Decode a c32 string back into a hex string. Note that the c32 input
* string is assumed to be big-endian (and the resulting hex string will
* be as well).
* @param {string} c32input - the c32-encoded input to decode
* @param {number} minLength - the minimum length of the output hex string (in bytes)
* @returns {string} the hex-encoded representation of the data, as a string
*/
export function c32decode(c32input: string, minLength?: number) : string {
c32input = c32normalize(c32input)

// must result in a c32 string
if (!c32input.match(`^[${c32}]*$`)) {
throw new Error('Not a c32-encoded string')
}

const zeroPrefix = c32input.match(`^${c32[0]}*`)
const numLeadingZeroBytes = zeroPrefix ? zeroPrefix[0].length : 0

let res = []
let carry = 0
let carryBits = 0
for (let i = c32input.length - 1; i >= 0; i--) {
if (carryBits === 4) {
res.unshift(hex[carry])
carryBits = 0
carry = 0
}
const currentCode = c32.indexOf(c32input[i]) << carryBits
const currentValue = currentCode + carry
const currentHexDigit = hex[currentValue % 16]
carryBits += 1
carry = currentValue >> 4
if (carry > 1 << carryBits) {
throw new Error('Panic error in decoding.')
}
res.unshift(currentHexDigit)
}
// one last carry
res.unshift(hex[carry])

if (res.length % 2 === 1) {
res.unshift('0')
}

let hexLeadingZeros = 0
for (let i = 0; i < res.length; i++) {
if (res[i] !== '0') {
break
} else {
hexLeadingZeros++
}
}

res = res.slice(hexLeadingZeros - (hexLeadingZeros % 2))

let hexStr = res.join('')
for (let i = 0; i < numLeadingZeroBytes; i++) {
hexStr = `00${hexStr}`
}

if (minLength) {
const count = minLength * 2 - hexStr.length
for (let i = 0; i < count; i += 2) {
hexStr = `00${hexStr}`
}
}

return hexStr
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from 'bech32';
import bigInt from 'big-integer';
import { blake2b, blake2bHex } from 'blakejs';
import { c32encode, c32decode } from './blockstacks/encoding';
import { decode as bs58DecodeNoCheck, encode as bs58EncodeNoCheck } from 'bs58';
// @ts-ignore
import {
Expand Down Expand Up @@ -1403,6 +1404,7 @@ export const formats: IFormat[] = [
},
getConfig('IOTA', 4218, bs58Encode, bs58Decode),
getConfig('HNS', 5353, hnsAddressEncoder, hnsAddressDecoder),
getConfig('STX', 5757, c32encode, c32decode),
hexChecksumChain('GO', 6060),
getConfig('NULS', 8964, nulsAddressEncoder, nulsAddressDecoder),
bech32Chain('AVAX', 9000, 'avax'),
Expand Down