-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from ardriveapp/PE-661_send_signed_tx
PE-661: send signed tx
- Loading branch information
Showing
13 changed files
with
278 additions
and
46 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,27 @@ | ||
import { ByteCount } from 'ardrive-core-js'; | ||
import { CLICommand, ParametersHelper } from '../CLICommand'; | ||
import { CLIAction } from '../CLICommand/action'; | ||
import { SUCCESS_EXIT_CODE } from '../CLICommand/error_codes'; | ||
import { BoostParameter } from '../parameter_declarations'; | ||
import axios, { AxiosResponse } from 'axios'; | ||
|
||
async function getBaseReward(byteCount?: ByteCount): Promise<string> { | ||
const response: AxiosResponse = await axios.get(`https://arweave.net/price/${byteCount ?? 0}`); | ||
return `${response.data}`; | ||
} | ||
|
||
new CLICommand({ | ||
name: 'base-reward', | ||
parameters: [BoostParameter], | ||
action: new CLIAction(async function action(options) { | ||
const parameters = new ParametersHelper(options); | ||
let baseRewardStr = await getBaseReward(); | ||
const multiple = parameters.getOptionalBoostSetting(); | ||
if (multiple) { | ||
baseRewardStr = multiple.boostReward(baseRewardStr); | ||
} | ||
|
||
console.log(baseRewardStr); | ||
return SUCCESS_EXIT_CODE; | ||
}) | ||
}); |
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,52 @@ | ||
import { ADDR, AR, JWKWallet, TxID, W, Winston } from 'ardrive-core-js'; | ||
import { CreateTransactionInterface } from 'arweave/node/common'; | ||
import { cliArweave, CLI_APP_NAME, CLI_APP_VERSION } from '..'; | ||
import { CLICommand } from '../CLICommand'; | ||
import { ParametersHelper } from '../CLICommand'; | ||
import { CLIAction } from '../CLICommand/action'; | ||
import { SUCCESS_EXIT_CODE } from '../CLICommand/error_codes'; | ||
import { | ||
ArAmountParameter, | ||
DestinationAddressParameter, | ||
LastTxParameter, | ||
RewardParameter, | ||
WalletTypeParameters | ||
} from '../parameter_declarations'; | ||
|
||
new CLICommand({ | ||
name: 'create-tx', | ||
parameters: [ | ||
ArAmountParameter, | ||
DestinationAddressParameter, | ||
RewardParameter, | ||
LastTxParameter, | ||
...WalletTypeParameters | ||
], | ||
action: new CLIAction(async function action(options) { | ||
const parameters = new ParametersHelper(options); | ||
const arAmount = parameters.getRequiredParameterValue(ArAmountParameter, AR.from); | ||
const winston: Winston = arAmount.toWinston(); | ||
const destAddress = parameters.getRequiredParameterValue(DestinationAddressParameter, ADDR); | ||
const jwkWallet = (await parameters.getRequiredWallet()) as JWKWallet; | ||
const lastTxParam = parameters.getParameterValue(LastTxParameter); // Can be provided as a txID or empty string | ||
const last_tx = lastTxParam && lastTxParam.length ? `${TxID(lastTxParam)}` : undefined; | ||
|
||
// Create and sign transaction | ||
const trxAttributes: Partial<CreateTransactionInterface> = { | ||
target: destAddress.toString(), | ||
quantity: winston.toString(), | ||
reward: `${parameters.getRequiredParameterValue(RewardParameter, W)}`, | ||
last_tx | ||
}; | ||
const transaction = await cliArweave.createTransaction(trxAttributes, jwkWallet.getPrivateKey()); | ||
transaction.addTag('App-Name', CLI_APP_NAME); | ||
transaction.addTag('App-Version', CLI_APP_VERSION); | ||
transaction.addTag('Type', 'transfer'); | ||
|
||
await cliArweave.transactions.sign(transaction, jwkWallet.getPrivateKey()); | ||
|
||
console.log(JSON.stringify(transaction)); | ||
|
||
return SUCCESS_EXIT_CODE; | ||
}) | ||
}); |
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 |
---|---|---|
@@ -1,21 +1,25 @@ | ||
import '../parameter_declarations'; | ||
import './base_reward'; | ||
import './create_drive'; | ||
import './create_folder'; | ||
import './create_tx'; | ||
import './drive_info'; | ||
import './upload_file'; | ||
import './tx_status'; | ||
import './get_mempool'; | ||
import './send_ar'; | ||
import './get_balance'; | ||
import './get_address'; | ||
import './file_info'; | ||
import './folder_info'; | ||
import './generate_seedphrase'; | ||
import './generate_wallet'; | ||
import './list_folder'; | ||
import './list_drive'; | ||
import './get_address'; | ||
import './get_balance'; | ||
import './get_drive_key'; | ||
import './get_file_key'; | ||
import './get_mempool'; | ||
import './last_tx'; | ||
import './list_all_drives'; | ||
import './folder_info'; | ||
import './create_folder'; | ||
import './file_info'; | ||
import './list_drive'; | ||
import './list_folder'; | ||
import './move_file'; | ||
import './move_folder'; | ||
import './get_drive_key'; | ||
import './get_file_key'; | ||
import './send_ar'; | ||
import './send_tx'; | ||
import './tx_status'; | ||
import './upload_file'; |
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,23 @@ | ||
import { ArweaveAddress } from 'ardrive-core-js'; | ||
import { CLICommand, ParametersHelper } from '../CLICommand'; | ||
import { CLIAction } from '../CLICommand/action'; | ||
import { SUCCESS_EXIT_CODE } from '../CLICommand/error_codes'; | ||
import { AddressParameter, WalletTypeParameters } from '../parameter_declarations'; | ||
import axios, { AxiosResponse } from 'axios'; | ||
|
||
async function lastTxForAddress(address: ArweaveAddress): Promise<string> { | ||
const response: AxiosResponse = await axios.get(`https://arweave.net/wallet/${address}/last_tx`); | ||
return `${response.data}`; | ||
} | ||
|
||
new CLICommand({ | ||
name: 'last-tx', | ||
parameters: [...WalletTypeParameters, AddressParameter], | ||
action: new CLIAction(async function action(options) { | ||
const parameters = new ParametersHelper(options); | ||
const walletAddress = await parameters.getWalletAddress(); | ||
const lastTx = await lastTxForAddress(walletAddress); | ||
console.log(lastTx); | ||
return SUCCESS_EXIT_CODE; | ||
}) | ||
}); |
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,54 @@ | ||
import { cliArweave } from '..'; | ||
import { CLICommand, ParametersHelper } from '../CLICommand'; | ||
import { DryRunParameter, TxFilePathParameter } from '../parameter_declarations'; | ||
import * as fs from 'fs'; | ||
import Transaction from 'arweave/node/lib/transaction'; | ||
import * as crypto from 'crypto'; | ||
import { ERROR_EXIT_CODE, SUCCESS_EXIT_CODE } from '../CLICommand/error_codes'; | ||
import { CLIAction } from '../CLICommand/action'; | ||
import { b64UrlToBuffer, bufferTob64Url } from 'ardrive-core-js'; | ||
|
||
new CLICommand({ | ||
name: 'send-tx', | ||
parameters: [TxFilePathParameter, DryRunParameter], | ||
action: new CLIAction(async function action(options) { | ||
const parameters = new ParametersHelper(options); | ||
const transaction = new Transaction( | ||
JSON.parse(fs.readFileSync(parameters.getRequiredParameterValue(TxFilePathParameter)).toString()) | ||
); | ||
const srcAddress = bufferTob64Url( | ||
crypto.createHash('sha256').update(b64UrlToBuffer(transaction.owner)).digest() | ||
); | ||
|
||
console.log(`Source address: ${srcAddress}`); | ||
console.log(`AR amount sent: ${cliArweave.ar.winstonToAr(transaction.quantity)}`); | ||
console.log(`Destination address: ${transaction.target}`); | ||
|
||
const response = await (async () => { | ||
if (options.dryRun) { | ||
return { status: 200, statusText: 'OK', data: '' }; | ||
} else { | ||
return await cliArweave.transactions.post(transaction); | ||
} | ||
})(); | ||
if (response.status === 200 || response.status === 202) { | ||
console.log( | ||
JSON.stringify( | ||
{ | ||
txID: transaction.id, | ||
winston: transaction.quantity, | ||
reward: transaction.reward | ||
}, | ||
null, | ||
4 | ||
) | ||
); | ||
|
||
return SUCCESS_EXIT_CODE; | ||
} else { | ||
console.log(`Failed to send tx with error: ${response.statusText}`); | ||
} | ||
|
||
return ERROR_EXIT_CODE; | ||
}) | ||
}); |
Oops, something went wrong.