mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-30 17:15:49 +00:00
Update README with transaction signing example
This commit is contained in:
@@ -17,3 +17,71 @@
|
||||
accountID: 'r9LqNeG6qHxjeUocjvVki2XR35weJ9mZgQ',
|
||||
publicKey: 'ED5F5AC8B98974A3CA843326D9B88CEBD0560177B973EE0B149F782CFAA06DC66A' }')
|
||||
```
|
||||
|
||||
## Sign a transaction
|
||||
see [examples/sign-transaction.js](examples/sign-transaction.js)
|
||||
```js
|
||||
var Transaction = require('ripple-lib').Transaction;
|
||||
var keyPairFromSeed = require('../').keyPairFromSeed;
|
||||
|
||||
var SIGNING_PREFIX = [0x53, 0x54, 0x58, 0x00];
|
||||
|
||||
function prettyJSON(obj) {
|
||||
return JSON.stringify(obj, undefined, 2);
|
||||
}
|
||||
|
||||
function signingData(tx) {
|
||||
return SIGNING_PREFIX.concat(tx.serialize().buffer);
|
||||
}
|
||||
|
||||
function signTxJson(seed, json) {
|
||||
var keyPair = keyPairFromSeed(seed);
|
||||
var tx = Transaction.from_json(json);
|
||||
var tx_json = tx.tx_json;
|
||||
|
||||
tx_json.SigningPubKey = keyPair.pubKeyHex();
|
||||
tx_json.TxnSignature = keyPair.signHex(signingData(tx));
|
||||
|
||||
var serialized = tx.serialize();
|
||||
|
||||
var id = tx.hash('HASH_TX_ID', /* Uint256: */ false , /* pre: */ serialized);
|
||||
|
||||
return {
|
||||
hash: id,
|
||||
tx_blob: serialized.to_hex(),
|
||||
tx_json: tx_json
|
||||
};
|
||||
}
|
||||
|
||||
var seed = 'sEd7t79mzn2dwy3vvpvRmaaLbLhvme6';
|
||||
var tx_json = {
|
||||
Account: 'r9LqNeG6qHxjeUocjvVki2XR35weJ9mZgQ',
|
||||
Amount: '1000',
|
||||
Destination: 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh',
|
||||
Fee: '10',
|
||||
Flags: 2147483648,
|
||||
Sequence: 1,
|
||||
TransactionType: 'Payment',
|
||||
};
|
||||
|
||||
console.log(prettyJSON(signTxJson(seed, tx_json)));
|
||||
|
||||
```
|
||||
|
||||
```json
|
||||
{
|
||||
"hash": "1B6B9652F95D826C9D9C3FD30F208130433CBC7C48C10F6EC2CC5E4A85D167FF",
|
||||
"tx_blob": "120000228000000024000000016140000000000003E868400000000000000A7321ED5F5AC8B98974A3CA843326D9B88CEBD0560177B973EE0B149F782CFAA06DC66A74407D0825105229923B261C716F225181E5A66A34C9480446ABE64818A673954CC34D42946CD82172814F037976AE3800BDE983624A45FCDBED4A548C4650BF900D81145B812C9D57731E27A2DA8B1830195F88EF32A3B68314B5F762798A53D543A014CAF8B297CFF8F2F937E8",
|
||||
"tx_json": {
|
||||
"Account": "r9LqNeG6qHxjeUocjvVki2XR35weJ9mZgQ",
|
||||
"Amount": "1000",
|
||||
"Destination": "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh",
|
||||
"Fee": "10",
|
||||
"Flags": 2147483648,
|
||||
"Sequence": 1,
|
||||
"TransactionType": "Payment",
|
||||
"SigningPubKey": "ED5F5AC8B98974A3CA843326D9B88CEBD0560177B973EE0B149F782CFAA06DC66A",
|
||||
"TxnSignature": "7D0825105229923B261C716F225181E5A66A34C9480446ABE64818A673954CC34D42946CD82172814F037976AE3800BDE983624A45FCDBED4A548C4650BF900D"
|
||||
}
|
||||
}
|
||||
```
|
||||
45
packages/ripple-keypairs/examples/sign-transaction.js
Normal file
45
packages/ripple-keypairs/examples/sign-transaction.js
Normal file
@@ -0,0 +1,45 @@
|
||||
var Transaction = require('ripple-lib').Transaction;
|
||||
var keyPairFromSeed = require('../').keyPairFromSeed;
|
||||
|
||||
var SIGNING_PREFIX = [0x53, 0x54, 0x58, 0x00];
|
||||
|
||||
function prettyJSON(obj) {
|
||||
return JSON.stringify(obj, undefined, 2);
|
||||
}
|
||||
|
||||
function signingData(tx) {
|
||||
return SIGNING_PREFIX.concat(tx.serialize().buffer);
|
||||
}
|
||||
|
||||
function signTxJson(seed, json) {
|
||||
var keyPair = keyPairFromSeed(seed);
|
||||
var tx = Transaction.from_json(json);
|
||||
var tx_json = tx.tx_json;
|
||||
|
||||
tx_json.SigningPubKey = keyPair.pubKeyHex();
|
||||
tx_json.TxnSignature = keyPair.signHex(signingData(tx));
|
||||
|
||||
var serialized = tx.serialize();
|
||||
|
||||
var id = tx.hash('HASH_TX_ID', /* Uint256: */ false , /* pre: */ serialized);
|
||||
|
||||
return {
|
||||
hash: id,
|
||||
tx_blob: serialized.to_hex(),
|
||||
tx_json: tx_json
|
||||
};
|
||||
}
|
||||
|
||||
var seed = 'sEd7t79mzn2dwy3vvpvRmaaLbLhvme6';
|
||||
var tx_json = {
|
||||
Account: 'r9LqNeG6qHxjeUocjvVki2XR35weJ9mZgQ',
|
||||
Amount: '1000',
|
||||
Destination: 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh',
|
||||
Fee: '10',
|
||||
Flags: 2147483648,
|
||||
Sequence: 1,
|
||||
TransactionType: 'Payment',
|
||||
};
|
||||
|
||||
console.log('unsigned', prettyJSON(tx_json));
|
||||
console.log('signed', prettyJSON(signTxJson(seed, tx_json)));
|
||||
@@ -34,7 +34,7 @@
|
||||
"map-stream": "~0.1.0",
|
||||
"mocha": "~2.1.0",
|
||||
"nock": "^0.34.1",
|
||||
"ripple-lib": "ripple/ripple-lib#8d98e443c5302a87d70410d834758099595d46df",
|
||||
"ripple-lib": "^0.12.4",
|
||||
"webpack": "~1.5.3",
|
||||
"yargs": "~1.3.1"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user