Multichain authentication

Enhanced functionality and blockchain support with molecule.sh

molecule.sh not only serves as the global library for MEM functions, offering access to pre-defined plugins and reusable code, but it also provides support for 14 networks. This allows your functions to seamlessly authenticate with various network standards.

For example, you can code a MEM function that is compatible with MetaMask wallet for caller validation (using msg.sender in this case).

Supported Networks

Networkelliptic curve / crypto-algo

EVM

secp256k1

Dfinity

ed25519

Arweave

rsa256

Solana

ed25519

Zilliqa

EC-Schnorr (ECC)

Tron

ECDSA

Stacks (STX)

ECDSA

Substrate

sr25519

TON

ed25519

Massa

ed25519

Tezos (tz1 address)

ed25519

Aptos

PureEdDSA

Nostr

Schnorr (over secp256k1)

Fuels

secp256k1

Example: EVM-based Caller Authentication with signer atom of evm molecule

The following example demonstrates a MEM function implementation that utilizes EVM for caller authentication (action.caller or similar to msg.sender). This authentication mechanism is provided by the signer atom of the evm molecule. This approach ensures secure and reliable validation of the function's caller.

MEM function source code

function.js
export async function handle(state, action) {
  const input = action.input;

  const names = state.names;
  const signatures = state.signatures;
  const verification_message = state.verification_message;
  const evm_molecule_endpoint = state.evm_molecule_endpoint;

  if (input.function === "register") {
    const name = input.name;
    const caller = input.caller;
    const signature = input.signature;

    ContractAssert(name.trim().length, "error invalid name");
    ContractAssert(!(name in names), "name already registered");
    ContractAssert(caller && signature, "missing required arguments");
    ContractAssert(
      !signatures.includes(signature),
      "error signed message used"
    );

    const message = btoa(verification_message);
    await _moleculeSignatureVerification(caller, message, signature);
    state.names[caller] = name.trim();
    signatures.push(signature);

    return { state };
  }

  async function _moleculeSignatureVerification(caller, message, signature) {
    try {
      const isValid = await EXM.deterministicFetch(
        `${evm_molecule_endpoint}/signer/${caller}/${message}/${signature}`
      );
      ContractAssert(isValid.asJSON()?.result, "unauthorized caller");
    } catch (error) {
      throw new ContractError("molecule res error");
    }
  }
}

MEM function initial state

state.json
{
	"names": {},
	"signatures": [],
	"verification_message": "hello world",
	"evm_molecule_endpoint": "http://evm.molecule.sh"
}

Last updated

mem.tech ยฉ 2023