-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from Dartroom/22-suport-for-defly-connect
22 suport for defly connect
- Loading branch information
Showing
11 changed files
with
226 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
/node_modules | ||
.env | ||
.npmrc | ||
/algoSigner | ||
/algoSigner | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.