This repository has been archived by the owner on Nov 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Print SwapRequest msg with --local flag * Add switch in case of --local for prompt output amount * Add swap accept command * Types * Add swap accept and swap complete commands and test local flow e2e * Readme
- Loading branch information
Showing
9 changed files
with
368 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
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,88 @@ | ||
import { info, log, error, success } from '../logger'; | ||
import State from '../state'; | ||
import { WalletInterface, fromWIF, fetchUtxos } from '../wallet'; | ||
import { decrypt } from '../crypto'; | ||
import { makeid } from '../helpers'; | ||
const state = new State(); | ||
|
||
const { Confirm, Password } = require('enquirer'); | ||
|
||
const confirm = new Confirm({ | ||
name: 'question', | ||
message: 'Do you accept the proposed terms?' | ||
}); | ||
const password = new Password({ | ||
type: 'password', | ||
name: 'key', | ||
message: 'Type your password' | ||
}); | ||
|
||
|
||
export default function (message: string): void { | ||
info('=========*** Swap ***==========\n'); | ||
|
||
const { wallet, network } = state.get(); | ||
|
||
if (!wallet.selected) | ||
return error('A wallet is required. Create or restoste with wallet command'); | ||
|
||
let json: any | ||
try { | ||
json = JSON.parse(message); | ||
} catch (ignore) { | ||
return error('Not a valid SwapRequest message'); | ||
} | ||
|
||
log(JSON.stringify(json, undefined, 2)); | ||
log(); | ||
|
||
const psbtBase64 = json.transaction; | ||
|
||
|
||
let walletInstance: WalletInterface; | ||
|
||
confirm.run().then((keepGoing: Boolean) => { | ||
if (!keepGoing) | ||
throw "Canceled"; | ||
|
||
const execute = wallet.keystore.type === "encrypted" ? | ||
() => password.run() : | ||
() => Promise.resolve(wallet.keystore.value); | ||
|
||
return execute() | ||
}).then((passwordOrWif: string) => { | ||
const wif = wallet.keystore.type === "encrypted" ? | ||
decrypt(wallet.keystore.value, passwordOrWif) : | ||
passwordOrWif; | ||
|
||
walletInstance = fromWIF(wif, network.chain); | ||
|
||
return fetchUtxos(walletInstance.address, network.explorer) | ||
}).then((utxos: Array<any>) => { | ||
// Add inputs and putputs to psbt | ||
|
||
const unsignedPsbt = walletInstance.updateTx( | ||
psbtBase64, | ||
utxos, | ||
json.amount_r, | ||
json.amount_p, | ||
json.asset_r, | ||
json.asset_p | ||
); | ||
|
||
log("\nSigning with private key..."); | ||
return walletInstance.sign(unsignedPsbt); | ||
}).then((signedPsbt: string) => { | ||
success("\n√ Done\n"); | ||
|
||
const TradeReply = { | ||
SwapAccept: { | ||
id: makeid(8), | ||
request_id: json.id, | ||
transaction: signedPsbt | ||
} | ||
}; | ||
|
||
success(`\nSwapAccept message\n\n${JSON.stringify(TradeReply.SwapAccept)}`); | ||
}).catch(error) | ||
} |
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.