Skip to content

Commit

Permalink
Merge pull request #9 from CoinSpace/private
Browse files Browse the repository at this point in the history
Multiple private key formats
  • Loading branch information
paulmillr authored May 23, 2023
2 parents b9d4b83 + c914e43 commit c436b6d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
22 changes: 19 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ed25519 } from '@noble/curves/ed25519';
import { base58, base64, utf8 } from '@scure/base';
import { base58, base64, hex, utf8 } from '@scure/base';
import { sha256 } from '@noble/hashes/sha256';
import * as P from 'micro-packed';

Expand Down Expand Up @@ -775,9 +775,25 @@ export function getAddressFromPublicKey(publicKey: Bytes) {
return base58.encode(publicKey);
}

export function formatPrivate(privateKey: Bytes) {
type PrivateKeyFormat = 'base58' | 'hex' | 'array';

export function formatPrivate(privateKey: Bytes, format: PrivateKeyFormat = 'base58') {
const publicKey = getPublicKey(privateKey);
return base58.encode(P.concatBytes(privateKey, publicKey));
const fullKey = P.concatBytes(privateKey, publicKey);
switch (format) {
case 'base58': {
return base58.encode(fullKey);
}
case 'hex': {
return hex.encode(fullKey);
}
case 'array': {
return Array.from(fullKey);
}
default: {
throw new Error('sol: unsupported format');
}
}
}

export function createTxComplex(address: string, instructions: Instruction[], blockhash: string) {
Expand Down
23 changes: 23 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ const shortVecVectors = [
const key = hex.decode('1b2f49096e3e5dbd0fcfa9c0c0cd92d9ab3b21544b34d5dd4a65d98b878b9922');
deepStrictEqual(sol.getAddressFromPublicKey(key), '2q7pyhPwAwZ3QMfZrnAbDhnh9mDUqycszcpf86VgQxhF');
});
should('sol: format private key base58', () => {
const key = hex.decode('99da9559e15e913ee9ab2e53e3dfad575da33b49be1125bb922e33494f498828');
deepStrictEqual(sol.formatPrivate(key), '45QmaP6zVBfDPLWrbtaMiVFKbRLPwwAqXHiDkx2FcUHZoV1uU6uB8cZyGBKQbiExXyyzghaE65THFi2h8mSwkFuj');
});
should('sol: format private key hex', () => {
const key = hex.decode('99da9559e15e913ee9ab2e53e3dfad575da33b49be1125bb922e33494f498828');
deepStrictEqual(sol.formatPrivate(key, 'hex'), '99da9559e15e913ee9ab2e53e3dfad575da33b49be1125bb922e33494f4988281b2f49096e3e5dbd0fcfa9c0c0cd92d9ab3b21544b34d5dd4a65d98b878b9922');
});
should('sol: format private key array', () => {
const key = hex.decode('99da9559e15e913ee9ab2e53e3dfad575da33b49be1125bb922e33494f498828');
deepStrictEqual(sol.formatPrivate(key, 'array'), [
153, 218, 149, 89, 225, 94, 145, 62, 233, 171, 46,
83, 227, 223, 173, 87, 93, 163, 59, 73, 190, 17,
37, 187, 146, 46, 51, 73, 79, 73, 136, 40, 27,
47, 73, 9, 110, 62, 93, 189, 15, 207, 169, 192,
192, 205, 146, 217, 171, 59, 33, 84, 75, 52, 213,
221, 74, 101, 217, 139, 135, 139, 153, 34
]);
});
should('sol: format private key throws', () => {
const key = hex.decode('99da9559e15e913ee9ab2e53e3dfad575da33b49be1125bb922e33494f498828');
throws(() => sol.formatPrivate(key, 'foobar'));
});
for (let i = 0; i < vectors.keypair.length; i++) {
should(`sol: key generation ${i}`, () => {
const { priv, pub } = vectors.keypair[i];
Expand Down

0 comments on commit c436b6d

Please sign in to comment.