Skip to content

Commit

Permalink
Add modified decodeAccount function
Browse files Browse the repository at this point in the history
This modified version can import private keys formated as:

- base58 strings
- raw arrays

Context: project-serum#74 (comment)
  • Loading branch information
moshthepitt committed Feb 2, 2021
1 parent f0386ce commit ec01efe
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
16 changes: 1 addition & 15 deletions src/components/AddAccountDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import TextField from '@material-ui/core/TextField';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import FormGroup from '@material-ui/core/FormGroup';
import Switch from '@material-ui/core/Switch';
import { Account } from '@solana/web3.js';
import { decodeAccount } from './helpers/import';
import DialogForm from './DialogForm';

export default function AddAccountDialog({ open, onAdd, onClose }) {
Expand Down Expand Up @@ -81,17 +81,3 @@ export default function AddAccountDialog({ open, onAdd, onClose }) {
</DialogForm>
);
}

/**
* Returns an account object when given the private key
*
* @param {string} privateKey - the private key in array format
*/
function decodeAccount(privateKey) {
try {
const a = new Account(JSON.parse(privateKey));
return a;
} catch (_) {
return undefined;
}
}
19 changes: 19 additions & 0 deletions src/components/helpers/import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Account } from '@solana/web3.js';
import * as bs58 from 'bs58';

/**
* Returns an account object when given the private key
*
* @param {string} privateKey - the private key in array format
*/
export const decodeAccount = (privateKey: string) => {
try {
return new Account(JSON.parse(privateKey));
} catch (_) {
try {
return new Account(bs58.decode(privateKey));
} catch (_) {
return undefined;
}
}
}
24 changes: 24 additions & 0 deletions src/components/helpers/tests/import.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { decodeAccount } from '../import';

test('decodeAccount works as expected', () => {
// these two private keys are for the same account
const base58Token = '1saKGirgZWmdT4SRQTCTovm8tW6iTdpvsEsCWGwcfen2GoSyummQPWof3WdmZhBy67MsWkkkiqJcMPnL4ZFfqwu';
const rawArrayToken = '[0,192,128,45,243,200,60,181,169,230,168,65,215,92,19,29,106,141,191,82,82,28,99,88,40,199,160,149,19,226,159,19,50,198,38,252,188,134,166,183,236,56,37,12,52,116,162,234,224,95,173,109,228,126,210,222,129,127,101,216,217,133,203,64]';
// create the accounts using both the keys
const account1 = decodeAccount(base58Token);
const account2 = decodeAccount(rawArrayToken);
// assert that account1 is fully defined
expect(account1).toBeDefined();
expect(account1?.publicKey).toBeDefined();
expect(account1?.secretKey).toBeDefined();
// assert that account2 is fully defined
expect(account2).toBeDefined();
expect(account2?.publicKey).toBeDefined();
expect(account2?.secretKey).toBeDefined();
// assert that account1 and account2 are the same account
expect(account1?.publicKey).toEqual(account2?.publicKey);
expect(account1?.secretKey).toEqual(account2?.secretKey);
// confirm that this is indeed the account that was imported
const originalSecretKey = account1 ? Array.from(account1.secretKey) : [];
expect(originalSecretKey).toEqual(JSON.parse(rawArrayToken));
});

0 comments on commit ec01efe

Please sign in to comment.