Guest User

Untitled

a guest
Sep 5th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.46 KB | None | 0 0
  1. import * as Req from 'request-promise-lite';
  2. import * as DashD from './RPCDefinitions';
  3.  
  4. /**
  5. * Each call returns this
  6. */
  7. export interface CallResult<A> {
  8. readonly result?: A;
  9. readonly error?: any;
  10. readonly id: number;
  11. }
  12.  
  13. /**
  14. * Configurations for instantiating DarkcoinClient
  15. */
  16. export interface DashdConfig {
  17. readonly url: string;
  18. readonly user: string;
  19. readonly password: string;
  20. }
  21.  
  22. /**
  23. * Client instance for doing RPC calls on Dashd
  24. */
  25. export default class DarkcoinClient {
  26. public readonly config: DashdConfig;
  27. constructor(dashdConfig: DashdConfig) {
  28. this.config = dashdConfig;
  29. }
  30.  
  31. /**
  32. * This methods allows to call any of the RPC methods provided by dashd directly.
  33. * Call it with 'help' for the full list of commands.
  34. * @param method name of the method to call, e.g getblockhash
  35. * @param params params of the method, e.g. height for getblockhash
  36. * @param callId id to associate with call
  37. */
  38. public callRPCMethod<T>(
  39. method: string,
  40. params: ReadonlyArray<any>,
  41. callId?: number
  42. ): Promise<CallResult<T>> {
  43. const id: number = callId ? callId : Math.floor(Math.random() * 100000);
  44. const auth = { user: this.config.user, password: this.config.password };
  45. const body = { method, params, id };
  46. const req = new Req.Request('POST', this.config.url, {
  47. json: true,
  48. body,
  49. auth
  50. });
  51.  
  52. return req.run().then(response => {
  53. return (response as any) as CallResult<T>;
  54. });
  55. }
  56.  
  57.  
  58. // Wallet Methods
  59.  
  60. /**
  61. * Mark in-wallet transaction <txid> as abandoned
  62. * This will mark this transaction and all its in-wallet descendants as abandoned which will allow
  63. * for their inputs to be respent. It can be used to replace "stuck" or evicted transactions.
  64. * It only works on transactions which are not included in a block and are not currently in the mempool.
  65. * It has no effect on transactions which are already conflicted or abandoned.
  66. */
  67. public abandonTransaction(txId: string): Promise<CallResult<null>> {
  68. return this.callRPCMethod<null>('abandontransaction', [txId]);
  69. }
  70.  
  71. /**
  72. * Returns an object containing various wallet state info.
  73. */
  74. public getWalletInfo(): Promise<CallResult<DashD.WalletInfo>> {
  75. return this.callRPCMethod<DashD.WalletInfo>('getwalletinfo', []);
  76. }
  77.  
  78. /**
  79. * Returns a new Dash address for receiving payments.
  80. */
  81. public getNewAddress(): Promise<CallResult<string>> {
  82. return this.callRPCMethod<string>('getnewaddress', []);
  83. }
  84.  
  85. /**
  86. * Send an amount to a given address.
  87. * Returns transaction id.
  88. */
  89. public sendToAddress(
  90. dest: string,
  91. amount: number,
  92. comment?: string,
  93. commentTo?: string,
  94. subtractFeeFromAmount?: boolean,
  95. useIS?: boolean,
  96. usePS?: boolean
  97. ): Promise<CallResult<string>> {
  98. const params: ReadonlyArray<any> = [
  99. dest,
  100. amount,
  101. comment,
  102. commentTo,
  103. subtractFeeFromAmount,
  104. useIS,
  105. usePS
  106. ];
  107. return this.filterUndefined(arguments, params).then(filteredParams =>
  108. this.callRPCMethod<string>('sendtoaddress', filteredParams)
  109. );
  110. }
  111.  
  112. /**
  113. * Set the transaction fee per kB. Overwrites the paytxfee parameter.
  114. * @param amount The transaction fee in DASH/kB
  115. */
  116. public setTxFee(amount: number): Promise<CallResult<boolean>> {
  117. return this.callRPCMethod<boolean>('settxfee', [amount]);
  118. }
  119.  
  120. /**
  121. * Sign a message with the private key of an address
  122. * @param address The dash address to use for the private key.
  123. * @param message The message to create a signature of.
  124. * @returns The signature of the message encoded in base 64
  125. */
  126. public signMessage(address: string, message: string): Promise<CallResult<string>> {
  127. return this.callRPCMethod<string>('signmessage', [address, message]);
  128. }
  129.  
  130.  
  131. // Masternodes
  132.  
  133. /**
  134. * Returns key/value dictionary pairs for all masternodes.
  135. */
  136. public masternodeList(): Promise<CallResult<DashD.MasterNodeList>> {
  137. return this.callRPCMethod<DashD.MasterNodeList>('masternodelist', []);
  138. }
  139.  
  140. // GObjects
  141.  
  142. /**
  143. * Prepare GObject
  144. */
  145. public gobjectPrepare(parentHash: string, revision: number, creationTime: number, gobjectData: string): Promise<CallResult<string>> {
  146. return this.callRPCMethod<string>('gobject', ['prepare', parentHash, revision, creationTime, gobjectData]);
  147. }
  148.  
  149. /**
  150. * Submit GObject
  151. */
  152. public gobjectSubmit(parentHash: string, revision: number, creationTime: number, gobjectData: string, txId: string): Promise<CallResult<string>> {
  153. return this.callRPCMethod<string>('gobject', ['submit', parentHash, revision, creationTime, gobjectData, txId]);
  154. }
  155.  
  156. /**
  157. * Returns key/value pairs for all current GObjects with the key. Will include both funding gobjects and trigger gobjects,
  158. * Make sure to parse them and pull out the ones you want.
  159. */
  160. public gobjectList(): Promise<CallResult<DashD.GObjectList>> {
  161. return this.callRPCMethod<DashD.GObjectList>('gobject', ['list']);
  162. }
  163.  
  164. public gobjectCurrentVotes(
  165. hash: string
  166. ): Promise<CallResult<DashD.GObjectCurrentVotesList>> {
  167. return this.callRPCMethod<DashD.GObjectCurrentVotesList>('gobject', ['getcurrentvotes', hash]);
  168. }
  169.  
  170.  
  171. // Network Information
  172.  
  173. /**
  174. * The getnetworkinfo RPC returns information about the node’s connection to the network.
  175. */
  176. public getNetworkInfo(): Promise<CallResult<DashD.NetworkInfo>> {
  177. return this.callRPCMethod<DashD.NetworkInfo>('getnetworkinfo', []);
  178. }
  179.  
  180. /**
  181. * Returns network related governance info, i.e. superblock height, proposal fee, and minquorum.
  182. */
  183. public getGovernanceInfo(): Promise<CallResult<DashD.GovernanceInfo>> {
  184. return this.callRPCMethod<DashD.GovernanceInfo>('getgovernanceinfo', []);
  185. }
  186.  
  187. /**
  188. * Returns mining related info, i.e. difficulty, blocksize, currentblocktx, and a network hash rate estimate.
  189. */
  190. public getMiningInfo(): Promise<CallResult<DashD.MiningInfo>> {
  191. return this.callRPCMethod<DashD.MiningInfo>('getmininginfo', []);
  192. }
  193.  
  194.  
  195. // Private Methods
  196.  
  197. /**
  198. * Build the parameter list by removing optional arguments
  199. * @param originalArgs original argument list
  200. * @param params
  201. */
  202. private filterUndefined<T>(
  203. originalArgs: IArguments,
  204. params: ReadonlyArray<T>
  205. ): Promise<ReadonlyArray<T>> {
  206. const undefinedIndex = params.findIndex(v => v === undefined);
  207. if (undefinedIndex >= 0 && undefinedIndex < originalArgs.length - 1) {
  208. return Promise.reject(
  209. new Error('Undefined arguments found after defined arguments.')
  210. );
  211. }
  212. return Promise.resolve(params.filter(v => v !== undefined));
  213. }
  214. }
Add Comment
Please, Sign In to add comment