Skip to content

Commit 7cfa1d1

Browse files
authored
Strk support (#365)
* port starknetjs * Remove comment out of validateChecksumAddress * Fix tslint error * Add bytes * Remove unused code * Fix error on importing BN
1 parent 204530f commit 7cfa1d1

File tree

12 files changed

+420
-136
lines changed

12 files changed

+420
-136
lines changed

README.md

Lines changed: 125 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# address-encoder
2+
23
This typescript library encodes and decodes address formats for various cryptocurrencies.
34

45
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.
@@ -32,130 +33,130 @@ console.log(addr); // 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa
3233

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

35-
- ABBC (base58 + ripemd160-checksum)
36-
- ADA (base58, no check + crc32-checksum and bech32)
37-
- AE (base58check)
38-
- AIB (base58check P2PKH and P2SH)
39-
- AION (hex)
40-
- ALGO (checksummed-base32)
41-
- AR (base64url)
42-
- ARB1 (checksummed-hex)
43-
- ARDR
44-
- ARK (base58check)
45-
- ATOM (bech32)
46-
- AVAX (bech32)
47-
- AVAXC (checksummed-hex)
48-
- BCD (base58check P2PKH and P2SH, and bech32 segwit)
49-
- BCH (base58check and cashAddr; decodes to cashAddr)
50-
- BCN (base58xmr)
51-
- BDX (base58xmr)
52-
- BNB (bech32)
53-
- BPS (base58check P2PKH and P2SH)
54-
- BSC (checksummed-hex)
55-
- BSV (base58check)
56-
- BTC (base58check P2PKH and P2SH, and bech32 segwit)
57-
- BTG (base58check P2PKH and P2SH, and bech32 segwit)
58-
- BTM (bech32 segwit)
59-
- BTS (base58 + ripemd160-checksum)
60-
- CCA (base58check P2PKH and P2SH)
61-
- CCXX (base58check P2PKH and P2SH, and bech32 segwit)
62-
- CELO (checksummed-hex)
63-
- CKB (bech32)
64-
- CLO (checksummed-hex)
65-
- CRO (checksummed-hex)
66-
- DASH (base58check P2PKH and P2SH)
67-
- DCR (base58, no check)
68-
- DGB (base58check P2PKH and P2SH, and bech32 segwit)
69-
- DIVI (base58check P2PKH and P2SH)
70-
- DOGE (base58check P2PKH and P2SH)
71-
- DOT (ss58)
72-
- EGLD (bech32)
73-
- ELA (base58)
74-
- EOS (base58 + ripemd160-checksum)
75-
- ETC (checksummed-hex)
76-
- ETH (checksummed-hex)
77-
- ETN (base58xmr)
78-
- EWT (checksummed-hex)
79-
- FIL (base10 + leb128 and base32 + blake2b checksum)
80-
- FIO (base58 + ripemd160-checksum)
81-
- FIRO (base58check P2PKH and P2SH)
82-
- FLOW (hex)
83-
- FTM (checksummed-hex)
84-
- GO (checksummed-hex)
85-
- GRIN (base58check)
86-
- GRS (base58check P2PKH and P2SH, and bech32 segwit)
87-
- GXC (base58 + ripemd160-checksum)
88-
- HBAR
89-
- HIVE (base58 + ripemd160-checksum)
90-
- HNS
91-
- HNT (base58check)
92-
- ICX
93-
- IOST (base58, no check)
94-
- IOTA (iotaBech32)
95-
- IOTX (bech32)
96-
- IRIS (bech32)
97-
- KAVA (bech32)
98-
- KMD (base58check)
99-
- KSM (ss58)
100-
- LCC (base58check P2PKH and P2SH, and bech32 segwit)
101-
- LRG (base58check P2PKH and P2SH)
102-
- LSK (hex with suffix)
103-
- LTC (base58check P2PHK and P2SH, and bech32 segwit)
104-
- LUNA (bech32)
105-
- MATIC (checksummed-hex)
106-
- MONA (base58check P2PKH and P2SH, and bech32 segwit)
107-
- MRX (base58check)
108-
- NANO (nano-base32)
109-
- NAS(base58 + sha3-256-checksum)
110-
- NEAR
111-
- NEM(XEM) (base32)
112-
- NEO (base58check)
113-
- NMC (base58check)
114-
- NRG (checksummed-hex)
115-
- NULS (base58)
116-
- ONE (bech32)
117-
- ONT (base58check)
118-
- OP (checksummed-hex)
119-
- POA (checksummed-hex)
120-
- PPC (base58check P2PKH and P2SH)
121-
- QTUM (base58check)
122-
- RDD (base58check P2PKH and P2SH)
123-
- RSK (checksummed-hex)
124-
- RUNE (bech32)
125-
- RVN (base58check P2PKH and P2SH)
126-
- SC (blake2b checksum)
127-
- SERO (base58, no check)
128-
- SOL (base58, no check)
129-
- SRM (base58, no check)
130-
- STEEM (base58 + ripemd160-checksum)
131-
- STRAT (base58check P2PKH and P2SH)
132-
- STX (crockford base32 P2PKH and P2SH + ripemd160-checksum)
133-
- SYS (base58check P2PKH and P2SH, and bech32 segwit)
134-
- TFUEL (checksummed-hex)
135-
- THETA (base58check)
136-
- TOMO (checksummed-hex)
137-
- TRX (base58check)
138-
- TT (checksummed-hex)
139-
- VET (checksummed-hex)
140-
- VIA (base58check P2PKH and P2SH)
141-
- VLX (base58, no check)
142-
- VSYS
143-
- WAN (checksummed-hex)
144-
- WAVES (base58)
145-
- WICC (base58check P2PKH and P2SH)
146-
- XCH (bech32m)
147-
- XDAI (checksummed-hex)
148-
- XHV (base58xmr)
149-
- XLM (ed25519 public key)
150-
- XMR (base58xmr)
151-
- XRP (base58check-ripple)
152-
- XTZ (base58check)
153-
- XVG (base58check P2PKH and P2SH)
154-
- ZEC (transparent addresses: base58check P2PKH and P2SH, and Sapling shielded payment addresses: bech32; doesn't support Sprout shielded payment addresses)
155-
- ZEL (transparent addresses: base58check P2PKH and P2SH, and Sapling shielded payment addresses: bech32; doesn't support Sprout shielded payment addresses)
156-
- ZEN (base58 check)
157-
- ZIL (bech32)
158-
36+
- ABBC (base58 + ripemd160-checksum)
37+
- ADA (base58, no check + crc32-checksum and bech32)
38+
- AE (base58check)
39+
- AIB (base58check P2PKH and P2SH)
40+
- AION (hex)
41+
- ALGO (checksummed-base32)
42+
- AR (base64url)
43+
- ARB1 (checksummed-hex)
44+
- ARDR
45+
- ARK (base58check)
46+
- ATOM (bech32)
47+
- AVAX (bech32)
48+
- AVAXC (checksummed-hex)
49+
- BCD (base58check P2PKH and P2SH, and bech32 segwit)
50+
- BCH (base58check and cashAddr; decodes to cashAddr)
51+
- BCN (base58xmr)
52+
- BDX (base58xmr)
53+
- BNB (bech32)
54+
- BPS (base58check P2PKH and P2SH)
55+
- BSC (checksummed-hex)
56+
- BSV (base58check)
57+
- BTC (base58check P2PKH and P2SH, and bech32 segwit)
58+
- BTG (base58check P2PKH and P2SH, and bech32 segwit)
59+
- BTM (bech32 segwit)
60+
- BTS (base58 + ripemd160-checksum)
61+
- CCA (base58check P2PKH and P2SH)
62+
- CCXX (base58check P2PKH and P2SH, and bech32 segwit)
63+
- CELO (checksummed-hex)
64+
- CKB (bech32)
65+
- CLO (checksummed-hex)
66+
- CRO (checksummed-hex)
67+
- DASH (base58check P2PKH and P2SH)
68+
- DCR (base58, no check)
69+
- DGB (base58check P2PKH and P2SH, and bech32 segwit)
70+
- DIVI (base58check P2PKH and P2SH)
71+
- DOGE (base58check P2PKH and P2SH)
72+
- DOT (ss58)
73+
- EGLD (bech32)
74+
- ELA (base58)
75+
- EOS (base58 + ripemd160-checksum)
76+
- ETC (checksummed-hex)
77+
- ETH (checksummed-hex)
78+
- ETN (base58xmr)
79+
- EWT (checksummed-hex)
80+
- FIL (base10 + leb128 and base32 + blake2b checksum)
81+
- FIO (base58 + ripemd160-checksum)
82+
- FIRO (base58check P2PKH and P2SH)
83+
- FLOW (hex)
84+
- FTM (checksummed-hex)
85+
- GO (checksummed-hex)
86+
- GRIN (base58check)
87+
- GRS (base58check P2PKH and P2SH, and bech32 segwit)
88+
- GXC (base58 + ripemd160-checksum)
89+
- HBAR
90+
- HIVE (base58 + ripemd160-checksum)
91+
- HNS
92+
- HNT (base58check)
93+
- ICX
94+
- IOST (base58, no check)
95+
- IOTA (iotaBech32)
96+
- IOTX (bech32)
97+
- IRIS (bech32)
98+
- KAVA (bech32)
99+
- KMD (base58check)
100+
- KSM (ss58)
101+
- LCC (base58check P2PKH and P2SH, and bech32 segwit)
102+
- LRG (base58check P2PKH and P2SH)
103+
- LSK (hex with suffix)
104+
- LTC (base58check P2PHK and P2SH, and bech32 segwit)
105+
- LUNA (bech32)
106+
- MATIC (checksummed-hex)
107+
- MONA (base58check P2PKH and P2SH, and bech32 segwit)
108+
- MRX (base58check)
109+
- NANO (nano-base32)
110+
- NAS(base58 + sha3-256-checksum)
111+
- NEAR
112+
- NEM(XEM) (base32)
113+
- NEO (base58check)
114+
- NMC (base58check)
115+
- NRG (checksummed-hex)
116+
- NULS (base58)
117+
- ONE (bech32)
118+
- ONT (base58check)
119+
- OP (checksummed-hex)
120+
- POA (checksummed-hex)
121+
- PPC (base58check P2PKH and P2SH)
122+
- QTUM (base58check)
123+
- RDD (base58check P2PKH and P2SH)
124+
- RSK (checksummed-hex)
125+
- RUNE (bech32)
126+
- RVN (base58check P2PKH and P2SH)
127+
- SC (blake2b checksum)
128+
- SERO (base58, no check)
129+
- SOL (base58, no check)
130+
- SRM (base58, no check)
131+
- STEEM (base58 + ripemd160-checksum)
132+
- STRAT (base58check P2PKH and P2SH)
133+
- STRK (keccak256-checksumed-hex)
134+
- STX (crockford base32 P2PKH and P2SH + ripemd160-checksum)
135+
- SYS (base58check P2PKH and P2SH, and bech32 segwit)
136+
- TFUEL (checksummed-hex)
137+
- THETA (base58check)
138+
- TOMO (checksummed-hex)
139+
- TRX (base58check)
140+
- TT (checksummed-hex)
141+
- VET (checksummed-hex)
142+
- VIA (base58check P2PKH and P2SH)
143+
- VLX (base58, no check)
144+
- VSYS
145+
- WAN (checksummed-hex)
146+
- WAVES (base58)
147+
- WICC (base58check P2PKH and P2SH)
148+
- XCH (bech32m)
149+
- XDAI (checksummed-hex)
150+
- XHV (base58xmr)
151+
- XLM (ed25519 public key)
152+
- XMR (base58xmr)
153+
- XRP (base58check-ripple)
154+
- XTZ (base58check)
155+
- XVG (base58check P2PKH and P2SH)
156+
- ZEC (transparent addresses: base58check P2PKH and P2SH, and Sapling shielded payment addresses: bech32; doesn't support Sprout shielded payment addresses)
157+
- ZEL (transparent addresses: base58check P2PKH and P2SH, and Sapling shielded payment addresses: bech32; doesn't support Sprout shielded payment addresses)
158+
- ZEN (base58 check)
159+
- ZIL (bech32)
159160

160161
## Development guide
161162

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__tests__/index.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,16 @@ const vectors: Array<TestVector> = [
10891089
},
10901090
],
10911091
},
1092+
{
1093+
name: 'STRK',
1094+
coinType: 9004,
1095+
passingVectors: [
1096+
{
1097+
text: '0x02Fd23d9182193775423497fc0c472E156C57C69E4089A1967fb288A2d84e914',
1098+
hex: '02fd23d9182193775423497fc0c472e156c57c69e4089a1967fb288a2d84e914'
1099+
}
1100+
],
1101+
},
10921102
{
10931103
name: 'NRG_LEGACY',
10941104
coinType: 9797,

src/groestl-hash-js/groestl.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
import { bech32, bech32m } from 'bech32';
2-
const {
3-
decode: bech32Decode,
4-
encode: bech32Encode,
5-
fromWords: bech32FromWords,
6-
toWords: bech32ToWords
7-
} = bech32;
82
import bigInt from 'big-integer';
93
import { blake2b, blake2bHex } from 'blakejs';
104
import { decode as bs58DecodeNoCheck, encode as bs58EncodeNoCheck } from 'bs58';
@@ -39,6 +33,18 @@ import { ChainID, isValidAddress } from './flow/index';
3933
import { groestl_2 } from './groestl-hash-js/index';
4034
import { xmrAddressDecoder, xmrAddressEncoder } from './monero/xmr-base58';
4135
import { nimqDecoder, nimqEncoder } from './nimq';
36+
import {
37+
getChecksumAddress as starkGetChecksumAddress,
38+
validateChecksumAddress as starkValidateChecksumAddress
39+
} from './starknet';
40+
41+
const {
42+
decode: bech32Decode,
43+
encode: bech32Encode,
44+
fromWords: bech32FromWords,
45+
toWords: bech32ToWords
46+
} = bech32;
47+
4248
const SLIP44_MSB = 0x80000000
4349
type EnCoder = (data: Buffer) => string;
4450
type DeCoder = (data: string) => Buffer;
@@ -931,6 +937,17 @@ function nasAddressDecoder(data: string): Buffer {
931937
return bufferData;
932938
}
933939

940+
function starkAddressEncoder(data: Buffer): string {
941+
return starkGetChecksumAddress('0x' + data.toString('hex'))
942+
}
943+
944+
function starkAddressDecoder(data: string): Buffer {
945+
if(!starkValidateChecksumAddress(data)) {
946+
throw Error('Invalid checksum');
947+
}
948+
return Buffer.from(rskStripHexPrefix(data), 'hex');
949+
}
950+
934951
// Referenced from following
935952
// https://github.com/icon-project/icon-service/blob/master/iconservice/base/address.py#L219
936953
function icxAddressEncoder(data: Buffer): string {
@@ -1563,6 +1580,7 @@ export const formats: IFormat[] = [
15631580
bech32mChain('XCH', 8444, 'xch', 90),
15641581
getConfig('NULS', 8964, nulsAddressEncoder, nulsAddressDecoder),
15651582
getConfig('AVAX', 9000, makeBech32Encoder('avax'), makeAvaxDecoder('avax')),
1583+
getConfig('STRK', 9004, starkAddressEncoder, starkAddressDecoder),
15661584
hexChecksumChain('NRG_LEGACY', 9797),
15671585
getConfig('ARDR', 16754, ardrAddressEncoder, ardrAddressDecoder),
15681586
zcashChain('ZEL', 19167, 'za', [[0x1c, 0xb8]], [[0x1c, 0xbd]]),

0 commit comments

Comments
 (0)