Last active
February 7, 2019 12:49
-
-
Save planethouki/f5a05b73ced1599855ec651b4b575e7c to your computer and use it in GitHub Desktop.
NEM2カタパルトで双方向ペイメントチャネルするには その3 ペイロードの送信と罰則 ref: https://qiita.com/planethouki/items/945239fe04d2af1fb203
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
function sendAndCosignAggregateTransaction(payloads, address1, account2, addressG) { | |
return new Promise((resolve, reject) => { | |
const calcurateTransactionHash = function(payload) { | |
const hashInputPayload = | |
payload.substr(4*2,32*2) + | |
payload.substr((4+64)*2,32*2) + | |
payload.substr((4+64+32)*2); | |
const hasher = sha3_256.create(); | |
return hasher.update(Buffer.from(hashInputPayload, 'hex')).hex().toUpperCase(); | |
} | |
const cosignAggregateBondedTransaction = function(transaction, account) { | |
const cosignatureTransaction = CosignatureTransaction.create(transaction); | |
const signedTransaction = account.signCosignatureTransaction(cosignatureTransaction); | |
return signedTransaction; | |
} | |
const payload = payloads[0]; | |
const hash = calcurateTransactionHash(payload); | |
const lockFundsTxPayload = payloads[1]; | |
const lockFundsTxHash = calcurateTransactionHash(lockFundsTxPayload); | |
const listener = new Listener(ENDPOINT); | |
const transactionHttp = new TransactionHttp(ENDPOINT); | |
listener.open().then(() => { | |
return new Promise((resolveReq, rejectReq) => { | |
console.log(lockFundsTxHash); | |
request({ | |
url: `${ENDPOINT}/transaction`, | |
method: 'PUT', | |
headers: { | |
'Content-Type':'application/json' | |
}, | |
json: {"payload": lockFundsTxPayload} | |
}, (error, response, body) => { | |
console.log(body); | |
error ? rejectReq(error) : resolveReq(); | |
}); | |
}); | |
}).then(() => { | |
listener.confirmed(addressG).pipe( | |
filter((transaction) => transaction.transactionInfo !== undefined | |
&& transaction.transactionInfo.hash === lockFundsTxHash), | |
flatMap(ignored => rx.from(new Promise((resolveReq, rejectReq) => { | |
console.log(hash); | |
request({ | |
url: `${ENDPOINT}/transaction/partial`, | |
method: 'PUT', | |
headers: { | |
'Content-Type':'application/json' | |
}, | |
json: {payload} | |
}, (error, response, body) => { | |
error ? rejectReq(error) : resolveReq(body); | |
}); | |
}))) | |
).subscribe( | |
x => console.log(x), | |
err => console.error(err) | |
); | |
listener.aggregateBondedAdded(account2.address).pipe( | |
filter((_) => !_.signedByAccount(account2.publicAccount)), | |
map(transaction => cosignAggregateBondedTransaction(transaction, account2)), | |
flatMap(cosignatureSignedTransaction => transactionHttp.announceAggregateBondedCosignature(cosignatureSignedTransaction)) | |
).subscribe( | |
x => console.log(x), | |
err => console.error(err) | |
); | |
listener.confirmed(address1).pipe( | |
filter((transaction) => transaction.transactionInfo !== undefined | |
&& transaction.transactionInfo.hash === hash) | |
).subscribe( | |
ignore => { | |
listener.close(); | |
resolve(); | |
}, | |
err => console.error(err) | |
); | |
}).catch((error) => { | |
console.error(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
$ node paymentch9c.js | |
=== send XEM first === | |
EB4B38E0166CC7F14E0148B0B1CA9FEC0D83EBC89B2D8DB009A6465AE84F46D3 | |
connection open | |
TransactionAnnounceResponse { | |
message: 'packet 9 was pushed to the network via /transaction' } | |
=== create multisig account === | |
C97C074389684061FD9A5876CB01E5343F5E84FBF17DB7B9B3F0660D5229E520 | |
connection open | |
TransactionAnnounceResponse { | |
message: 'packet 9 was pushed to the network via /transaction' } | |
=== show balance [Multisig, Alice, Bob] === | |
[ 0, 5000000, 5000000 ] | |
=== create opning transaction payload === | |
=== create commitment 1 transaction payload === | |
=== send opening transaction === | |
connection open | |
9254669288957604223ED99703D8A7759CAD4E73E1C54FBF2CBDDC481E7C186F | |
{ message: 'packet 9 was pushed to the network via /transaction' } | |
B0CAA690C5B19F371CDDB1FEF1E9E3DC3BB03C7E3018AB1AF0755857D333EC6A | |
{ message: | |
'packet 500 was pushed to the network via /transaction/partial' } | |
TransactionAnnounceResponse { | |
message: | |
'packet 501 was pushed to the network via /transaction/cosignature' } | |
=== show balance [Multisig, Alice, Bob] === | |
[ 10000000, 0, 0 ] | |
=== create commitment 2 transaction payload === | |
=== cheat (send old commitment transaction) === | |
connection open | |
9D3CC19B769B5F0282F297CDE0545757929E4D75B1F28BAAE964F171FE7B8524 | |
{ message: 'packet 9 was pushed to the network via /transaction' } | |
E1880F5B30D430F3A28B18CDCB88623783A2BBCD22AA3CF30BBC32208C18914A | |
{ message: | |
'packet 500 was pushed to the network via /transaction/partial' } | |
TransactionAnnounceResponse { | |
message: | |
'packet 501 was pushed to the network via /transaction/cosignature' } | |
=== show balance [Multisig, Alice, Bob] === | |
[ 0, 4000000, 0 ] | |
=== penalty (collect all XEM) === | |
D9DEDBB21B4883EB37F7688AC940EAC5FD4E201F0824E857E50AB4D5F07D5484 | |
connection open | |
TransactionAnnounceResponse { | |
message: 'packet 9 was pushed to the network via /transaction' } | |
=== show balance [Multisig, Alice, Bob] === | |
[ 0, 10000000, 0 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment