Skip to content

Commit 3d5db14

Browse files
committed
added getCompressedPublicKey to utils
1 parent 91d5022 commit 3d5db14

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@wireio/core",
33
"description": "Library for working with Wire powered blockchains.",
4-
"version": "0.0.9-3",
4+
"version": "0.0.9-4",
55
"homepage": "https://github.com/Wire-Network/sdk-core",
66
"license": "FSL-1.1-Apache-2.0",
77
"main": "lib/core.js",

src/utils.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ABISerializableObject } from './serializer/serializable';
22
import rand from 'brorand';
33
import { Base58 } from './base58';
4+
import { getCurve } from './crypto/curves';
45

56
export function arrayEquals(a: ArrayLike<number>, b: ArrayLike<number>) {
67
const len = a.length;
@@ -174,4 +175,25 @@ export function evmSigToWire(eth_sig: string, prefix = 'EM') {
174175
);
175176

176177
return `SIG_${prefix}_${payload}`;
177-
}
178+
}
179+
180+
const ec = getCurve('K1')
181+
182+
/**
183+
* Get the public key in compressed format from a public or private key.
184+
*
185+
* @param key Either a public or private key
186+
* @param isPrivate Boolean indicating if the key is private, defaults to false.
187+
* @returns The public key in compressed format.
188+
*/
189+
190+
export const getCompressedPublicKey = (
191+
key: string,
192+
isPrivate = false
193+
): string => {
194+
if (key.startsWith('0x')) key = key.slice(2);
195+
const keyPair = isPrivate
196+
? ec.keyFromPrivate(key)
197+
: ec.keyFromPublic(key, 'hex');
198+
return keyPair.getPublic(true, 'hex');
199+
};

0 commit comments

Comments
 (0)