Skip to content

Commit

Permalink
Unit tests for wallet API compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
soerenbf committed Oct 25, 2023
1 parent 66272c4 commit 2a29a76
Show file tree
Hide file tree
Showing 12 changed files with 880 additions and 105 deletions.
7 changes: 6 additions & 1 deletion .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ module.exports = {
ecmaFeatures: {
jsx: true,
},
project: ['./tsconfig.json', './packages/browser-wallet/tsconfig.eslint.json', './examples/*/tsconfig.json'],
project: [
'./tsconfig.json',
'./packages/browser-wallet/tsconfig.eslint.json',
'./packages/browser-wallet-api/tsconfig.eslint.json',
'./examples/*/tsconfig.json',
],
tsconfigRootDir: __dirname,
},
env: {
Expand Down
2 changes: 1 addition & 1 deletion examples/piggybank/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const smash = (account: string, index: bigint, subindex = 0n) => {
address: ContractAddress.create(index, subindex),
receiveName: ReceiveName.fromString(`${CONTRACT_NAME}.smash`),
maxContractExecutionEnergy: Energy.create(30000),
} as UpdateContractPayload)
})
.then((txHash) =>
console.log(`https://testnet.ccdscan.io/?dcount=1&dentity=transaction&dhash=${txHash}`)
)
Expand Down
120 changes: 110 additions & 10 deletions packages/browser-wallet-api-helpers/src/wallet-api-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ import type {
Base58String,
Base64String,
ContractAddress,
UpdateCredentialsPayload,
RegisterDataPayload,
SimpleTransferPayload,
SimpleTransferWithMemoPayload,
DeployModulePayload,
ConfigureBakerPayload,
ConfigureDelegationPayload,
} from '@concordium/web-sdk';
import type { RpcTransport } from '@protobuf-ts/runtime-rpc';
import { LaxNumberEnumValue, LaxStringEnumValue } from './util';
Expand Down Expand Up @@ -51,10 +58,13 @@ export interface CredentialProof {
verificationMethod: string;
}

export type SendTransactionUpdateContractPayload = Omit<UpdateContractPayload, 'message'>;
export type SendTransactionInitContractPayload = Omit<InitContractPayload, 'param'>;

export type SendTransactionPayload =
| Exclude<AccountTransactionPayload, UpdateContractPayload | InitContractPayload>
| Omit<UpdateContractPayload, 'message'>
| Omit<InitContractPayload, 'param'>;
| SendTransactionUpdateContractPayload
| SendTransactionInitContractPayload;

export type SmartContractParameters =
| { [key: string]: SmartContractParameters }
Expand Down Expand Up @@ -114,18 +124,108 @@ interface MainWalletApi {
* @param accountAddress the address of the account that should sign the transaction
* @param type the type of transaction that is to be signed and sent.
* @param payload the payload of the transaction to be signed and sent. Note that for smart contract transactions, the payload should not contain the params/message fields, those should instead be provided in the subsequent argument instead.
* @param parameters parameters for the initContract and updateContract transactions in JSON-like format.
* @param schema schema used for the initContract and updateContract transactions to serialize the parameters. Should be base64 encoded.
* @param [parameters] parameters for the initContract and updateContract transactions in JSON-like format.
* @param [schema] schema used for the initContract and updateContract transactions to serialize the parameters. Should be base64 encoded.
* @param [schemaVersion] version of the schema provided. Must be supplied for schemas that use version 0 or 1, as they don't have the version embedded.
*/
sendTransaction(
accountAddress: AccountAddressSource,
type: LaxNumberEnumValue<AccountTransactionType.Update | AccountTransactionType.InitContract>,
payload: SendTransactionPayload,
parameters: SmartContractParameters,
schema: SchemaSource,
type: LaxNumberEnumValue<AccountTransactionType.InitContract>,
payload: SendTransactionInitContractPayload,
parameters?: SmartContractParameters,
schema?: SchemaSource,
schemaVersion?: SchemaVersion
): Promise<string>;
/**
* Sends a transaction to the Concordium Wallet and awaits the users action. Note that a header is not sent, and will be constructed by the wallet itself.
* Note that if the user rejects signing the transaction, this will throw an error.
* @param accountAddress the address of the account that should sign the transaction
* @param type the type of transaction that is to be signed and sent.
* @param payload the payload of the transaction to be signed and sent. Note that for smart contract transactions, the payload should not contain the params/message fields, those should instead be provided in the subsequent argument instead.
* @param [parameters] parameters for the initContract and updateContract transactions in JSON-like format.
* @param [schema] schema used for the initContract and updateContract transactions to serialize the parameters. Should be base64 encoded.
* @param [schemaVersion] version of the schema provided. Must be supplied for schemas that use version 0 or 1, as they don't have the version embedded.
*/
sendTransaction(
accountAddress: AccountAddressSource,
type: LaxNumberEnumValue<AccountTransactionType.Update>,
payload: SendTransactionUpdateContractPayload,
parameters?: SmartContractParameters,
schema?: SchemaSource,
schemaVersion?: SchemaVersion
): Promise<string>;
/**
* Sends a transaction to the Concordium Wallet and awaits the users action. Note that a header is not sent, and will be constructed by the wallet itself.
* Note that if the user rejects signing the transaction, this will throw an error.
* @param accountAddress the address of the account that should sign the transaction
* @param type the type of transaction that is to be signed and sent.
* @param payload the payload of the transaction to be signed and sent. Note that for smart contract transactions, the payload should not contain the parameters, those should instead be provided in the subsequent argument instead.
*/
sendTransaction(
accountAddress: AccountAddressSource,
type: LaxNumberEnumValue<AccountTransactionType.UpdateCredentials>,
payload: UpdateCredentialsPayload
): Promise<string>;
/**
* Sends a transaction to the Concordium Wallet and awaits the users action. Note that a header is not sent, and will be constructed by the wallet itself.
* Note that if the user rejects signing the transaction, this will throw an error.
* @param accountAddress the address of the account that should sign the transaction
* @param type the type of transaction that is to be signed and sent.
* @param payload the payload of the transaction to be signed and sent. Note that for smart contract transactions, the payload should not contain the parameters, those should instead be provided in the subsequent argument instead.
*/
sendTransaction(
accountAddress: AccountAddressSource,
type: LaxNumberEnumValue<AccountTransactionType.RegisterData>,
payload: RegisterDataPayload
): Promise<string>;
/**
* Sends a transaction to the Concordium Wallet and awaits the users action. Note that a header is not sent, and will be constructed by the wallet itself.
* Note that if the user rejects signing the transaction, this will throw an error.
* @param accountAddress the address of the account that should sign the transaction
* @param type the type of transaction that is to be signed and sent.
* @param payload the payload of the transaction to be signed and sent. Note that for smart contract transactions, the payload should not contain the parameters, those should instead be provided in the subsequent argument instead.
*/
sendTransaction(
accountAddress: AccountAddressSource,
type: LaxNumberEnumValue<AccountTransactionType.Transfer>,
payload: SimpleTransferPayload
): Promise<string>;
/**
* Sends a transaction to the Concordium Wallet and awaits the users action. Note that a header is not sent, and will be constructed by the wallet itself.
* Note that if the user rejects signing the transaction, this will throw an error.
* @param accountAddress the address of the account that should sign the transaction
* @param type the type of transaction that is to be signed and sent.
* @param payload the payload of the transaction to be signed and sent. Note that for smart contract transactions, the payload should not contain the parameters, those should instead be provided in the subsequent argument instead.
*/
sendTransaction(
accountAddress: AccountAddressSource,
type: LaxNumberEnumValue<AccountTransactionType.TransferWithMemo>,
payload: SimpleTransferWithMemoPayload
): Promise<string>;
/**
* Sends a transaction to the Concordium Wallet and awaits the users action. Note that a header is not sent, and will be constructed by the wallet itself.
* Note that if the user rejects signing the transaction, this will throw an error.
* @param accountAddress the address of the account that should sign the transaction
* @param type the type of transaction that is to be signed and sent.
* @param payload the payload of the transaction to be signed and sent. Note that for smart contract transactions, the payload should not contain the parameters, those should instead be provided in the subsequent argument instead.
*/
sendTransaction(
accountAddress: AccountAddressSource,
type: LaxNumberEnumValue<AccountTransactionType.DeployModule>,
payload: DeployModulePayload
): Promise<string>;
/**
* Sends a transaction to the Concordium Wallet and awaits the users action. Note that a header is not sent, and will be constructed by the wallet itself.
* Note that if the user rejects signing the transaction, this will throw an error.
* @param accountAddress the address of the account that should sign the transaction
* @param type the type of transaction that is to be signed and sent.
* @param payload the payload of the transaction to be signed and sent. Note that for smart contract transactions, the payload should not contain the parameters, those should instead be provided in the subsequent argument instead.
*/
sendTransaction(
accountAddress: AccountAddressSource,
type: LaxNumberEnumValue<AccountTransactionType.ConfigureBaker>,
payload: ConfigureBakerPayload
): Promise<string>;
/**
* Sends a transaction to the Concordium Wallet and awaits the users action. Note that a header is not sent, and will be constructed by the wallet itself.
* Note that if the user rejects signing the transaction, this will throw an error.
Expand All @@ -135,8 +235,8 @@ interface MainWalletApi {
*/
sendTransaction(
accountAddress: AccountAddressSource,
type: LaxNumberEnumValue<AccountTransactionType>,
payload: SendTransactionPayload
type: LaxNumberEnumValue<AccountTransactionType.ConfigureDelegation>,
payload: ConfigureDelegationPayload
): Promise<string>;
/**
* Sends a message to the Concordium Wallet and awaits the users action. If the user signs the message, this will resolve to the signature.
Expand Down
16 changes: 16 additions & 0 deletions packages/browser-wallet-api/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { pathsToModuleNameMapper } from 'ts-jest';
import type { Config } from '@jest/types';
import tsconfig from './tsconfig.json';

const esModules = [].join('|');

const config: Config.InitialOptions = {
preset: 'ts-jest/presets/js-with-ts-esm',
moduleNameMapper: {
...pathsToModuleNameMapper(tsconfig.compilerOptions.paths, { prefix: '<rootDir>/' }),
},
transformIgnorePatterns: [`node_modules/(?!${esModules})`],
modulePaths: ['<rootDir>/../../node_modules', '<rootDir>/node_modules'],
};

export default config;
8 changes: 8 additions & 0 deletions packages/browser-wallet-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@
"@protobuf-ts/grpcweb-transport": "^2.8.2",
"@protobuf-ts/runtime-rpc": "^2.8.2",
"buffer": "^6.0.3"
},
"devDependencies": {
"jest": "^29.7.0",
"ts-jest": "^29.1.1"
},
"scripts": {
"test": "jest",
"build": "tsc"
}
}
Loading

0 comments on commit 2a29a76

Please sign in to comment.