1.0.0 • Published 4 years ago

@walletconnect/node v1.0.0

Weekly downloads
49
License
LGPL-3.0
Repository
github
Last release
4 years ago

WalletConnect NodeJS Client

NodeJS Client for WalletConnect

For more details, read the documentation

Install

yarn add @walletconnect/node

# OR

npm install --save @walletconnect/node @walletconnect/qrcode-modal

Initiate Connection

import NodeWalletConnect from "@walletconnect/node";
import WalletConnectQRCodeModal from "@walletconnect/qrcode-modal";

// Create WalletConnector
const connector = new NodeWalletConnect(
  {
    bridge: "https://bridge.walletconnect.org", // Required
  },
  {
    clientMeta: {
      description: "WalletConnect NodeJS Client",
      url: "https://nodejs.org/en/",
      icons: ["https://nodejs.org/static/images/logo.svg"],
      name: "WalletConnect",
    },
  },
);

// Check if connection is already established
if (!connector.connected) {
  // create new session
  connector.createSession().then(() => {
    // get uri for QR Code modal
    const uri = connector.uri;
    // display QR Code modal
    WalletConnectQRCodeModal.open(uri, () => {
      console.log("QR Code Modal closed");
    });
  });
}

// Subscribe to connection events
connector.on("connect", (error, payload) => {
  if (error) {
    throw error;
  }

  // Close QR Code Modal
  WalletConnectQRCodeModal.close();

  // Get provided accounts and chainId
  const { accounts, chainId } = payload.params[0];
});

connector.on("session_update", (error, payload) => {
  if (error) {
    throw error;
  }

  // Get updated accounts and chainId
  const { accounts, chainId } = payload.params[0];
});

connector.on("disconnect", (error, payload) => {
  if (error) {
    throw error;
  }

  // Delete connector
});

Send Transaction (eth_sendTransaction)

// Draft transaction
const tx = {
  from: "0xbc28Ea04101F03aA7a94C1379bc3AB32E65e62d3", // Required
  to: "0x89D24A7b4cCB1b6fAA2625Fe562bDd9A23260359", // Required (for non contract deployments)
  data: "0x", // Required
  gasPrice: "0x02540be400", // Optional
  gasLimit: "0x9c40", // Optional
  value: "0x00", // Optional
  nonce: "0x0114", // Optional
};

// Send transaction
connector
  .sendTransaction(tx)
  .then(result => {
    // Returns transaction id (hash)
    console.log(result);
  })
  .catch(error => {
    // Error returned when rejected
    console.error(error);
  });

Sign Transaction (eth_signTransaction)

// Draft transaction
const tx = {
  from: "0xbc28Ea04101F03aA7a94C1379bc3AB32E65e62d3", // Required
  to: "0x89D24A7b4cCB1b6fAA2625Fe562bDd9A23260359", // Required (for non contract deployments)
  data: "0x", // Required
  gasPrice: "0x02540be400", // Optional
  gasLimit: "0x9c40", // Optional
  value: "0x00", // Optional
  nonce: "0x0114", // Optional
};

// Sign transaction
connector
  .signTransaction(tx)
  .then(result => {
    // Returns signed transaction
    console.log(result);
  })
  .catch(error => {
    // Error returned when rejected
    console.error(error);
  });

Sign Personal Message (personal_sign)

// Draft Message Parameters
const message = "My email is john@doe.com - 1537836206101"

const msgParams = [
  convertUtf8ToHex(message)                                                 // Required
  "0xbc28ea04101f03ea7a94c1379bc3ab32e65e62d3",                             // Required
];


// Sign personal message
connector
  .signPersonalMessage(msgParams)
  .then((result) => {
    // Returns signature.
    console.log(result)
  })
  .catch(error => {
    // Error returned when rejected
    console.error(error);
  })

Sign Message (eth_sign)

// Draft Message Parameters
const message = "My email is john@doe.com - 1537836206101";

const msgParams = [
  "0xbc28ea04101f03ea7a94c1379bc3ab32e65e62d3",                            // Required
  keccak256("\x19Ethereum Signed Message:\n" + len(message) + message))    // Required
];


// Sign message
connector
  .signMessage(msgParams)
  .then((result) => {
    // Returns signature.
    console.log(result)
  })
  .catch(error => {
    // Error returned when rejected
    console.error(error);
  })

Sign Typed Data (eth_signTypedData)

// Draft Message Parameters
const typedData = {
  types: {
    EIP712Domain: [
      { name: "name", type: "string" },
      { name: "version", type: "string" },
      { name: "chainId", type: "uint256" },
      { name: "verifyingContract", type: "address" },
    ],
    Person: [
      { name: "name", type: "string" },
      { name: "account", type: "address" },
    ],
    Mail: [
      { name: "from", type: "Person" },
      { name: "to", type: "Person" },
      { name: "contents", type: "string" },
    ],
  },
  primaryType: "Mail",
  domain: {
    name: "Example Dapp",
    version: "1.0.0-beta",
    chainId: 1,
    verifyingContract: "0x0000000000000000000000000000000000000000",
  },
  message: {
    from: {
      name: "Alice",
      account: "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
    },
    to: {
      name: "Bob",
      account: "0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
    },
    contents: "Hey, Bob!",
  },
};

const msgParams = [
  "0xbc28ea04101f03ea7a94c1379bc3ab32e65e62d3", // Required
  typedData, // Required
];

// Sign Typed Data
connector
  .signTypedData(msgParams)
  .then(result => {
    // Returns signature.
    console.log(result);
  })
  .catch(error => {
    // Error returned when rejected
    console.error(error);
  });

Send Custom Request

// Draft Custom Request
const customRequest = {
  id: 1337,
  jsonrpc: "2.0",
  method: "eth_signTransaction",
  params: [
    {
      from: "0xbc28Ea04101F03aA7a94C1379bc3AB32E65e62d3",
      to: "0x89D24A7b4cCB1b6fAA2625Fe562bDd9A23260359",
      data: "0x",
      gasPrice: "0x02540be400",
      gasLimit: "0x9c40",
      value: "0x00",
      nonce: "0x0114",
    },
  ],
};

// Send Custom Request
connector
  .sendCustomRequest(customRequest)
  .then(result => {
    // Returns request result
    console.log(result);
  })
  .catch(error => {
    // Error returned when rejected
    console.error(error);
  });
1.0.0

4 years ago

1.0.0-rc.4

4 years ago

1.0.0-rc.3

4 years ago

1.0.0-rc.1

4 years ago

1.0.0-rc.2

4 years ago

1.0.0-rc.0

4 years ago

1.0.0-beta.103

4 years ago

1.0.0-beta.102

4 years ago

1.0.0-beta.101

4 years ago

1.0.0-beta.100

4 years ago

1.0.0-beta.99

4 years ago

1.0.0-beta.98

4 years ago

1.0.0-beta.97

5 years ago

1.0.0-beta.96

5 years ago

1.0.0-beta.95

5 years ago

1.0.0-beta.94

5 years ago

1.0.0-beta.93

5 years ago

1.0.0-beta.91

5 years ago

1.0.0-beta.92

5 years ago

1.0.0-beta.90

5 years ago

1.0.0-beta.88

5 years ago

1.0.0-beta.89

5 years ago

1.0.0-beta.86

5 years ago

1.0.0-beta.87

5 years ago

1.0.0-beta.85

5 years ago

1.0.0-beta.84

5 years ago

1.0.0-beta.83

5 years ago

1.0.0-beta.82

5 years ago

1.0.0-beta.81

5 years ago

1.0.0-beta.80

5 years ago

1.0.0-beta.79

5 years ago

1.0.0-beta.77

5 years ago

1.0.0-beta.76

5 years ago

1.0.0-beta.75

5 years ago

1.0.0-beta.73

5 years ago

1.0.0-beta.74

5 years ago

1.0.0-beta.71

5 years ago

1.0.0-beta.72

5 years ago

1.0.0-beta.70

5 years ago

1.0.0-beta.69

5 years ago

1.0.0-beta.68

5 years ago

1.0.0-beta.67

5 years ago

1.0.0-beta.66

5 years ago

1.0.0-beta.65

5 years ago

1.0.0-beta.64

5 years ago

1.0.0-beta.63

5 years ago

1.0.0-beta.62

5 years ago

1.0.0-beta.61

5 years ago

1.0.0-beta.60

5 years ago

1.0.0-beta.59

5 years ago

1.0.0-beta.58

5 years ago

1.0.0-beta.57

5 years ago

1.0.0-beta.56

5 years ago

1.0.0-beta.55

5 years ago

1.0.0-beta.54

5 years ago

1.0.0-beta.53

5 years ago

1.0.0-beta.52

5 years ago

1.0.0-beta.50

5 years ago

1.0.0-beta.48

5 years ago

1.0.0-beta.49

5 years ago

1.0.0-beta.47

5 years ago

1.0.0-beta.46

5 years ago

1.0.0-beta.45

5 years ago

1.0.0-beta.44

5 years ago

1.0.0-beta.43

5 years ago