0.2.10 ā€¢ Published 1 year ago

@thencc/any-wallet v0.2.10

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

āš ļø this is beta software: no significant security audits have yet been performed so use at your own discretion.

@thencc/any-wallet

a wallet connection handler for web3 dapps npm.io

šŸŒ live-demo | example dapp source code

šŸ”— npm šŸ”— jsdelivr

about

any-wallet provides a common interface layer between the dapp and the wallet so that dapp developers can treat any connected wallet the same way. any-wallet does not load a wallet's client library until it needs to / when the user starts interacting with that wallet. for optimal user experience across page loads, any-wallet saves the last connected account(s) to localstorage (dont worry, no secrets are saved, just the user's public address and a reference to which wallet they connected with).

currently supported chains + wallets:

  • algorand - āœ… Pera - āœ… Inkey - āœ… MyAlgo - āœ… AlgoSigner - āœ… Exodus - āœ… Defly - āœ…āš ļø Mnemonic (not secure client-side)

usage

install

npm i @thencc/any-wallet
# or
pnpm add @thencc/any-wallet

import + construct

import {
	AnyWalletState
} from '@thencc/any-wallet';

const awState = new AnyWalletState();

connecting

// connect pera
await awState.connectWallet('pera');

// or, connect inkey
const accts = await awState.connectWallet('inkey');

// now...
// accts[0] == awState.activeAccount;

signing transactions

import { 
	AnyWalletState
} from '@thencc/any-wallet';

const awState = new AnyWalletState();

const txn1 = await algonaut.atomicSendAlgo({
	amount: 1000,
	to: receiverAddr,
	from: senderAddr // .from needed IF not authenticated
});

const assetId = 10458941;
const txn2 = await algonaut.atomicOptInAsset(assetId);

// prompts user for approval using current activeWallet as chosen by the user
const signedTxns = await awState.signTransactions([txn1, txn2]); // throws if user rejects txn
// "signedTxns" is an array of Uint8Array's (raw algorand txn bytes)

// now do what you want w the signed txns, like submit them to the network
const txnGroup = await algonaut.algodClient.sendRawTransaction(signedTxns).do();
console.log('txn id: ', txnGroup.txId);

state

the AnyWalletState instance is enhanced with reactive proxy fields that update as you connect client wallets, sign transactions and change connected accounts.

a few helpful things inside AnyWalletState

import { 
	AnyWalletState
} from '@thencc/any-wallet';

const awState = new AnyWalletState();

// access the current address
console.log(awState.activeAddress);

// boolean for if any of the wallets are doing some txn signing
console.log(awState.isSigning);

recall previous state (localstorage):

// even after a page reload, the dapp can access the last connected account(s) by initializing with the same storageKey, which uses localstorage (by default) to remember relavent state fields. 
import { 
	AnyWalletState
} from '@thencc/any-wallet';

const awStateA = new AnyWalletState({
	storageKey: 'state1'
});
const awStateB = new AnyWalletState({
	storageKey: 'state1'
});

// now... 
/// awStateA.activeAddress == awStateB.activeAddress

using the mnemonic wallet (for dev!)

import {
	AnyWalletState
} from '@thencc/any-wallet';

const awState = new AnyWalletState();

let accts = await awState.connectWallet('mnemonic', 'uniform eager witness salt evolve pole envelope name supreme column begin venue decline blast finger grunt avoid people crawl during street priority diary ability lend');

vue

when used with vue, you might need to re-render a component's <template> computations if you are displaying information stored in awState because these deep object changes arent tracked by the vue runtime. for this, there is the helpful method subscribeToAccountChanges which allows you to define a callback when the active account changes.

Errors in builds have been observed if the entire awState is included in a component's data

This note on reactivity applies to most frontend frameworks with components in some way: ā€¢ this.requestUpdate() in lit ā€¢ this.forceUpdate() in react.

import {
	AnyWalletState
} from '@thencc/any-wallet';

const awState = new AnyWalletState();

// then in the vue component's mounted or setup hook you would add:
export default defineComponent({
	mounted() {
		let unsubscribe = awState.subscribeToAccountChanges(
			() => this.$forceUpdate()
		);

		// unsubscribe(); // call to stop callbacks
	},
});

Notes

  • this lib handles txn signing and is supposed to be used in conjuction with a lib that does txn construction + submitting (we recommend @thencc/algonautjs though algosdk works as well)
  • some wallets (like defly/pera) require that the mobile app is set to the same net (testnet/mainnet) as requested for signing by the dapp

Attributions

big thanks to txnlab/use-wallet for developing the react groundwork for this pkg

0.2.10

1 year ago

0.1.96

1 year ago

0.1.97

1 year ago

0.1.91

1 year ago

0.1.92

1 year ago

0.1.93

1 year ago

0.1.94

1 year ago

0.1.95

1 year ago

0.1.89

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.2.7

1 year ago

0.2.6

1 year ago

0.2.9

1 year ago

0.2.8

1 year ago

0.2.3

1 year ago

0.2.2

1 year ago

0.2.5

1 year ago

0.2.4

1 year ago

0.1.86

1 year ago

0.1.87

1 year ago

0.1.85

1 year ago

0.1.82

1 year ago

0.1.83

1 year ago

0.1.84

1 year ago

0.1.80

1 year ago

0.1.81

1 year ago

0.1.79

1 year ago

0.1.78

1 year ago

0.1.52

2 years ago

0.1.53

2 years ago

0.1.54

2 years ago

0.1.55

2 years ago

0.1.56

2 years ago

0.1.57

2 years ago

0.1.58

2 years ago

0.1.59

2 years ago

0.1.50

2 years ago

0.1.51

2 years ago

0.1.49

2 years ago

0.1.41

2 years ago

0.1.42

2 years ago

0.1.43

2 years ago

0.1.44

2 years ago

0.1.45

2 years ago

0.1.46

2 years ago

0.1.47

2 years ago

0.1.48

2 years ago

0.1.40

2 years ago

0.1.38

2 years ago

0.1.39

2 years ago

0.1.74

1 year ago

0.1.30

2 years ago

0.1.75

1 year ago

0.1.31

2 years ago

0.1.76

1 year ago

0.1.32

2 years ago

0.1.77

1 year ago

0.1.33

2 years ago

0.1.34

2 years ago

0.1.35

2 years ago

0.1.36

2 years ago

0.1.37

2 years ago

0.1.70

1 year ago

0.1.71

1 year ago

0.1.72

1 year ago

0.1.73

1 year ago

0.1.27

2 years ago

0.1.28

2 years ago

0.1.29

2 years ago

0.1.63

1 year ago

0.1.64

1 year ago

0.1.65

1 year ago

0.1.66

1 year ago

0.1.22

2 years ago

0.1.67

1 year ago

0.1.23

2 years ago

0.1.68

1 year ago

0.1.24

2 years ago

0.1.69

1 year ago

0.1.25

2 years ago

0.1.26

2 years ago

0.1.60

2 years ago

0.1.61

2 years ago

0.1.62

2 years ago

0.1.20

2 years ago

0.1.21

2 years ago

0.1.10

2 years ago

0.1.11

2 years ago

0.1.12

2 years ago

0.1.13

2 years ago

0.1.14

2 years ago

0.1.15

2 years ago

0.1.16

2 years ago

0.1.17

2 years ago

0.1.18

2 years ago

0.1.19

2 years ago

0.1.9

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago