Function connect

  • Connect to the wallet. This is a necessary step if you want address to work. You must wait for Superhero to announce itself (see detect) before attempting to connect. This has the same return data as detect. This does not pop up the confirmation dialog for the user. Superhero never rejects the connection request. This call is instantaneous in practice.

    Example

    // example calling code
    await sk.connect('ske-connect-1', // message id, can be arbitrary string/number
    {name: 'sidekick examples', // name can be any string
    version: 1}, // version must be 1
    // optional field networkId which is ae_uat or ae_mainnet
    sk.TIMEOUT_DEF_CONNECT_MS, // timeout
    "failed to connect to wallet", // timeout error message
    sk.cl()); // logger
    // example return data (positive case)
    {ok : true,
    result : {id : "{aee9e933-52b6-410a-8c3f-99c6be596b4e}",
    name : "Superhero",
    networkId : "ae_uat",
    origin : "moz-extension://ee425d81-d5b2-44b6-9406-4da31b019e7c",
    type : "extension"}}
    // example return data (negative case)
    {ok : false,
    error : {code : 420,
    message : "failed to detect wallet",
    data : {}}}

    Example

    // example calling code
    // from: https://github.com/aeternity/Vanillae/blob/0e030cb39554e9f09e4460d1da4e7654fe7456de/sidekick/examples/src/connection.ts#L68-L109
    // document has a button with id=connect
    // this is the function that is triggered when the button is clicked
    // you want your user to do the detect sequence first
    async function connect (logger: sk.Logger) : Promise<void> {
    let h4 = document.getElementById("connected")!;
    let pre = document.getElementById("connect-info")!;

    h4.innerHTML = 'connecting...';
    h4.style.color = 'GoldenRod';

    // try to connect to the wallet
    // will fail on timeout error
    let maybe_wallet_info = await sk.connect(
    'ske-connect-1',
    {name: 'sidekick examples',
    version: 1},
    sk.TIMEOUT_DEF_CONNECT_MS,
    "failed to connect to wallet",
    logger
    );

    console.log(maybe_wallet_info);

    // ok means wallet was connected
    if (maybe_wallet_info.ok)
    {
    h4.innerHTML = "connected";
    h4.style.color = "green";
    pre.innerHTML = JSON.stringify(maybe_wallet_info.result, undefined, 4);
    }
    else
    {
    h4.innerHTML = "error";
    h4.style.color = "crimson";
    pre.innerHTML = JSON.stringify(maybe_wallet_info.error, undefined, 4);
    }
    }

    let logger = sk.cl();
    document.getElementById('connect')!.onclick = function() { connect(logger); };

    Parameters

    Returns Promise<Safe<Result_W2A_connection_open, RpcError | SkTimeoutError>>

Generated using TypeDoc