Skip to content

Commit

Permalink
Strk support (#365)
Browse files Browse the repository at this point in the history
* port starknetjs

* Remove comment out of validateChecksumAddress

* Fix tslint error

* Add bytes

* Remove unused code

* Fix error on importing BN
  • Loading branch information
makoto authored Dec 5, 2022
1 parent 204530f commit 7cfa1d1
Show file tree
Hide file tree
Showing 12 changed files with 420 additions and 136 deletions.
249 changes: 125 additions & 124 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# address-encoder

This typescript library encodes and decodes address formats for various cryptocurrencies.

Text-format addresses are decoded into their native binary representations, and vice-versa. In the case of Bitcoin-derived chains, this means their scriptPubKey; for Ethereum-derived chains this is their hash.
Expand Down Expand Up @@ -32,130 +33,130 @@ console.log(addr); // 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa

This library currently supports the following cryptocurrencies and address formats (ordered alphabetically):

- ABBC (base58 + ripemd160-checksum)
- ADA (base58, no check + crc32-checksum and bech32)
- AE (base58check)
- AIB (base58check P2PKH and P2SH)
- AION (hex)
- ALGO (checksummed-base32)
- AR (base64url)
- ARB1 (checksummed-hex)
- ARDR
- ARK (base58check)
- ATOM (bech32)
- AVAX (bech32)
- AVAXC (checksummed-hex)
- BCD (base58check P2PKH and P2SH, and bech32 segwit)
- BCH (base58check and cashAddr; decodes to cashAddr)
- BCN (base58xmr)
- BDX (base58xmr)
- BNB (bech32)
- BPS (base58check P2PKH and P2SH)
- BSC (checksummed-hex)
- BSV (base58check)
- BTC (base58check P2PKH and P2SH, and bech32 segwit)
- BTG (base58check P2PKH and P2SH, and bech32 segwit)
- BTM (bech32 segwit)
- BTS (base58 + ripemd160-checksum)
- CCA (base58check P2PKH and P2SH)
- CCXX (base58check P2PKH and P2SH, and bech32 segwit)
- CELO (checksummed-hex)
- CKB (bech32)
- CLO (checksummed-hex)
- CRO (checksummed-hex)
- DASH (base58check P2PKH and P2SH)
- DCR (base58, no check)
- DGB (base58check P2PKH and P2SH, and bech32 segwit)
- DIVI (base58check P2PKH and P2SH)
- DOGE (base58check P2PKH and P2SH)
- DOT (ss58)
- EGLD (bech32)
- ELA (base58)
- EOS (base58 + ripemd160-checksum)
- ETC (checksummed-hex)
- ETH (checksummed-hex)
- ETN (base58xmr)
- EWT (checksummed-hex)
- FIL (base10 + leb128 and base32 + blake2b checksum)
- FIO (base58 + ripemd160-checksum)
- FIRO (base58check P2PKH and P2SH)
- FLOW (hex)
- FTM (checksummed-hex)
- GO (checksummed-hex)
- GRIN (base58check)
- GRS (base58check P2PKH and P2SH, and bech32 segwit)
- GXC (base58 + ripemd160-checksum)
- HBAR
- HIVE (base58 + ripemd160-checksum)
- HNS
- HNT (base58check)
- ICX
- IOST (base58, no check)
- IOTA (iotaBech32)
- IOTX (bech32)
- IRIS (bech32)
- KAVA (bech32)
- KMD (base58check)
- KSM (ss58)
- LCC (base58check P2PKH and P2SH, and bech32 segwit)
- LRG (base58check P2PKH and P2SH)
- LSK (hex with suffix)
- LTC (base58check P2PHK and P2SH, and bech32 segwit)
- LUNA (bech32)
- MATIC (checksummed-hex)
- MONA (base58check P2PKH and P2SH, and bech32 segwit)
- MRX (base58check)
- NANO (nano-base32)
- NAS(base58 + sha3-256-checksum)
- NEAR
- NEM(XEM) (base32)
- NEO (base58check)
- NMC (base58check)
- NRG (checksummed-hex)
- NULS (base58)
- ONE (bech32)
- ONT (base58check)
- OP (checksummed-hex)
- POA (checksummed-hex)
- PPC (base58check P2PKH and P2SH)
- QTUM (base58check)
- RDD (base58check P2PKH and P2SH)
- RSK (checksummed-hex)
- RUNE (bech32)
- RVN (base58check P2PKH and P2SH)
- SC (blake2b checksum)
- SERO (base58, no check)
- SOL (base58, no check)
- SRM (base58, no check)
- STEEM (base58 + ripemd160-checksum)
- STRAT (base58check P2PKH and P2SH)
- STX (crockford base32 P2PKH and P2SH + ripemd160-checksum)
- SYS (base58check P2PKH and P2SH, and bech32 segwit)
- TFUEL (checksummed-hex)
- THETA (base58check)
- TOMO (checksummed-hex)
- TRX (base58check)
- TT (checksummed-hex)
- VET (checksummed-hex)
- VIA (base58check P2PKH and P2SH)
- VLX (base58, no check)
- VSYS
- WAN (checksummed-hex)
- WAVES (base58)
- WICC (base58check P2PKH and P2SH)
- XCH (bech32m)
- XDAI (checksummed-hex)
- XHV (base58xmr)
- XLM (ed25519 public key)
- XMR (base58xmr)
- XRP (base58check-ripple)
- XTZ (base58check)
- XVG (base58check P2PKH and P2SH)
- ZEC (transparent addresses: base58check P2PKH and P2SH, and Sapling shielded payment addresses: bech32; doesn't support Sprout shielded payment addresses)
- ZEL (transparent addresses: base58check P2PKH and P2SH, and Sapling shielded payment addresses: bech32; doesn't support Sprout shielded payment addresses)
- ZEN (base58 check)
- ZIL (bech32)

- ABBC (base58 + ripemd160-checksum)
- ADA (base58, no check + crc32-checksum and bech32)
- AE (base58check)
- AIB (base58check P2PKH and P2SH)
- AION (hex)
- ALGO (checksummed-base32)
- AR (base64url)
- ARB1 (checksummed-hex)
- ARDR
- ARK (base58check)
- ATOM (bech32)
- AVAX (bech32)
- AVAXC (checksummed-hex)
- BCD (base58check P2PKH and P2SH, and bech32 segwit)
- BCH (base58check and cashAddr; decodes to cashAddr)
- BCN (base58xmr)
- BDX (base58xmr)
- BNB (bech32)
- BPS (base58check P2PKH and P2SH)
- BSC (checksummed-hex)
- BSV (base58check)
- BTC (base58check P2PKH and P2SH, and bech32 segwit)
- BTG (base58check P2PKH and P2SH, and bech32 segwit)
- BTM (bech32 segwit)
- BTS (base58 + ripemd160-checksum)
- CCA (base58check P2PKH and P2SH)
- CCXX (base58check P2PKH and P2SH, and bech32 segwit)
- CELO (checksummed-hex)
- CKB (bech32)
- CLO (checksummed-hex)
- CRO (checksummed-hex)
- DASH (base58check P2PKH and P2SH)
- DCR (base58, no check)
- DGB (base58check P2PKH and P2SH, and bech32 segwit)
- DIVI (base58check P2PKH and P2SH)
- DOGE (base58check P2PKH and P2SH)
- DOT (ss58)
- EGLD (bech32)
- ELA (base58)
- EOS (base58 + ripemd160-checksum)
- ETC (checksummed-hex)
- ETH (checksummed-hex)
- ETN (base58xmr)
- EWT (checksummed-hex)
- FIL (base10 + leb128 and base32 + blake2b checksum)
- FIO (base58 + ripemd160-checksum)
- FIRO (base58check P2PKH and P2SH)
- FLOW (hex)
- FTM (checksummed-hex)
- GO (checksummed-hex)
- GRIN (base58check)
- GRS (base58check P2PKH and P2SH, and bech32 segwit)
- GXC (base58 + ripemd160-checksum)
- HBAR
- HIVE (base58 + ripemd160-checksum)
- HNS
- HNT (base58check)
- ICX
- IOST (base58, no check)
- IOTA (iotaBech32)
- IOTX (bech32)
- IRIS (bech32)
- KAVA (bech32)
- KMD (base58check)
- KSM (ss58)
- LCC (base58check P2PKH and P2SH, and bech32 segwit)
- LRG (base58check P2PKH and P2SH)
- LSK (hex with suffix)
- LTC (base58check P2PHK and P2SH, and bech32 segwit)
- LUNA (bech32)
- MATIC (checksummed-hex)
- MONA (base58check P2PKH and P2SH, and bech32 segwit)
- MRX (base58check)
- NANO (nano-base32)
- NAS(base58 + sha3-256-checksum)
- NEAR
- NEM(XEM) (base32)
- NEO (base58check)
- NMC (base58check)
- NRG (checksummed-hex)
- NULS (base58)
- ONE (bech32)
- ONT (base58check)
- OP (checksummed-hex)
- POA (checksummed-hex)
- PPC (base58check P2PKH and P2SH)
- QTUM (base58check)
- RDD (base58check P2PKH and P2SH)
- RSK (checksummed-hex)
- RUNE (bech32)
- RVN (base58check P2PKH and P2SH)
- SC (blake2b checksum)
- SERO (base58, no check)
- SOL (base58, no check)
- SRM (base58, no check)
- STEEM (base58 + ripemd160-checksum)
- STRAT (base58check P2PKH and P2SH)
- STRK (keccak256-checksumed-hex)
- STX (crockford base32 P2PKH and P2SH + ripemd160-checksum)
- SYS (base58check P2PKH and P2SH, and bech32 segwit)
- TFUEL (checksummed-hex)
- THETA (base58check)
- TOMO (checksummed-hex)
- TRX (base58check)
- TT (checksummed-hex)
- VET (checksummed-hex)
- VIA (base58check P2PKH and P2SH)
- VLX (base58, no check)
- VSYS
- WAN (checksummed-hex)
- WAVES (base58)
- WICC (base58check P2PKH and P2SH)
- XCH (bech32m)
- XDAI (checksummed-hex)
- XHV (base58xmr)
- XLM (ed25519 public key)
- XMR (base58xmr)
- XRP (base58check-ripple)
- XTZ (base58check)
- XVG (base58check P2PKH and P2SH)
- ZEC (transparent addresses: base58check P2PKH and P2SH, and Sapling shielded payment addresses: bech32; doesn't support Sprout shielded payment addresses)
- ZEL (transparent addresses: base58check P2PKH and P2SH, and Sapling shielded payment addresses: bech32; doesn't support Sprout shielded payment addresses)
- ZEN (base58 check)
- ZIL (bech32)

## Development guide

Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,16 @@ const vectors: Array<TestVector> = [
},
],
},
{
name: 'STRK',
coinType: 9004,
passingVectors: [
{
text: '0x02Fd23d9182193775423497fc0c472E156C57C69E4089A1967fb288A2d84e914',
hex: '02fd23d9182193775423497fc0c472e156c57c69e4089a1967fb288a2d84e914'
}
],
},
{
name: 'NRG_LEGACY',
coinType: 9797,
Expand Down
2 changes: 1 addition & 1 deletion src/groestl-hash-js/groestl.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 24 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import { bech32, bech32m } from 'bech32';
const {
decode: bech32Decode,
encode: bech32Encode,
fromWords: bech32FromWords,
toWords: bech32ToWords
} = bech32;
import bigInt from 'big-integer';
import { blake2b, blake2bHex } from 'blakejs';
import { decode as bs58DecodeNoCheck, encode as bs58EncodeNoCheck } from 'bs58';
Expand Down Expand Up @@ -39,6 +33,18 @@ import { ChainID, isValidAddress } from './flow/index';
import { groestl_2 } from './groestl-hash-js/index';
import { xmrAddressDecoder, xmrAddressEncoder } from './monero/xmr-base58';
import { nimqDecoder, nimqEncoder } from './nimq';
import {
getChecksumAddress as starkGetChecksumAddress,
validateChecksumAddress as starkValidateChecksumAddress
} from './starknet';

const {
decode: bech32Decode,
encode: bech32Encode,
fromWords: bech32FromWords,
toWords: bech32ToWords
} = bech32;

const SLIP44_MSB = 0x80000000
type EnCoder = (data: Buffer) => string;
type DeCoder = (data: string) => Buffer;
Expand Down Expand Up @@ -931,6 +937,17 @@ function nasAddressDecoder(data: string): Buffer {
return bufferData;
}

function starkAddressEncoder(data: Buffer): string {
return starkGetChecksumAddress('0x' + data.toString('hex'))
}

function starkAddressDecoder(data: string): Buffer {
if(!starkValidateChecksumAddress(data)) {
throw Error('Invalid checksum');
}
return Buffer.from(rskStripHexPrefix(data), 'hex');
}

// Referenced from following
// https://github.com/icon-project/icon-service/blob/master/iconservice/base/address.py#L219
function icxAddressEncoder(data: Buffer): string {
Expand Down Expand Up @@ -1563,6 +1580,7 @@ export const formats: IFormat[] = [
bech32mChain('XCH', 8444, 'xch', 90),
getConfig('NULS', 8964, nulsAddressEncoder, nulsAddressDecoder),
getConfig('AVAX', 9000, makeBech32Encoder('avax'), makeAvaxDecoder('avax')),
getConfig('STRK', 9004, starkAddressEncoder, starkAddressDecoder),
hexChecksumChain('NRG_LEGACY', 9797),
getConfig('ARDR', 16754, ardrAddressEncoder, ardrAddressDecoder),
zcashChain('ZEL', 19167, 'za', [[0x1c, 0xb8]], [[0x1c, 0xbd]]),
Expand Down
Loading

0 comments on commit 7cfa1d1

Please sign in to comment.