Skip to content

Commit

Permalink
use mina provider package and fix hardcoded network
Browse files Browse the repository at this point in the history
  • Loading branch information
ecioppettini committed Apr 25, 2024
1 parent e565ced commit 03cd9d2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 34 deletions.
8 changes: 3 additions & 5 deletions packages/paima-sdk/paima-crypto/src/mina.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ export class MinaCrypto implements IVerify {
sigStruct: string
): Promise<boolean> => {
try {
const [field, scalar, ...remainder] = sigStruct.split(';');
if (!field || !scalar || remainder.length > 0) {
const [field, scalar, network, ...remainder] = sigStruct.split(';');
if (!field || !scalar || !network || remainder.length > 0) {
return false;
}

const Client = require('mina-signer');

// type Network = 'mainnet' | 'testnet'
// TODO: unhardcode, but not sure yet where to get this
const signerClient = new Client({ network: 'testnet' });
const signerClient = new Client({ network });

const verifyBody = {
data: message,
Expand Down
1 change: 1 addition & 0 deletions packages/paima-sdk/paima-providers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@paima/utils": "2.3.0",
"@perawallet/connect": "^1.2.3",
"@polkadot/extension-dapp": "^0.44.9",
"@aurowallet/mina-provider": "^1.0.2",
"bech32": "^2.0.0",
"web3": "1.10.0",
"web3-utils": "1.10.0",
Expand Down
51 changes: 22 additions & 29 deletions packages/paima-sdk/paima-providers/src/mina.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,11 @@ import {
WalletNotFound,
} from './errors.js';
import { getWindow } from './window.js';
import AuroMinaApi from '@aurowallet/mina-provider';

type MinaAddress = any;

// TODO: import the provider package instead
type SignMessageArgs = {
readonly message: string;
};

interface SignedData {
publicKey: string;
data: string;
signature: {
field: string;
scalar: string;
};
}
export type MinaApi = AuroMinaApi;

interface ProviderError extends Error {
message: string;
code: number;
data?: unknown;
}

Promise<SignedData | ProviderError>;
export interface MinaApi {
signMessage: (args: SignMessageArgs) => Promise<SignedData>;
requestAccounts: () => Promise<string[]>;
}
type MinaAddress = any;

declare global {
interface Window {
Expand Down Expand Up @@ -157,15 +134,31 @@ export class MinaProvider implements IProvider<MinaApi> {
signMessage = async (message: string): Promise<UserSignature> => {
// There is no way of choosing the signing account here. At most we could
// monitor the changed events and erroring out if it changed.
const { signature } = await this.conn.api.signMessage({ message: message });
return `${signature.field};${signature.scalar}`;
const signed = await this.conn.api.signMessage({ message: message });

if ('signature' in signed) {
const { signature } = signed;

const networkKind = await this.conn.api.requestNetwork();

// only these two options are used
// https://github.com/aurowallet/auro-wallet-browser-extension/blob/624de322dd99baaa09617bbad4a4a838f0a88edc/src/background/lib/index.js#L17
// this is part of the hashed message
const network = networkKind.chainId === 'mainnet' ? 'mainnet' : 'testnet';

return `${signature.field};${signature.scalar};${network}`;
} else {
throw new ProviderApiError('Failed to sign message');
}
};

static fetchAddress = async (conn: ActiveConnection<MinaApi>): Promise<string> => {
try {
const addresses = await conn.api.requestAccounts();
if (addresses.length > 0) {
if ('length' in addresses && addresses.length > 0) {
return addresses[0];
} else {
throw new ProviderApiError('requestAccounts request failed');
}
} catch (err) {
console.log('[pickMinaAddress] error calling requestAccounts:', err);
Expand Down

0 comments on commit 03cd9d2

Please sign in to comment.