Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                ### Interaction

<script src="https://cdn.cosmic.plus/stellar-sdk"></script>
<script src="https://cdn.cosmic.plus/cosmic-lib@2.x"></script>


<form>
  
  <fieldset><legend>#network</legend>
    <select id="network">
      <option value="test" selected>Test</option>
      <option value="public">Public</option>
    </select>
  </fieldset>
  
  <fieldset><legend>#memo</legend>
    <input id="memo" type="text" placeholder="28 chars max.">
  </fieldset>
  
  <fieldset><legend>#destination</legend>
    <input id="destination" type="text" placeholder="Pubkey / Federated address">
  </fieldset>
  
  <fieldset><legend>#amount</legend>
    <input id="amount" type="number" min="0" placeholder="Amount">
  </fieldset>
  
  <fieldset><legend>#submit</legend>
    <input id="submit" type="button" value="Open signing frame">
  </fieldset>
  
  <fieldset><legend>#error</legend>
    <pre id="error"></pre>
  </fieldset>

</form>


### Description

This pen makes use of `cosmic-lib` to create a transfer request that you can sign with your wallet.

### Keypoints

- The payment request accepts a number of optional parameters, such as `memo`, `callback`, `source`, and `asset`.
- **#memo** accepts those type prefixes: `text:`, `base64:`, `id:`, `hash:` & `return:`.
- `asset` syntax is `{code}:{issuer}`, where issuer can be a federated address.
- It is possible to use a custom network by setting `network` to its passphrase and `horizon` to its horizon URL.
- `txRequest.page` can be set to any compatible service, such as _https://stellar-authenticator.org_ or _https://cosmic.plus/js-cosmic-lib/web/demo_.
- `txRequest.open()` optionally accepts a **target** argument: `"frame"`, `"tab"`, `"current"`, or `"sep7"`.

### Challenges

- `[Easy]` Add a **#service** editable selectors with several default options.
- `[Hard]` Add a **#submitSep7** button that builds & transmit the request through the SEP-0007 protocol.

_Give it a try by editing the code!_

### Documentation

- <a href="https://cosmic.plus/#view:js-cosmic-lib/web/doc/CosmicLink.html" target="_blank">CosmicLink documentation</a>
- <a href="https://cosmic.plus/#view:js-cosmic-lib/web/doc/tutorial-10-specs-query.html" target="_blank">CosmicLink specifications</a>
- <a href="https://stellar.github.io/js-stellar-sdk/Operation.html#.payment" target="_blank">StellarSdk payment operation documentation</a>

### Social

- [Discuss that pen on GalacticTalk](https://galactictalk.org/d/2098-pen-of-the-week-cosmic-lib-transfer-request)

              
            
!

CSS

              
                
              
            
!

JS

              
                // $ npm install cosmic-lib
// const cosmicLib = require("cosmic-lib")

const $ = document.querySelector.bind(document);
const service = "https://cosmic.link";

/* Core */
function makeTxRequest() {
  return new cosmicLib.CosmicLink({
    network: $("#network").value,
    memo: $("#memo").value || null
  }).addOperation("payment", {
    destination: $("#destination").value,
    amount: $("#amount").value
  });
}

/* Interface */
function submitTxRequest() {
  const txRequest = makeTxRequest();

  if (!txRequest.status) {
    txRequest.page = service;
    txRequest.open();
  } else {
    const errors = JSON.stringify(txRequest.errors, null, 2);
    $("#error").textContent = errors;
  }
}

/* Events */
$("#submit").onclick = submitTxRequest;

              
            
!
999px

Console