|
| 1 | +/** |
| 2 | + * This script sets up an incoming payment on a receiving wallet address, |
| 3 | + * and a quote on the sending wallet address (after getting grants for both of the resources). |
| 4 | + * |
| 5 | + * The final step is asking for an outgoing payment grant for the sending wallet address. |
| 6 | + * Since this needs user interaction, you will need to navigate to the URL, and accept the interactive grant. |
| 7 | + * |
| 8 | + * To start, please add the variables for configuring the client & the wallet addresses for the payment. |
| 9 | + */ |
| 10 | + |
| 11 | +import { |
| 12 | + createAuthenticatedClient, |
| 13 | + OpenPaymentsClientError, |
| 14 | + isFinalizedGrant |
| 15 | +} from '@interledger/open-payments' |
| 16 | +import readline from 'readline/promises' |
| 17 | +;(async () => { |
| 18 | + // Client configuration |
| 19 | + const PRIVATE_KEY_PATH = 'private.key' |
| 20 | + const KEY_ID = '' |
| 21 | + |
| 22 | + // Make sure the wallet addresses starts with https:// (not $) |
| 23 | + const CLIENT_WALLET_ADDRESS_URL = '' |
| 24 | + const SENDING_WALLET_ADDRESS_URL = '' |
| 25 | + const RECEIVING_WALLET_ADDRESS_URL = '' |
| 26 | + |
| 27 | + const client = await createAuthenticatedClient({ |
| 28 | + walletAddressUrl: CLIENT_WALLET_ADDRESS_URL, |
| 29 | + keyId: KEY_ID, |
| 30 | + privateKey: PRIVATE_KEY_PATH |
| 31 | + }) |
| 32 | + |
| 33 | + // Step 1: Get the sending and receiving wallet addresses |
| 34 | + const sendingWalletAddress = await client.walletAddress.get({ |
| 35 | + url: SENDING_WALLET_ADDRESS_URL |
| 36 | + }) |
| 37 | + const receivingWalletAddress = await client.walletAddress.get({ |
| 38 | + url: RECEIVING_WALLET_ADDRESS_URL |
| 39 | + }) |
| 40 | + |
| 41 | + console.log('\nStep 1: got wallet addresses', { |
| 42 | + receivingWalletAddress, |
| 43 | + sendingWalletAddress |
| 44 | + }) |
| 45 | + |
| 46 | + // Step 2: Get a grant for the incoming payment, so we can create the incoming payment on the receiving wallet address |
| 47 | + const incomingPaymentGrant = await client.grant.request( |
| 48 | + { |
| 49 | + url: receivingWalletAddress.authServer |
| 50 | + }, |
| 51 | + { |
| 52 | + access_token: { |
| 53 | + access: [ |
| 54 | + { |
| 55 | + type: 'incoming-payment', |
| 56 | + actions: ['read', 'complete', 'create'] |
| 57 | + } |
| 58 | + ] |
| 59 | + } |
| 60 | + } |
| 61 | + ) |
| 62 | + |
| 63 | + console.log( |
| 64 | + '\nStep 2: got incoming payment grant for receiving wallet address', |
| 65 | + incomingPaymentGrant |
| 66 | + ) |
| 67 | + |
| 68 | + if (!isFinalizedGrant(incomingPaymentGrant)) { |
| 69 | + throw new Error('Expected finalized incoming payment grant') |
| 70 | + } |
| 71 | + |
| 72 | + // Step 3: Create the incoming payment. This will be where funds will be received. |
| 73 | + const incomingPayment = await client.incomingPayment.create( |
| 74 | + { |
| 75 | + url: receivingWalletAddress.resourceServer, |
| 76 | + accessToken: incomingPaymentGrant.access_token.value |
| 77 | + }, |
| 78 | + { |
| 79 | + walletAddress: receivingWalletAddress.id, |
| 80 | + incomingAmount: { |
| 81 | + assetCode: receivingWalletAddress.assetCode, |
| 82 | + assetScale: receivingWalletAddress.assetScale, |
| 83 | + value: '1000' |
| 84 | + } |
| 85 | + } |
| 86 | + ) |
| 87 | + |
| 88 | + console.log( |
| 89 | + '\nStep 3: created incoming payment on receiving wallet address', |
| 90 | + incomingPayment |
| 91 | + ) |
| 92 | + |
| 93 | + // Step 4: Get a quote grant, so we can create a quote on the sending wallet address |
| 94 | + const quoteGrant = await client.grant.request( |
| 95 | + { |
| 96 | + url: sendingWalletAddress.authServer |
| 97 | + }, |
| 98 | + { |
| 99 | + access_token: { |
| 100 | + access: [ |
| 101 | + { |
| 102 | + type: 'quote', |
| 103 | + actions: ['create', 'read'] |
| 104 | + } |
| 105 | + ] |
| 106 | + } |
| 107 | + } |
| 108 | + ) |
| 109 | + |
| 110 | + if (!isFinalizedGrant(quoteGrant)) { |
| 111 | + throw new Error('Expected finalized quote grant') |
| 112 | + } |
| 113 | + |
| 114 | + console.log('\nStep 4: got quote grant on sending wallet address', quoteGrant) |
| 115 | + |
| 116 | + // Step 5: Create a quote, this gives an indication of how much it will cost to pay into the incoming payment |
| 117 | + const quote = await client.quote.create( |
| 118 | + { |
| 119 | + url: sendingWalletAddress.resourceServer, |
| 120 | + accessToken: quoteGrant.access_token.value |
| 121 | + }, |
| 122 | + { |
| 123 | + walletAddress: sendingWalletAddress.id, |
| 124 | + receiver: incomingPayment.id, |
| 125 | + method: 'ilp' |
| 126 | + } |
| 127 | + ) |
| 128 | + |
| 129 | + console.log('\nStep 5: got quote on sending wallet address', quote) |
| 130 | + |
| 131 | + // Step 7: Start the grant process for the outgoing payments. |
| 132 | + // This is an interactive grant: the user (in this case, you) will need to accept the grant by navigating to the outputted link. |
| 133 | + const outgoingPaymentGrant = await client.grant.request( |
| 134 | + { |
| 135 | + url: sendingWalletAddress.authServer |
| 136 | + }, |
| 137 | + { |
| 138 | + access_token: { |
| 139 | + access: [ |
| 140 | + { |
| 141 | + type: 'outgoing-payment', |
| 142 | + actions: ['read', 'create'], |
| 143 | + limits: { |
| 144 | + debitAmount: { |
| 145 | + assetCode: quote.debitAmount.assetCode, |
| 146 | + assetScale: quote.debitAmount.assetScale, |
| 147 | + value: quote.debitAmount.value |
| 148 | + } |
| 149 | + }, |
| 150 | + identifier: sendingWalletAddress.id |
| 151 | + } |
| 152 | + ] |
| 153 | + }, |
| 154 | + interact: { |
| 155 | + start: ['redirect'] |
| 156 | + // finish: { |
| 157 | + // method: "redirect", |
| 158 | + // // This is where you can (optionally) redirect a user to after going through interaction. |
| 159 | + // // Keep in mind, you will need to parse the interact_ref in the resulting interaction URL, |
| 160 | + // // and pass it into the grant continuation request. |
| 161 | + // uri: "https://example.com", |
| 162 | + // nonce: crypto.randomUUID(), |
| 163 | + // }, |
| 164 | + } |
| 165 | + } |
| 166 | + ) |
| 167 | + |
| 168 | + console.log( |
| 169 | + '\nStep 7: got pending outgoing payment grant', |
| 170 | + outgoingPaymentGrant |
| 171 | + ) |
| 172 | + console.log( |
| 173 | + 'Please navigate to the following URL, to accept the interaction from the sending wallet:' |
| 174 | + ) |
| 175 | + console.log(outgoingPaymentGrant.interact.redirect) |
| 176 | + |
| 177 | + await readline |
| 178 | + .createInterface({ input: process.stdin, output: process.stdout }) |
| 179 | + .question('\nPlease accept grant and press enter...') |
| 180 | + |
| 181 | + let finalizedOutgoingPaymentGrant |
| 182 | + |
| 183 | + const grantContinuationErrorMessage = |
| 184 | + '\nThere was an error continuing the grant. You probably have not accepted the grant at the url (or it has already been used up, in which case, rerun the script).' |
| 185 | + |
| 186 | + try { |
| 187 | + finalizedOutgoingPaymentGrant = await client.grant.continue({ |
| 188 | + url: outgoingPaymentGrant.continue.uri, |
| 189 | + accessToken: outgoingPaymentGrant.continue.access_token.value |
| 190 | + }) |
| 191 | + } catch (err) { |
| 192 | + if (err instanceof OpenPaymentsClientError) { |
| 193 | + console.log(grantContinuationErrorMessage) |
| 194 | + process.exit() |
| 195 | + } |
| 196 | + |
| 197 | + throw err |
| 198 | + } |
| 199 | + |
| 200 | + if (!isFinalizedGrant(finalizedOutgoingPaymentGrant)) { |
| 201 | + console.log( |
| 202 | + 'There was an error continuing the grant. You probably have not accepted the grant at the url.' |
| 203 | + ) |
| 204 | + process.exit() |
| 205 | + } |
| 206 | + |
| 207 | + console.log( |
| 208 | + '\nStep 6: got finalized outgoing payment grant', |
| 209 | + finalizedOutgoingPaymentGrant |
| 210 | + ) |
| 211 | + |
| 212 | + // Step 7: Finally, create the outgoing payment on the sending wallet address. |
| 213 | + // This will make a payment from the outgoing payment to the incoming one (over ILP) |
| 214 | + const outgoingPayment = await client.outgoingPayment.create( |
| 215 | + { |
| 216 | + url: sendingWalletAddress.resourceServer, |
| 217 | + accessToken: finalizedOutgoingPaymentGrant.access_token.value |
| 218 | + }, |
| 219 | + { |
| 220 | + walletAddress: sendingWalletAddress.id, |
| 221 | + quoteId: quote.id |
| 222 | + } |
| 223 | + ) |
| 224 | + |
| 225 | + console.log( |
| 226 | + '\nStep 7: Created outgoing payment. Funds will now move from the outgoing payment to the incoming payment.', |
| 227 | + outgoingPayment |
| 228 | + ) |
| 229 | + |
| 230 | + process.exit() |
| 231 | +})() |
0 commit comments