Skip to content

Commit

Permalink
Fix RSK checksums, be more particular about hex addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
Arachnid committed Oct 23, 2019
1 parent e74726d commit 5bf53b1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 24 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

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

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@
},
"dependencies": {
"@types/cashaddrjs": "^0.3.0",
"base-x": "^3.0.7",
"bech32": "^1.1.3",
"bs58check": "^2.1.2",
"cashaddrjs": "^0.3.8",
"eip55": "^1.0.3",
"ripple-address-codec": "^4.0.0",
"rskjs-util": "^1.0.3",
"safe-buffer": "^5.2.0"
}
}
4 changes: 0 additions & 4 deletions src/@types/eip55/index.d.ts

This file was deleted.

12 changes: 11 additions & 1 deletion src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,17 @@ const vectors: Array<TestVector> = [
name: 'RSK',
coinType: 137,
passingVectors: [
{ text: '0x314159265dD8dbb310642f98f50C066173C1259b', hex: '314159265dd8dbb310642f98f50c066173c1259b' },
{ text: '0x5aaEB6053f3e94c9b9a09f33669435E7ef1bEAeD', hex: '5aaeb6053f3e94c9b9a09f33669435e7ef1beaed' },
{
text: '0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed',
hex: '5aaeb6053f3e94c9b9a09f33669435e7ef1beaed',
canonical: '0x5aaEB6053f3e94c9b9a09f33669435E7ef1bEAeD',
},
{
text: '0x5AAEB6053F3E94C9B9A09F33669435E7EF1BEAED',
hex: '5aaeb6053f3e94c9b9a09f33669435e7ef1beaed',
canonical: '0x5aaEB6053f3e94c9b9a09f33669435E7ef1bEAeD',
},
],
},
{
Expand Down
28 changes: 19 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as bech32 from 'bech32';
import * as bs58check from 'bs58check';
import * as cashaddr from 'cashaddrjs';
import * as eip55 from 'eip55';
import * as ripple from 'ripple-address-codec';
import * as rsk from 'rskjs-util';

interface IFormat {
coinType: number;
Expand Down Expand Up @@ -163,18 +163,28 @@ function decodeBitcoinCash(data: string): Buffer {
}
}

function encodeChecksummedHex(data: Buffer): string {
return eip55.encode('0x' + data.toString('hex'));
function makeChecksummedHexEncoder(chainId?: number) {
return (data: Buffer) => rsk.toChecksumAddress(data.toString('hex'), chainId || null);
}

function decodeChecksummedHex(data: string): Buffer {
return Buffer.from(data.slice(2), 'hex');
function makeChecksummedHexDecoder(chainId?: number) {
return (data: string) => {
const stripped = rsk.stripHexPrefix(data);
if (
!rsk.isValidChecksumAddress(data, chainId || null) &&
stripped !== stripped.toLowerCase() &&
stripped !== stripped.toUpperCase()
) {
throw Error('Invalid address checksum');
}
return Buffer.from(rsk.stripHexPrefix(data), 'hex');
};
}

const hexChecksumChain = (name: string, coinType: number) => ({
const hexChecksumChain = (name: string, coinType: number, chainId?: number) => ({
coinType,
decoder: decodeChecksummedHex,
encoder: encodeChecksummedHex,
decoder: makeChecksummedHexDecoder(chainId),
encoder: makeChecksummedHexEncoder(chainId),
name,
});

Expand All @@ -185,7 +195,7 @@ const formats: IFormat[] = [
base58Chain('MONA', 22, [0x32], [0x05]),
hexChecksumChain('ETH', 60),
hexChecksumChain('ETC', 61),
hexChecksumChain('RSK', 137),
hexChecksumChain('RSK', 137, 30),
{
coinType: 144,
decoder: (data: string) => ripple.codec.decodeChecked(data),
Expand Down

0 comments on commit 5bf53b1

Please sign in to comment.