Created
August 2, 2020 22:50
-
-
Save unwriter/a300ff33f313fba6ded2619b549fd5ec to your computer and use it in GitHub Desktop.
baemail demo
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
<html> | |
<body> | |
<script src="https://www.moneybutton.com/moneybutton.js"></script> | |
<script src='https://unpkg.com/bsv@1.0.0/bsv.min.js'></script> | |
<script src='https://unpkg.com/bsv@1.0.0/bsv-mnemonic.min.js'></script> | |
<div id='button'></div> | |
<script> | |
const Baemail = {}; | |
// visible moneybutton | |
const vmb = (el, cryptoOperations) => { | |
return new Promise((resolve, reject) => { | |
moneyButton.render(el, { | |
label: "sign", | |
cryptoOperations: cryptoOperations, | |
onCryptoOperations: (cryptoOperations) => { | |
resolve({ cryptoOperations: cryptoOperations }) | |
} | |
}) | |
}) | |
} | |
Baemail.createMessage = async (imb, to, fromName, fromPaymail, fromPki, subject, html, amount) => { | |
try { | |
console.log("amount = ", amount) | |
const numount = Number(amount) | |
const strimount = String(amount) | |
const baemail = {body: {time: Date.now(), blocks: html, version: '3.0.0'}} | |
const dataToEncrypt = JSON.stringify(baemail) | |
const baemailData = JSON.stringify({ | |
summary: 'Baemail', | |
amountUsd: numount, | |
to: to, | |
cc: [], | |
subject: subject, | |
salesPitch: '', | |
from: { | |
name: fromName, | |
primaryPaymail: fromPaymail, | |
pki: fromPki | |
} | |
}) | |
const biscuit = bsv.crypto.Hash.sha256(bsv.deps.Buffer.from('1BAESxZMweg2mG4FG2DEZmB1Ury2ruAr9K' + baemailData)).toString('hex') | |
let cryptoOperations = [ | |
{ | |
name: 'mySignature', | |
method: 'sign', | |
data: biscuit, | |
dataEncoding: 'utf8', | |
key: 'identity', | |
algorithm: 'bitcoin-signed-message' | |
} | |
] | |
cryptoOperations.push({ | |
name: 'baemailData', | |
method: 'encrypt', | |
paymail: 'baemail@moneybutton.com', | |
data: baemailData, | |
dataEncoding: 'utf8', | |
key: 'identity', | |
algorithm: 'electrum-ecies' | |
}) | |
cryptoOperations.push({ | |
name: 'privateBaemail', | |
method: 'encrypt', | |
paymail: fromPaymail, | |
data: dataToEncrypt, | |
dataEncoding: 'utf8', | |
key: 'identity', | |
algorithm: 'electrum-ecies' | |
}) | |
cryptoOperations.push({ | |
name: 'privateSent', | |
method: 'encrypt', | |
paymail: to, | |
data: dataToEncrypt, | |
dataEncoding: 'utf8', | |
key: 'identity', | |
algorithm: 'electrum-ecies' | |
}) | |
console.log('Swiping: ', baemailData, cryptoOperations) | |
let response; | |
if (imb instanceof Element) { | |
response = await vmb(imb, cryptoOperations) | |
} else { | |
response = await imb.swipe({ | |
cryptoOperations: cryptoOperations | |
}) | |
} | |
console.log('Success: ', response) | |
let OP_RETURN = ['1BAESxZMweg2mG4FG2DEZmB1Ury2ruAr9K'] | |
OP_RETURN.push(response.cryptoOperations.find(x => x.name === 'baemailData').value) | |
OP_RETURN.push(response.cryptoOperations.find(x => x.name === 'privateBaemail').value) | |
OP_RETURN.push(response.cryptoOperations.find(x => x.name === 'privateSent').value) | |
OP_RETURN.push('|', '15igChEkUWgx4dsEcSuPitcLNZmNDfUvgA', biscuit, response.cryptoOperations.find(x => x.name ==='mySignature').value, fromPki, fromPaymail) | |
let outputs = [] | |
outputs.push({ | |
script: bsv.Script.buildSafeDataOut(OP_RETURN).toASM(), | |
amount: '0', | |
currency: 'USD' | |
}) | |
outputs.push({ | |
to: to, | |
amount: strimount, | |
currency: 'USD' | |
}) | |
outputs.push({ | |
to: '1BaemaiLK15EnJyFEhwLbrYJmRjqYoBMTe', | |
amount: '0.01', | |
currency: 'USD' | |
}) | |
return { | |
outputs: outputs, | |
buttonData: baemailData | |
} | |
} catch (er) { | |
console.log(er) | |
return false | |
} | |
} | |
Baemail.createMessage( | |
document.querySelector("#button"), // el | |
"644@moneybutton.com", // to | |
"unwriter", // fromName | |
"644@moneybutton.com", // fromPaymail | |
"Test subject", // subject | |
"This is a test", // html, | |
"0" // amount | |
).then((res) => { | |
console.log("res = ", res) | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment