Skip to content

Commit

Permalink
Dynamically import crypto-specific packages (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienGllmt authored Oct 7, 2023
1 parent 2168ed6 commit 2ac58fa
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 23 deletions.
24 changes: 13 additions & 11 deletions packages/paima-sdk/paima-crypto/src/algorand.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { SignedTransaction, Transaction, SuggestedParams } from 'algosdk';
import algosdk from 'algosdk';
import nacl from 'tweetnacl';
import { doLog, hexStringToUint8Array } from '@paima/utils';
import web3UtilsPkg from 'web3-utils';
import type { IVerify } from './IVerify';
Expand All @@ -17,19 +15,19 @@ export class AlgorandCrypto implements IVerify {
): Promise<boolean> => {
try {
const sig = Buffer.from(hexStringToUint8Array(signature));
const txn = this.buildAlgorandTransaction(userAddress, message);
const txn = await this.buildAlgorandTransaction(userAddress, message);
const signedTx: SignedTransaction = {
txn,
sig,
};
return await Promise.resolve(this.verifySignedTransaction(signedTx));
return await this.verifySignedTransaction(signedTx);
} catch (err) {
doLog(`[funnel] error verifying algorand signature: ${err}`);
return await Promise.resolve(false);
}
};

buildAlgorandTransaction = (userAddress: string, message: string): Transaction => {
buildAlgorandTransaction = async (userAddress: string, message: string): Promise<Transaction> => {
const hexMessage = web3UtilsPkg.utf8ToHex(message).slice(2);
const msgArray = hexStringToUint8Array(hexMessage);
const SUGGESTED_PARAMS: SuggestedParams = {
Expand All @@ -39,7 +37,8 @@ export class AlgorandCrypto implements IVerify {
genesisID: 'mainnet-v1.0',
genesisHash: 'wGHE2Pwdvd7S12BL5FaOP20EGYesN73ktiC1qzkkit8=',
};
return algosdk.makePaymentTxnWithSuggestedParams(
const { makePaymentTxnWithSuggestedParams } = await import('algosdk');
return makePaymentTxnWithSuggestedParams(
userAddress,
userAddress,
0,
Expand All @@ -49,22 +48,25 @@ export class AlgorandCrypto implements IVerify {
);
};

verifySignedTransaction = (signedTransaction: SignedTransaction): boolean => {
verifySignedTransaction = async (signedTransaction: SignedTransaction): Promise<boolean> => {
if (signedTransaction.sig === undefined) return false;

const pkBytes = signedTransaction.txn.from.publicKey;

const signatureBytes = new Uint8Array(signedTransaction.sig);

const transactionBytes = algosdk.encodeObj(signedTransaction.txn.get_obj_for_encoding());
const { encodeObj } = await import('algosdk');
const transactionBytes = encodeObj(signedTransaction.txn.get_obj_for_encoding());
const messageBytes = new Uint8Array(transactionBytes.length + 2);
messageBytes.set(Buffer.from('TX'));
messageBytes.set(transactionBytes, 2);

return nacl.sign.detached.verify(messageBytes, signatureBytes, pkBytes);
const { sign } = await import('tweetnacl');
return sign.detached.verify(messageBytes, signatureBytes, pkBytes);
};

decodeSignedTransaction = (signedTx: Uint8Array): SignedTransaction => {
return algosdk.decodeSignedTransaction(signedTx);
decodeSignedTransaction = async (signedTx: Uint8Array): Promise<SignedTransaction> => {
const { decodeSignedTransaction } = await import('algosdk');
return decodeSignedTransaction(signedTx);
};
}
4 changes: 3 additions & 1 deletion packages/paima-sdk/paima-crypto/src/cardano.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import verifyCardanoDataSignature from '@cardano-foundation/cardano-verify-datasignature';
import { doLog } from '@paima/utils';
import type { IVerify } from './IVerify';

Expand All @@ -17,6 +16,9 @@ export class CardanoCrypto implements IVerify {
if (!signature || !key || remainder.length > 0) {
return false;
}
const { default: verifyCardanoDataSignature } = await import(
'@cardano-foundation/cardano-verify-datasignature'
);
return verifyCardanoDataSignature(signature, key, message, userAddress);
} catch (err) {
doLog('[address-validator] error verifying cardano signature:', err);
Expand Down
6 changes: 4 additions & 2 deletions packages/paima-sdk/paima-crypto/src/polkadot.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { cryptoWaitReady, decodeAddress, signatureVerify } from '@polkadot/util-crypto';
import { u8aToHex } from '@polkadot/util';
import { doLog } from '@paima/utils';
import type { IVerify } from './IVerify';

Expand All @@ -14,6 +12,10 @@ export class PolkadotCrypto implements IVerify {
signature: string
): Promise<boolean> => {
try {
const { cryptoWaitReady, decodeAddress, signatureVerify } = await import(
'@polkadot/util-crypto'
);
const { u8aToHex } = await import('@polkadot/util');
await cryptoWaitReady();
const publicKey = decodeAddress(userAddress);
const hexPublicKey = u8aToHex(publicKey);
Expand Down
7 changes: 4 additions & 3 deletions packages/paima-sdk/paima-providers/src/algorand.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PeraWalletConnect } from '@perawallet/connect';
import type { PeraWalletConnect } from '@perawallet/connect';
import type { ActiveConnection, GameInfo, IConnector, IProvider, UserSignature } from './IProvider';
import { CryptoManager } from '@paima/crypto';
import { uint8ArrayToHexString } from '@paima/utils';
Expand Down Expand Up @@ -44,6 +44,7 @@ export class AlgorandConnector implements IConnector<AlgorandApi> {
if (name !== SupportedAlgorandWallets.PERA) {
throw new UnsupportedWallet(`AlgorandProvider: unknown connection type ${name}`);
}
const { PeraWalletConnect } = await import('@perawallet/connect');
const peraWallet = new PeraWalletConnect();
return await this.connectExternal(gameInfo, {
metadata: {
Expand Down Expand Up @@ -89,7 +90,7 @@ export class AlgorandProvider implements IProvider<AlgorandApi> {
return this.address;
};
signMessage = async (message: string): Promise<UserSignature> => {
const txn = CryptoManager.Algorand().buildAlgorandTransaction(this.getAddress(), message);
const txn = await CryptoManager.Algorand().buildAlgorandTransaction(this.getAddress(), message);
const signerTx = {
txn,
signers: [this.getAddress()],
Expand All @@ -101,7 +102,7 @@ export class AlgorandProvider implements IProvider<AlgorandApi> {
`[signMessageAlgorand] invalid number of signatures returned: ${signedTxs.length}`
);
}
const signedTx = CryptoManager.Algorand().decodeSignedTransaction(signedTxs[0]);
const signedTx = await CryptoManager.Algorand().decodeSignedTransaction(signedTxs[0]);
const signature = signedTx.sig;
if (!signature) {
throw new ProviderApiError(`[signMessageAlgorand] signature missing in signed Tx`);
Expand Down
9 changes: 3 additions & 6 deletions packages/paima-sdk/paima-providers/src/polkadot.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import {
web3Accounts,
web3Enable,
web3FromAddress,
web3FromSource,
} from '@polkadot/extension-dapp';
import type { ActiveConnection, GameInfo, IConnector, IProvider, UserSignature } from './IProvider';
import {
ProviderApiError,
Expand Down Expand Up @@ -32,6 +26,7 @@ export class PolkadotConnector implements IConnector<PolkadotApi> {
if (this.provider != null) {
return this.provider;
}
const { web3Accounts, web3Enable, web3FromAddress } = await import('@polkadot/extension-dapp');
const extensions = await web3Enable(gameInfo.gameName);
if (extensions.length === 0) {
throw new WalletNotFound(`[polkadot] no extension detected`);
Expand Down Expand Up @@ -61,6 +56,8 @@ export class PolkadotConnector implements IConnector<PolkadotApi> {
return this.provider;
}

const { web3Enable, web3FromSource } = await import('@polkadot/extension-dapp');

await web3Enable(gameInfo.gameName);
try {
const injector = await web3FromSource(name);
Expand Down

0 comments on commit 2ac58fa

Please sign in to comment.