Skip to content

Commit

Permalink
Merge pull request #115 from massalabs/return-signatures-as-base58enc…
Browse files Browse the repository at this point in the history
…oded-string

update sig type to base58encoded string
  • Loading branch information
Ben-Rey authored Aug 23, 2023
2 parents 8363ffc + 69b4d46 commit 01f8c71
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/account/Account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
IAccountBalanceRequest,
IAccountBalanceResponse,
} from './AccountBalance';
import { IAccountSignRequest, IAccountSignResponse } from './AccountSign';
import { IAccountSignOutput, IAccountSignRequest } from './AccountSign';
import { connector } from '../connector/Connector';
import { IAccountDetails } from './IAccountDetails';
import { AvailableCommands, ITransactionDetails } from '..';
Expand Down Expand Up @@ -89,15 +89,15 @@ export class Account implements IAccount {
* @param data - The message to be signed.
* @returns An IAccountSignResponse object. It contains the signature of the message.
*/
public async sign(data: Buffer | Uint8Array): Promise<IAccountSignResponse> {
return new Promise<IAccountSignResponse>((resolve, reject) => {
public async sign(data: Buffer | Uint8Array): Promise<IAccountSignOutput> {
return new Promise<IAccountSignOutput>((resolve, reject) => {
connector.sendMessageToContentScript(
this._providerName,
AvailableCommands.AccountSign,
{ address: this._address, data } as IAccountSignRequest,
(result, err) => {
if (err) return reject(err);
return resolve(result as IAccountSignResponse);
return resolve(result as IAccountSignOutput);
},
);
});
Expand Down
10 changes: 9 additions & 1 deletion src/account/AccountSign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,13 @@ export interface IAccountSignRequest {
*/
export interface IAccountSignResponse {
publicKey: string;
signature: Uint8Array;
signature: string;
}

/**
* This interface represents the output of a sign() method
*/
export interface IAccountSignOutput {
publicKey: string;
base58Encoded: string;
}
4 changes: 2 additions & 2 deletions src/account/IAccount.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ITransactionDetails } from '..';
import { IAccountBalanceResponse } from './AccountBalance';
import { IAccountSignResponse } from './AccountSign';
import { IAccountSignOutput } from './AccountSign';
import { Args } from '@massalabs/web3-utils';

/**
Expand All @@ -12,7 +12,7 @@ export interface IAccount {
name(): string;
providerName(): string;
balance(): Promise<IAccountBalanceResponse>;
sign(data: Buffer | Uint8Array | string): Promise<IAccountSignResponse>;
sign(data: Buffer | Uint8Array | string): Promise<IAccountSignOutput>;
buyRolls(amount: bigint, fee: bigint): Promise<ITransactionDetails>;
sellRolls(amount: bigint, fee: bigint): Promise<ITransactionDetails>;
sendTransaction(
Expand Down
7 changes: 4 additions & 3 deletions src/bearbyWallet/BearbyAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { NodeStatus } from './NodeStatus';
import { JSON_RPC_REQUEST_METHOD } from './jsonRpcMethods';
import axios, { AxiosRequestHeaders, AxiosResponse } from 'axios';
import { decode } from 'bs58check';
import { IAccountSignOutput } from '../account/AccountSign';
/**
* The maximum allowed gas for a read operation
*/
Expand Down Expand Up @@ -115,7 +116,7 @@ export class BearbyAccount implements IAccount {
// need testing
public async sign(
data: Buffer | Uint8Array | string,
): Promise<IAccountSignResponse> {
): Promise<IAccountSignOutput> {
await this.connect();

let strData: string;
Expand All @@ -128,8 +129,8 @@ export class BearbyAccount implements IAccount {
const signature = await web3.wallet.signMessage(strData);
return {
publicKey: signature.publicKey,
signature: decode(signature.signature),
} as IAccountSignResponse;
base58Encoded: signature.signature,
};
}

// need testing
Expand Down
15 changes: 10 additions & 5 deletions src/massaStation/MassaStationAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
} from '@massalabs/web3-utils';

import { argsToBase64, uint8ArrayToBase64 } from '../utils/argsToBase64';
import { IAccountSignOutput } from '../account/AccountSign';
import { encode as base58Encode } from 'bs58check';

/**
* The maximum allowed gas for a read operation
Expand Down Expand Up @@ -137,8 +139,9 @@ export class MassaStationAccount implements IAccount {
*/
public async sign(
data: Buffer | Uint8Array | string,
): Promise<IAccountSignResponse> {
): Promise<IAccountSignOutput> {
let signOpResponse: JsonRpcResponseData<IAccountSignResponse> = null;

try {
signOpResponse = await postRequest<IAccountSignResponse>(
`${MASSA_STATION_ACCOUNTS_URL}/${this._name}/sign`,
Expand All @@ -151,16 +154,18 @@ export class MassaStationAccount implements IAccount {
console.error(`MassaStation account signing error`);
throw ex;
}

if (signOpResponse.isError || signOpResponse.error) {
throw signOpResponse.error;
}
// convert signOpResponse.result.signature to Uint8Array
const signature = new Uint8Array(
Buffer.from(signOpResponse.result.signature),

const signature = base58Encode(
Buffer.from(signOpResponse.result.signature, 'base64'),
);

return {
publicKey: signOpResponse.result.publicKey,
signature: signature,
base58Encoded: signature,
};
}

Expand Down

0 comments on commit 01f8c71

Please sign in to comment.