Skip to content

Commit

Permalink
Merge pull request #27 from Dartroom/22-suport-for-defly-connect
Browse files Browse the repository at this point in the history
22 suport for defly connect
  • Loading branch information
allandiamante authored Feb 3, 2024
2 parents 3bd1b41 + 30f599a commit 592d37c
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 51 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
/node_modules
.env
.npmrc
/algoSigner
/algoSigner
.vscode
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ A package that aggregates multiple Algorand signing options into one interface.

Integrates the following wallet apps:
- [AlgoSigner](https://github.com/PureStake/algosigner)
- [DeflyWallet](https://github.com/blockshake-io/defly-connect)
- [MyAlgo](https://github.com/randlabs/myalgo-connect)
- [PeraWallet](https://github.com/perawallet/connect)

Expand Down
45 changes: 43 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@dartroom/signer",
"version": "0.3.4-alpha",
"version": "0.3.5-alpha",

"description": "A package that aggregates multiple Algorand signing options into one interface. Handles signing and address/login management.",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down Expand Up @@ -31,6 +32,7 @@
"typescript": "^4.8.2"
},
"dependencies": {
"@blockshake/defly-connect": "^1.1.6",
"@perawallet/connect": "^1.2.1",
"@randlabs/myalgo-connect": "^1.4.0",
"algosdk": "^2.1.0",
Expand Down
6 changes: 6 additions & 0 deletions src/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Provider, Wallets, Addresses } from './main'
import connectMyAlgo from './myAlgo/connect'
import connectAlgoSigner from './algoSigner/connect'
import connectPera from './pera/connect'
import connectDefly from './defly/connect'


export interface ConnectSettings {
wallet: Wallets
Expand All @@ -25,6 +27,10 @@ export default async function connect (provider: Provider, { wallet }: ConnectSe
newAddresses = await connectPera(provider)
clearWallet(provider, "PeraWallet")
break
case "DeflyWallet":
newAddresses = await connectDefly(provider)
clearWallet(provider, "DeflyWallet")
break
case "AlgoSigner":
newAddresses = await connectAlgoSigner(provider)
clearWallet(provider, "AlgoSigner")
Expand Down
35 changes: 35 additions & 0 deletions src/defly/connect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Provider, Wallets, Address } from '../main'

function clearWallet(addresses: Provider['addresses'], w: Wallets, address?: string) {
if (address) {
addresses = addresses.filter(a => (a.wallet !== w) || (a.address !== address))
} else {
addresses = addresses.filter(a => a.wallet !== w)
}
}

export default async function connect ({ defly, addresses }: Provider): Promise<Array<Address>> {

let accounts: Array<string>

try {
accounts = await defly.connect()
} catch (err) {
throw new Error('Failed to connect with the Defly Wallet: '+ err)
}

defly.connector?.on('disconnect', () => {
clearWallet(addresses, "DeflyWallet")
})

if (accounts && accounts.length > 0) {
return accounts.map((address) => {
return {
address: address,
wallet: "DeflyWallet"
}
})
} else {
throw new Error('Either the user shared no accounts, or the Defly wallet did not return any.')
}
}
12 changes: 12 additions & 0 deletions src/defly/disconnect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Provider } from '../main'

export default async function disconnect ({ defly }: Provider) {

if (defly.isConnected) {
try {
await defly.disconnect()
} catch (err) {
throw new Error('Failed to disconnect from the current account: ' + err)
}
}
}
94 changes: 94 additions & 0 deletions src/defly/signTransactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { Provider } from '../main'
import { Txn, TxnArray } from '../signTransactions'
import { decodeUnsignedTransaction, Transaction } from 'algosdk'

export default async function sign<T extends Txn>({ defly }: Provider, txns: Array<Array<T>>): Promise<Array<Array<T>>> {

const unsignedTxns: Array<T> = []
let logicTxns: Array<Array<boolean>> = []
let formatedTxns: Array<Array<{ txn: Transaction, signers?: Array<string> }>> = []

for (let g = 0; g < txns.length; g++) {

const txnArray = txns[g]

if (!txnArray) {
throw new Error('Failed to parse transaction array.')
}

for (let t = 0; t < txnArray.length; t++) {

const txn = txnArray[t]

if (!txn) {
throw new Error('Failed to parse transaction array.')
}

unsignedTxns.push(txn)
}
}

formatedTxns = txns.map((txnGroup) => {
return txnGroup.map((txn) => {
return {
txn: decodeUnsignedTransaction(txn.blob),
signers: txn.signers
}
})
})

logicTxns = txns.map((txnGroup) => {
return txnGroup.map((txn) => txn.signers.length === 0)
})

let signedTxns: Array<Uint8Array> = []
let formated: Array<Array<T>> = []

await defly.reconnectSession()
signedTxns = await defly.signTransaction(formatedTxns)

if (signedTxns && signedTxns.length > 0) {
formated = txns.map((txnArray, i) => {
const unsignedArray = txns[i]
const logicsigs = logicTxns[i]

if (!logicsigs) {
throw new Error('Failed to check for logic sigs.')
}

const signedArray = logicsigs.map((logic) => {
if (logic) {
return null
} else {
return signedTxns.splice(0, 1)[0]
}
})

if (!unsignedArray) {
throw new Error('Failed to parse transaction array.')
}

return signedArray.map((signedTxn, index) => {
const usignedTxn = unsignedArray[index]

if (!usignedTxn) {
throw new Error('Failed to parse transaction array.')
}

if (!signedTxn) {
return usignedTxn
}

return {
...usignedTxn,
blob: signedTxn
}
})
})

} else {
throw new Error('The Defly wallet did not return any transactions.')
}

return formated
}
5 changes: 5 additions & 0 deletions src/disconnect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Provider, Wallets } from './main'
import disconnectPera from './pera/disconnect'
import disconnectDefly from './defly/disconnect'
import disconnectExodus from './exodus/disconnect'

export interface DisconnectSettings {
Expand All @@ -25,6 +26,10 @@ export default async function disconnect (provider: Provider, { wallet, address
await disconnectPera(provider)
clearWallet(provider, "PeraWallet")
break
case "DeflyWallet":
await disconnectDefly(provider)
clearWallet(provider, "DeflyWallet")
break
case "AlgoSigner":
clearWallet(provider, "AlgoSigner", address)
break
Expand Down
Loading

0 comments on commit 592d37c

Please sign in to comment.