Giter Site home page Giter Site logo

myalgo-connect's Introduction

Wallet My Algo

myalgo-logo

Overview

Wallet My Algo is a Javascript library developed by Rand Labs to securely sign transactions with My Algo

Installation

The library can be installed via npm:

npm i @randlabs/myalgo-connect

API Usage

Connect to My Algo

import MyAlgo from '@randlabs/myalgo-connect';


const myAlgoWallet = new MyAlgo();

/*Warning: Browser will block pop-up if user doesn't trigger myAlgoWallet.connect() with a button interation */
const connectToMyAlgo = async() => {
  try {
    const accounts = await myAlgoWallet.connect();

    const addresses = accounts.map(account => account.address);
    
  } catch (err) {
    console.error(err);
  }
}

Payment transaction (with note)

import algosdk from 'algosdk';

const algodClient = new algosdk.Algodv2(algodToken, algodServer, algodPort);


(async () => {
  try {

    let txn = await algodClient.getTransactionParams().do();
      
    txn = {
      ...txn,
      fee: 1000,
      flatFee: true,
      type: 'pay',
      from: addresses[0],
      to:  '...',
      amount: 1000000,
      note: new Uint8Array(Buffer.from('Hello World'))
    };
  
    let signedTxn = await myAlgoWallet.signTransaction(txn);
    console.log(signedTxn.txID);
  
    await algodClient.sendRawTransaction(signedTxn.blob).do();

  
  } catch(err) {
    console.error(err); 
  }
})();

Payment transaction (with closeTo)

import algosdk from 'algosdk';

const algodClient = new algosdk.Algodv2(algodToken, algodServer, algodPort);


(async () => {
  try {
  
    let txn = await algodClient.getTransactionParams().do();
      
    txn = {
      ...txn,
      fee: 1000,
      flatFee: true,
      type: 'pay',
      from: addresses[0],
      to:  '...',
      amount: 1000000,
      closeRemainderTo: '...' // closeTo address
    };
  
    let signedTxn = await myAlgoWallet.signTransaction(txn);
    console.log(signedTxn.txID);
  
    await algodClient.sendRawTransaction(signedTxn.blob).do();

  
  } catch(err) {
    console.error(err); 
  }
})();

Payment transaction (with rekey)

import algosdk from 'algosdk';

const algodClient = new algosdk.Algodv2(algodToken, algodServer, algodPort);


(async () => {
  try {

    let txn = await algodClient.getTransactionParams().do();
      
    txn = {
      ...txn,
      fee: 1000,
      flatFee: true,
      type: 'pay',
      from: addresses[0],
      to:  addresses[0],
      amount: 0,
      reKeyTo: '...' //Authorized address for signing
    };
  
    let signedTxn = await myAlgoWallet.signTransaction(txn);
    console.log(signedTxn.txID);
  
    await algodClient.sendRawTransaction(signedTxn.blob).do();

  
  } catch(err) {
    console.error(err); 
  }
})();

Payment transaction (with signer)

import algosdk from 'algosdk';

const algodClient = new algosdk.Algodv2(algodToken, algodServer, algodPort);


(async () => {
  try {

    let txn = await algodClient.getTransactionParams().do();
      
    txn = {
      ...txn,
      fee: 1000,
      flatFee: true,
      type: 'pay',
      from: '...', // Rekeyed address
      to:  '...',
      amount: 1000000,
      signer: addresses[0] // Authorized adresses for signing
    };
  
    let signedTxn = await myAlgoWallet.signTransaction(txn);
    console.log(signedTxn.txID);
  
    await algodClient.sendRawTransaction(signedTxn.blob).do();

  
  } catch(err) {
    console.error(err); 
  }
})();

Asset transfer (with note)

import algosdk from 'algosdk';

const algodClient = new algosdk.Algodv2(algodToken, algodServer, algodPort);


(async () => {
  try {

    let txn = await algodClient.getTransactionParams().do();
      
    txn = {
      ...txn,
      fee: 1000,
      flatFee: true,
      type: 'axfer',
      assetIndex: 123,
      from: addresses[0],
      to:  '...',
      amount: 1000000,
      note: new Uint8Array(Buffer.from('Hello World'))
    };
  
    let signedTxn = await myAlgoWallet.signTransaction(txn);
    console.log(signedTxn.txID);
  
    await algodClient.sendRawTransaction(signedTxn.blob).do();

  
  } catch(err) {
    console.error(err); 
  }
})();

Asset transfer (with closeTo)

import algosdk from 'algosdk';

const algodClient = new algosdk.Algodv2(algodToken, algodServer, algodPort);


(async () => {
  try {
  
    let txn = await algodClient.getTransactionParams().do();
      
    txn = {
      ...txn,
      fee: 1000,
      flatFee: true,
      type: 'axfer',
      assetIndex: 123,
      from: addresses[0],
      to:  '...',
      amount: 1000000,
      closeRemainderTo: '...'
    };
  
    let signedTxn = await myAlgoWallet.signTransaction(txn);
    console.log(signedTxn.txID);
  
    await algodClient.sendRawTransaction(signedTxn.blob).do();

  
  } catch(err) {
    console.error(err); 
  }
})();

Asset freeze (with note)

import algosdk from 'algosdk';

const algodClient = new algosdk.Algodv2(algodToken, algodServer, algodPort);


(async () => {
  try {

    let txn = await algodClient.getTransactionParams().do();
      
    txn = {
      ...txn,
      fee: 1000,
      flatFee: true,
      type: 'afrz',
      from: addresses[0],
      assetIndex: 123,
      freezeAccount:  '...', // Address to freeze
      note: new Uint8Array(Buffer.from('Hello World')),
      freezeState: true
    };
  
    let signedTxn = await myAlgoWallet.signTransaction(txn);
    console.log(signedTxn.txID);
  
    await algodClient.sendRawTransaction(signedTxn.blob).do();

  
  } catch(err) {
    console.error(err); 
  }
})();

Asset config (create ASA)

import algosdk from 'algosdk';

const algodClient = new algosdk.Algodv2(algodToken, algodServer, algodPort);


(async () => {
  try {

    let txn = await algodClient.getTransactionParams().do();
      
    txn = {
      ...txn,
      fee: 1000,
      flatFee: true,
      type: 'acfg',
      from: addresses[0],
      assetName: 'My New Coin',
      assetUnitName: 'MNC',
      assetDecimals: 2,
      assetTotal: 50000000,
      assetURL: 'developer.algorand.org',
      assetFreeze: '...',
      assetManager: '...',
      assetReserve: '...',
      assetDefaultFrozen: false
    };
  
    let signedTxn = await myAlgoWallet.signTransaction(txn);
    console.log(signedTxn.txID);
  
    await algodClient.sendRawTransaction(signedTxn.blob).do();

  
  } catch(err) {
    console.error(err); 
  }
})();

Asset config (update ASA)

import algosdk from 'algosdk';

const algodClient = new algosdk.Algodv2(algodToken, algodServer, algodPort);


(async () => {
  try {

    let txn = await algodClient.getTransactionParams().do();
      
    txn = {
      ...txn,
      fee: 1000,
      flatFee: true,
      type: 'acfg',
      from: addresses[0],
      assetIndex: 123,
      assetFreeze: '...',
      assetManager: '...',
      assetReserve: '...',
    };
  
    let signedTxn = await myAlgoWallet.signTransaction(txn);
    console.log(signedTxn.txID);
  
    await algodClient.sendRawTransaction(signedTxn.blob).do();

  
  } catch(err) {
    console.error(err); 
  }
})();

Asset config (remove ASA)

import algosdk from 'algosdk';

const algodClient = new algosdk.Algodv2(algodToken, algodServer, algodPort);


(async () => {
  try {

    let txn = await algodClient.getTransactionParams().do();
      
    txn = {
      ...txn,
      fee: 1000,
      flatFee: true,
      type: 'acfg',
      from: addresses[0],
      assetIndex: 123,
    };
  
    let signedTxn = await myAlgoWallet.signTransaction(txn);
    console.log(signedTxn.txID);
  
    await algodClient.sendRawTransaction(signedTxn.blob).do();

  
  } catch(err) {
    console.error(err); 
  }
})();

Keyreg

import algosdk from 'algosdk';

const algodClient = new algosdk.Algodv2(algodToken, algodServer, algodPort);


(async () => {
  try {

    let txn = await algodClient.getTransactionParams().do();
      
    txn = {
      ...txn,
      fee: 2000,
      flatFee: true,
      type: 'keyreg',
      from: addresses[0],
      voteKey: 'eXq34wzh2UIxCZaI1leALKyAvSz/+XOe0wqdHagM+bw=',
      selectionKey: 'X84ReKTmp+yfgmMCbbokVqeFFFrKQeFZKEXG89SXwm4=',
      voteFirst: 6000000,
      voteLast: 9000000,
      voteKeyDilution: 1730,
    };
  
    let signedTxn = await myAlgoWallet.signTransaction(txn);
    console.log(signedTxn.txID);
  
    await algodClient.sendRawTransaction(signedTxn.blob).do();

  
  } catch(err) {
    console.error(err); 
  }
})();

Sign Teal

import algosdk from 'algosdk';

const algodClient = new algosdk.Algodv2(algodToken, algodServer, algodPort);


(async () => {
  try {

    let txn = await algodClient.getTransactionParams().do();
      
    txn = {
      ...txn,
      fee: 1000,
      flatFee: true,
      type: 'pay',
      from: '...',
      to: '...',
      amount: 10000,
    };

    let program = new Uint8Array(Buffer.from('ASABASI=', "base64")); // int 1

    let lsig = algosdk.makeLogicSig(program);
    lsig.sig = await myAlgoWallet.signLogicSig(program, addresses[0]);
  
    let signedTxn = algosdk.signLogicSigTransaction(txn, lsig);
  
    await algodClient.sendRawTransaction(signedTxn.blob).do();

  
  } catch(err) {
    console.error(err); 
  }
})();

Copyright and License

See LICENSE file.

myalgo-connect's People

Contributors

alex99y avatar itslesther avatar ignacio-87 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.