Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/4-minimal-with-webhook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"type": "module",
"scripts": {
"start": "node index.js",
"start": "node server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
Expand Down
2 changes: 1 addition & 1 deletion examples/4-minimal-with-webhook/views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Code SDK - Example/3-minimal-with-webhook</title>
<title>Code SDK - Example/4-minimal-with-webhook</title>
</head>

<body>
Expand Down
22 changes: 22 additions & 0 deletions examples/6-minimal-with-login/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "6-minimal-with-login",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"start": "node server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@code-wallet/client": "^1.1.0",
"body-parser": "^1.20.2",
"ejs": "^3.1.9",
"express": "^4.18.2"
},
"devDependencies": {
"@types/express": "^4.17.17"
}
}
70 changes: 70 additions & 0 deletions examples/6-minimal-with-login/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import * as code from "@code-wallet/client";
import { Keypair } from "@code-wallet/library";
import express from "express";

const port = process.env.PORT || 3000;
const hostname = process.env.HOSTNAME || 'example.com';
const app = express();
const verifier = Keypair.generate();

// Set the view engine to ejs
app.set('view engine', 'ejs');

// index page (with the payment button)
app.get('/', function(req, res) {
res.render('index');
});

// This is a page that the user will be redirected to once the payment is made.
app.get('/success/:id', async (req, res) => {
// Get the payment intent id from the URL
const intent = req.params.id;

// Get the status of the payment intent and the user's public key
const status = await code.paymentIntents.getStatus({ intent });

// Render the success page with the intent id and status
res.render('success', { intent, status });
});

// This is a JSON file that Code will look for when verifying your domain. It
// should be publicly accessible at the root of your domain. For example:
// https://example.com/.well-known/code-payments.json
app.get('/.well-known/code-payments.json', (req, res) => {
res.json({ "public_keys": [verifier.getPublicKey().toBase58()] });
});

// Create a payment intent. We're letting Code know that a payment is coming
// soon and we want to be notified once it's made.
app.post('/create-intent', async (req, res) => {

const { clientSecret, id } = await code.paymentIntents.create({
amount: 0.05,
currency: 'usd',
destination: 'E8otxw1CVX9bfyddKu3ZB3BVLa4VVF9J7CTPdnUwT9jR',

login: {
verifier: verifier.getPublicKey().toBase58(),

// Cannot be localhost or a subdomain. It must be a domain that you own
// and have access to. Code will verify that this domain is owned by you
// by looking for the .well-known/code-payments.json file.
domain: hostname,
},

signers: [ verifier ]
});

console.log('Created intent', id);

// The clientSecret value needs to be sent to the browser so that the browser
// can use it to setup a payment with this intent instance. The client will
// use the payment details along with this value to derive the same payment
// intent id on its end.
res.send({ clientSecret });
});

app.listen(port, () => {
console.log(`🚀 Minimal example listening on port ${port}`)
console.log(`http://localhost:${port}`)
});
46 changes: 46 additions & 0 deletions examples/6-minimal-with-login/views/index.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Code SDK - Example/6-minimal-with-login</title>
</head>

<body>
<div id="button-container"></div>

<script type="module">
import code from 'https://js.getcode.com/v1';

const hostname = window.location.protocol + "//" + window.location.host;
console.log(hostname);

const { button } = code.elements.create('button', {
currency: 'usd',
destination: 'E8otxw1CVX9bfyddKu3ZB3BVLa4VVF9J7CTPdnUwT9jR',
amount: 0.05,

confirmParams: {
success: { url: `${hostname}/success/{{INTENT_ID}}`, },
cancel: { url: `${hostname}/`, },
},
});

// Wait for the button to be clicked
button.on('invoke', async () => {

// Get a payment intent from our own server
const res = await fetch('/create-intent', { method: 'POST' });
const { clientSecret } = await res.json();

// Update the button with the new client secret so that our server
// can be notified once the payment is complete.
button.update({ clientSecret });
})

button.mount('#button-container');
</script>
</body>

</html>
17 changes: 17 additions & 0 deletions examples/6-minimal-with-login/views/success.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Code SDK - Example/6-minimal-with-login</title>
</head>

<body>
<h1>Payment Submitted</h1>
<div>Intent Id: <%= intent %></div>
<div>Status: <pre><%= status %></pre></div>
</body>

</html>
42 changes: 35 additions & 7 deletions examples/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@code-wallet/client",
"version": "1.1.0",
"version": "1.1.1",
"license": "MIT",
"repository": {
"type": "git",
Expand All @@ -22,7 +22,7 @@
"maintained node versions"
],
"dependencies": {
"@code-wallet/library": "^1.1.0",
"@code-wallet/library": "^1.1.1",
"bs58": "^5.0.0",
"buffer": "6.0.3"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/library/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@code-wallet/library",
"version": "1.1.0",
"version": "1.1.1",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions packages/library/src/elements/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
ErrNotImplemented,
ErrInvalidValue,
} from '../errors';
import { Keypair, PublicKey } from '../keys';
import { PublicKey } from '../keys';

/**
* Validates the properties of the given `ElementOptions` for intents.
Expand Down Expand Up @@ -96,7 +96,7 @@ function validateSigners(intent: ElementOptions) {
if (!intent.signers) { return; }

for (const signer of intent.signers) {
if (!(signer instanceof Keypair)) {
if (!signer.sign && typeof signer.sign !== 'function') {
throw ErrInvalidValue();
}
}
Expand Down
12 changes: 6 additions & 6 deletions packages/library/src/intents/PaymentRequestWithLoginIntent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,17 @@ class PaymentRequestWithLoginIntent extends PaymentRequestIntent {
throw ErrLoginVerifierRequired();
}

const msg = this.toProto();
const req = msg.kind.value as proto.RequestToReceiveBill;
if (!req) {
const envelope = this.toProto();
const msg = envelope.kind.value as proto.RequestToReceiveBill;
if (!msg) {
throw ErrUnexpectedError();
}

req.signature = new proto.Common.Signature({
value: this.signer.sign(req.toBinary()),
msg.signature = new proto.Common.Signature({
value: this.signer.sign(msg.toBinary()),
});

const sig = this.rendezvousKeypair.sign(msg.toBinary());
const sig = this.rendezvousKeypair.sign(envelope.toBinary());
const intent = this.rendezvousKeypair.getPublicKey().toBase58();
const message = msg.toBinary();
const signature = sig;
Expand Down
Loading