Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mina/auro wallet support #355

Merged
merged 8 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,370 changes: 1,002 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;
}
}
42 changes: 42 additions & 0 deletions packages/paima-sdk/paima-crypto/src/mina.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { doLog } from '@paima/utils';
import type { IVerify } from './IVerify.js';

export class MinaCrypto implements IVerify {
verifyAddress = async (address: string): Promise<boolean> => {
// base58 alphabet
return await Promise.resolve(
/^[1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|G|H|J|K|L|M|N|P|Q|R|S|T|U|V|W|X|Y|Z|a|b|c|d|e|f|g|h|i|j|k|m|n|o|p|q|r|s|t|u|v|w|x|y|z]/.test(
SebastienGllmt marked this conversation as resolved.
Show resolved Hide resolved
address
)
);
};
verifySignature = async (
userAddress: string,
message: string,
sigStruct: string
): Promise<boolean> => {
try {
const [field, scalar, network, ...remainder] = sigStruct.split(';');
if (!field || !scalar || !network || remainder.length > 0) {
return false;
}

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

const signerClient = new Client({ network });

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,
SebastienGllmt marked this conversation as resolved.
Show resolved Hide resolved
}

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
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
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