Skip to content

Commit

Permalink
mina/auro wallet support (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecioppettini committed Apr 25, 2024
1 parent 88acf1c commit e057b7e
Show file tree
Hide file tree
Showing 17 changed files with 1,265 additions and 370 deletions.
1,331 changes: 963 additions & 368 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions packages/batcher/address-validator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ class PaimaAddressValidator {
return await CryptoManager.Polkadot().verifyAddress(address);
case AddressType.ALGORAND:
return await CryptoManager.Algorand().verifyAddress(address);
case AddressType.MINA:
return await CryptoManager.Mina().verifyAddress(address);
case AddressType.UNKNOWN:
return false;
default:
Expand Down Expand Up @@ -130,6 +132,12 @@ class PaimaAddressValidator {
message,
input.userSignature
);
case AddressType.MINA:
return await CryptoManager.Mina().verifySignature(
input.userAddress,
message,
input.userSignature
);
case AddressType.UNKNOWN:
return false;
default:
Expand Down
3 changes: 3 additions & 0 deletions packages/batcher/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const SUPPORTED_CHAIN_NAMES: string[] = [
addressTypeName(AddressType.POLKADOT),
addressTypeName(AddressType.CARDANO),
addressTypeName(AddressType.ALGORAND),
addressTypeName(AddressType.MINA),
];

const POLLING_INTERVAL = 10000;
Expand All @@ -70,6 +71,8 @@ export function addressTypeName(addressType: AddressType): string {
return 'Astar / Polkadot';
case AddressType.ALGORAND:
return 'Algorand';
case AddressType.MINA:
return 'Mina';
case AddressType.UNKNOWN:
return 'Unknown address type';
default:
Expand Down
2 changes: 2 additions & 0 deletions packages/engine/paima-funnel/src/paima-l2-processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ async function validateSubunitSignature(
return await CryptoManager.Polkadot().verifySignature(userAddress, message, userSignature);
case AddressType.ALGORAND:
return await CryptoManager.Algorand().verifySignature(userAddress, message, userSignature);
case AddressType.MINA:
return await CryptoManager.Mina().verifySignature(userAddress, message, userSignature);
default:
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/paima-sdk/paima-crypto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@polkadot/util": "^10.4.2",
"@polkadot/util-crypto": "^10.4.2",
"bech32": "^2.0.0",
"@paima/utils": "2.3.0"
"@paima/utils": "2.3.0",
"mina-signer": "2.1.1"
}
}
9 changes: 9 additions & 0 deletions packages/paima-sdk/paima-crypto/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AlgorandCrypto } from './algorand.js';
import { CardanoCrypto } from './cardano.js';
import { EvmCrypto } from './evm.js';
import { PolkadotCrypto } from './polkadot.js';
import { MinaCrypto } from './mina.js';
export { AlgorandCrypto, CardanoCrypto, EvmCrypto, PolkadotCrypto };
export type * from './IVerify.js';

Expand All @@ -13,6 +14,7 @@ export class CryptoManager {
private static cardano: CardanoCrypto | undefined;
private static evm: EvmCrypto | undefined;
private static polkadot: PolkadotCrypto | undefined;
private static mina: MinaCrypto | undefined;

static Algorand(): AlgorandCrypto {
if (CryptoManager.algorand == null) {
Expand Down Expand Up @@ -41,4 +43,11 @@ export class CryptoManager {
}
return CryptoManager.polkadot;
}

static Mina(): MinaCrypto {
if (CryptoManager.mina == null) {
CryptoManager.mina = new MinaCrypto();
}
return CryptoManager.mina;
}
}
40 changes: 40 additions & 0 deletions packages/paima-sdk/paima-crypto/src/mina.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { doLog } from '@paima/utils';
import type { IVerify } from './IVerify.js';

export class MinaCrypto implements IVerify {
verifyAddress = async (address: string): Promise<boolean> => {
// TODO: improve
return await Promise.resolve(/^[a-zA-Z0-9]+$/.test(address));
};
verifySignature = async (
userAddress: string,
message: string,
sigStruct: string
): Promise<boolean> => {
try {
const [field, scalar, ...remainder] = sigStruct.split(';');
if (!field || !scalar || 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 verifyBody = {
data: message,
publicKey: userAddress,
signature: { field, scalar },
};

const verifyResult = signerClient.verifyMessage(verifyBody);

return verifyResult;
} catch (err) {
doLog('[address-validator] error verifying mina signature:', err);
return false;
}
};
}
7 changes: 7 additions & 0 deletions packages/paima-sdk/paima-mw-core/src/delegate-wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
PolkadotConnector,
AlgorandConnector,
WalletMode,
MinaConnector,
} from '@paima/providers';
import { AddressType, ENV } from '@paima/utils';
import { paimaEndpoints } from '../index.js';
Expand Down Expand Up @@ -124,6 +125,9 @@ export class WalletConnectHelper {
case AddressType.ALGORAND:
provider = AlgorandConnector.instance().getProvider();
break;
case AddressType.MINA:
provider = MinaConnector.instance().getProvider();
break;
}
if (!provider) throw new Error('Cannot get provider ' + walletType);
return provider;
Expand Down Expand Up @@ -160,6 +164,9 @@ export class WalletConnectHelper {
case AddressType.ALGORAND:
loginInfo = { mode: WalletMode.Algorand };
break;
case AddressType.MINA:
loginInfo = { mode: WalletMode.Mina };
break;
case AddressType.UNKNOWN:
throw new Error('AddressTypes cannot be Unknown.');
default:
Expand Down
2 changes: 2 additions & 0 deletions packages/paima-sdk/paima-mw-core/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const enum PaimaMiddlewareErrorCode {
INTERNAL_INVALID_POSTING_MODE,
// only to be used as a counter
FINAL_PAIMA_GENERIC_ERROR = 1_000_000,
MINA_LOGIN,
}

export const PAIMA_MIDDLEWARE_ERROR_MESSAGES: Record<PaimaMiddlewareErrorCode, string> = {
Expand Down Expand Up @@ -102,6 +103,7 @@ export const PAIMA_MIDDLEWARE_ERROR_MESSAGES: Record<PaimaMiddlewareErrorCode, s
'Internal error: Invalid posting mode set',
[PaimaMiddlewareErrorCode.FINAL_PAIMA_GENERIC_ERROR]:
'Internal error: unknown generic paima error (FINAL_PAIMA_GENERIC_ERROR)',
[PaimaMiddlewareErrorCode.MINA_LOGIN]: 'Error while connecting to the Mina wallet',
};

export const paimaErrorMessageFxn: ErrorMessageFxn = buildErrorCodeTranslator(
Expand Down
32 changes: 32 additions & 0 deletions packages/paima-sdk/paima-mw-core/src/wallets/mina.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { LoginInfoMap, Result } from '../types.js';
import { PaimaMiddlewareErrorCode, buildEndpointErrorFxn } from '../errors.js';
import { MinaConnector } from '@paima/providers';
import type { ApiForMode, IProvider, WalletMode } from '@paima/providers';
import { getGameName } from '../state.js';
import { connectInjected } from './wallet-modes.js';

export async function minaLoginWrapper(
loginInfo: LoginInfoMap[WalletMode.Mina]
): Promise<Result<IProvider<ApiForMode<WalletMode.Mina>>>> {
const errorFxn = buildEndpointErrorFxn('minaLoginWrapper');

const gameInfo = {
gameName: getGameName(),
gameChainId: undefined, // Not needed because of batcher
};
const loginResult = await connectInjected(
'minaLoginWrapper',
errorFxn,
PaimaMiddlewareErrorCode.MINA_LOGIN,
loginInfo,
MinaConnector.instance(),
gameInfo
);
if (loginResult.success === false) {
return loginResult;
}
return {
success: true,
result: loginResult.result,
};
}
2 changes: 2 additions & 0 deletions packages/paima-sdk/paima-mw-core/src/wallets/wallet-modes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
EthersApi,
InjectionPreference,
WalletMode,
MinaApi,
} from '@paima/providers';
import { WalletNotFound, UnsupportedWallet, connectInjectedWallet } from '@paima/providers';
import type { EndpointErrorFxn } from '../errors.js';
Expand Down Expand Up @@ -37,6 +38,7 @@ export type LoginInfoMap = {
[WalletMode.Cardano]: BaseLoginInfo<CardanoApi>;
[WalletMode.Polkadot]: BaseLoginInfo<PolkadotApi>;
[WalletMode.Algorand]: BaseLoginInfo<AlgorandApi>;
[WalletMode.Mina]: BaseLoginInfo<MinaApi>;
};

type ToUnion<T> = {
Expand Down
4 changes: 4 additions & 0 deletions packages/paima-sdk/paima-mw-core/src/wallets/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ethersLoginWrapper } from './evm/ethers.js';
import type { IProvider } from '@paima/providers';
import { WalletMode } from '@paima/providers';
import { PostingMode, addLogin, setDefaultProvider, setPostingMode } from '../state.js';
import { minaLoginWrapper } from './mina.js';

export async function specificWalletLogin(
loginInfo: LoginInfo,
Expand All @@ -33,6 +34,9 @@ export async function specificWalletLogin(
case WalletMode.Algorand: {
return await algorandLoginWrapper(loginInfo);
}
case WalletMode.Mina: {
return await minaLoginWrapper(loginInfo);
}
default:
assertNever(loginInfo, true);
return errorFxn(FE_ERR_SPECIFIC_WALLET_NOT_INSTALLED);
Expand Down
2 changes: 2 additions & 0 deletions packages/paima-sdk/paima-providers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ export * from './algorand.js';
export * from './cardano.js';
export * from './evm/index.js';
export * from './polkadot.js';
export * from './mina.js';
export * from './errors.js';
export * from './utils.js';
export type * from './algorand.js';
export type * from './cardano.js';
export type * from './evm/index.js';
export type * from './polkadot.js';
export type * from './mina.js';
export type * from './IProvider.js';
export type * from './utils.js';
Loading

0 comments on commit e057b7e

Please sign in to comment.