Skip to content

Commit

Permalink
Merge pull request #47 from casper-ecosystem/hotfix/0.3.7
Browse files Browse the repository at this point in the history
Hotfix: Adds getActivePublicKey() method
  • Loading branch information
Maciej Zieliński authored Apr 23, 2021
2 parents e7f86d1 + 997e34b commit a8b87a4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "casperlabs-signer",
"version": "0.3.6",
"version": "0.3.7",
"private": true,
"dependencies": {
"@fortawesome/fontawesome-free": "^5.13.0",
Expand Down
2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"manifest_version": 2,
"version": "0.3.6",
"version": "0.3.7",
"name": "CasperLabs Signer",
"author": "https://casperlabs.io",
"description": "CasperLabs Signer tool for signing transactions on the blockchain.",
Expand Down
36 changes: 36 additions & 0 deletions src/background/SignMessageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ import { AppState } from '../lib/MemStore';
import PopupManager from '../background/PopupManager';
import { encodeBase64 } from 'tweetnacl-ts';
import * as nacl from 'tweetnacl-ts';
import { encodeBase16 } from 'casper-client-sdk';

const ed25519Key = {
prefix: '01',
length: 32
};

const secp256k1Key = {
prefix: '02',
length: 33
};

type SignMessageStatus = 'unsigned' | 'signed' | 'rejected';

Expand Down Expand Up @@ -65,6 +76,31 @@ export default class SignMessageManager extends events.EventEmitter {
});
}

/**
* Retrieve the active public key .
* @returns {string} Hex-encoded public key with algorithm prefix.
*/
public getActivePublicKey() {
return new Promise<string>((resolve, reject) => {
let publicKeyBytes = this.appState.selectedUserAccount?.signKeyPair
.publicKey;
if (!this.appState.connectionStatus) {
return reject(new Error('Please connect to the Signer first.'));
} else if (publicKeyBytes === undefined) {
return reject(new Error('Please create an account first.'));
}

switch (publicKeyBytes.length) {
case ed25519Key.length:
return resolve(ed25519Key.prefix + encodeBase16(publicKeyBytes));
case secp256k1Key.length:
return resolve(secp256k1Key.prefix + encodeBase16(publicKeyBytes));
default:
return reject(new Error('Key was not of expected format!'));
}
});
}

// return base64 encoded public key of the current selected account only if connected
public getSelectedPublicKeyBase64() {
return new Promise((resolve, reject) => {
Expand Down
4 changes: 4 additions & 0 deletions src/content/inpage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class CasperLabsPluginHelper {
async getSelectedPublicKeyBase64() {
return this.call<string>('getSelectedPublicKeyBase64');
}

async getActivePublicKey() {
return this.call<string>('getActivePublicKey');
}
}

// inject to window, so that Clarity code could use it.
Expand Down
4 changes: 4 additions & 0 deletions src/lib/rpc/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export function setupInjectPageAPIServer(
'getSelectedPublicKeyBase64',
signMessageManager.getSelectedPublicKeyBase64.bind(signMessageManager)
);
rpc.register(
'getActivePublicKey',
signMessageManager.getActivePublicKey.bind(signMessageManager)
);
rpc.register(
'isConnected',
connectionManager.isConnected.bind(connectionManager)
Expand Down

0 comments on commit a8b87a4

Please sign in to comment.