Stellarでrawトランザクションを生成・送信
$ npm install -g stellar-sdk
stellar_sign.jsを作成
var StellarSdk = require('stellar-sdk');
// The source account is the account we will be signing and sending from.
//var sourceSecretKey = 'SAKRB7EE6H23EF733WFU76RPIYOPEWVOMBBUXDQYQ3OF4NF6ZY6B6VLW';
var sourceSecretKey = 'SDJNVEAGYGUNXF4TNCWUZ4MHZ6CV35ULUSMTMVVC36ZKHCOKV73KQM34';
// Derive Keypair object and public key (that starts with a G) from the secret
var sourceKeypair = StellarSdk.Keypair.fromSecret(sourceSecretKey);
var sourcePublicKey = sourceKeypair.publicKey();
//var receiverPublicKey = 'GAIRISXKPLOWZBMFRPU5XRGUUX3VMA3ZEWKBM5MSNRU3CHV6P4PYZ74D';
var receiverPublicKey = 'GBUBE5JLB2P2VIV3HY6EAMGNJDDF7EJO27HJ6CYAOWKE625KB2675WKD';
// Configure StellarSdk to talk to the horizon instance hosted by Stellar.org
// To use the live network, set the hostname to 'horizon.stellar.org'
var server = new StellarSdk.Server('https://horizon-testnet.stellar.org');
// Uncomment the following line to build transactions for the live network. Be
// sure to also change the horizon hostname.
// StellarSdk.Network.usePublicNetwork();
StellarSdk.Network.useTestNetwork();
// Transactions require a valid sequence number that is specific to this account.
// We can fetch the current sequence number for the source account from Horizon.
server.loadAccount(sourcePublicKey)
.then(function(account) {
var transaction = new StellarSdk.TransactionBuilder(account)
// Add a payment operation to the transaction
.addOperation(StellarSdk.Operation.payment({
destination: receiverPublicKey,
// The term native asset refers to lumens
asset: StellarSdk.Asset.native(),
// Specify 350.1234567 lumens. Lumens are divisible to seven digits past
// the decimal. They are represented in JS Stellar SDK in string format
// to avoid errors from the use of the JavaScript Number data structure.
amount: '350.1234567',
}))
// Uncomment to add a memo (https://www.stellar.org/developers/learn/concepts/transactions.html)
// .addMemo(StellarSdk.Memo.text('Hello world!'))
.build();
// Sign this transaction with the secret key
// NOTE: signing is transaction is network specific. Test network transactions
// won't work in the public network. To switch networks, use the Network object
// as explained above (look for StellarSdk.Network).
transaction.sign(sourceKeypair);
// Let's see the XDR (encoded in base64) of the transaction we just built
console.log(transaction.toEnvelope().toXDR('base64'));
// Submit the transaction to the Horizon server. The Horizon server will then
// submit the transaction into the network for us.
server.submitTransaction(transaction)
.then(function(transactionResult) {
console.log(JSON.stringify(transactionResult, null, 2));
console.log('\nSuccess! View the transaction at: ');
console.log(transactionResult._links.transaction.href);
})
.catch(function(err) {
console.log('An error has occured:');
console.log(err);
});
})
.catch(function(e) {
console.error(e);
});
$ node stellar_sign.js
AAAAAGgSdSsOn6qiuz48QDDNSMZfkS7Xzp8LAHWUT2uqDr3+AAAAZAB/iskAAAADAAAAAAAAAAAAAAABAAAAAAAAAAEAAAAAaBJ1Kw6fqqK7PjxAMM1Ixl+RLtfOnwsAdZRPa6oOvf4AAAAAAAAAANCwmYcAAAAAAAAAAaoOvf4AAABAK6QglupIeJkiezUGUu9U9KFRLk+SXuAtoVS67Ss0Ip0ogljo88nJScAVoOkYD4mADF4vTIKtiDB5NuUobxesAQ==
{
"_links": {
"transaction": {
"href": "https://horizon-testnet.stellar.org/transactions/6468da6daa8b40342b362adb623fd66fdbd3ed7718c3e8312437f7cf18892863"
}
},
"hash": "6468da6daa8b40342b362adb623fd66fdbd3ed7718c3e8312437f7cf18892863",
"ledger": 10815182,
"envelope_xdr": "AAAAAGgSdSsOn6qiuz48QDDNSMZfkS7Xzp8LAHWUT2uqDr3+AAAAZAB/iskAAAADAAAAAAAAAAAAAAABAAAAAAAAAAEAAAAAaBJ1Kw6fqqK7PjxAMM1Ixl+RLtfOnwsAdZRPa6oOvf4AAAAAAAAAANCwmYcAAAAAAAAAAaoOvf4AAABAK6QglupIeJkiezUGUu9U9KFRLk+SXuAtoVS67Ss0Ip0ogljo88nJScAVoOkYD4mADF4vTIKtiDB5NuUobxesAQ==",
"result_xdr": "AAAAAAAAAGQAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAA=",
"result_meta_xdr": "AAAAAQAAAAIAAAADAKUGzgAAAAAAAAAAaBJ1Kw6fqqK7PjxAMM1Ixl+RLtfOnwsAdZRPa6oOvf4AAAAXP4YVVAB/iskAAAACAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAABAKUGzgAAAAAAAAAAaBJ1Kw6fqqK7PjxAMM1Ixl+RLtfOnwsAdZRPa6oOvf4AAAAXP4YVVAB/iskAAAADAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAABAAAAAA=="
}
Success! View the transaction at:
https://horizon-testnet.stellar.org/transactions/6468da6daa8b40342b362adb623fd66fdbd3ed7718c3e8312437f7cf18892863