Skip to content
This repository was archived by the owner on Feb 21, 2024. It is now read-only.
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
68 changes: 24 additions & 44 deletions frontend/sdk/package-lock.json

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

2 changes: 1 addition & 1 deletion frontend/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@code/sdk-elements",
"private": true,
"version": "0.1.0",
"version": "1.1.0",
"main": "./dist/code.umd.js",
"module": "./dist/code.mjs",
"typings": "./dist/index.d.ts",
Expand Down
66 changes: 51 additions & 15 deletions frontend/sdk/src/utils/payment-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useConfig } from '../config';
import { Kik } from './';

import { EventChannel, InternalCardEvents, InternalEvents } from "@code-wallet/events";
import { PaymentRequestIntent, decode, encode } from "@code-wallet/library";
import { PaymentRequestIntent, decode, encode, PublicKey } from "@code-wallet/library";
import * as proto from '@code-wallet/rpc';

class PaymentRequest {
Expand Down Expand Up @@ -113,16 +113,52 @@ class PaymentRequest {
await this.listen();
}

private async getStatus() : Promise<proto.GetStatusResponse | undefined> {
const config = useConfig();
const getStatus = await proto.RpcStream.createUnaryMethod(proto.MicroPayment, "getStatus", config.wsPath());
try {
const intentId = PublicKey.fromBase58(this.intent.getIntentId());
const res = await getStatus(new proto.GetStatusRequest({
intentId: {
value: intentId.toBuffer(),
},
}));
return res;
} catch (error) {
if (this.emitter) {
this.emitter.emit("error", error as any);
}
}
}

private async sendRequest() : Promise<proto.SendMessageResponse | undefined> {
const config = useConfig();
const req = await this.toProto();
const msgSend = await proto.RpcStream.createUnaryMethod(proto.Messaging, "sendMessage", config.wsPath());
try {
const res = await msgSend(req);
if (res.result == proto.SendMessageResponse_Result.OK) {
return res;
} else {
if (this.emitter) {
this.emitter.emit("error", { message: "Failed to send message" });
}
}
} catch (error) {
if (this.emitter) {
this.emitter.emit("error", error as any);
}
}
}

private async listen() {
if (!this.emitter) { return; }

const { rendezvousKeypair } = this.intent;
const config = useConfig();
const req = await this.toProto();

await this.generateKikCode();

const msgSend = await proto.RpcStream.createUnaryMethod(proto.Messaging, "sendMessage", config.wsPath());
const msgStream = await proto.RpcStream.create(proto.Messaging, "openMessageStream", config.wsPath(), {
onClose: () => {
if (this.emitter) {
Expand All @@ -136,21 +172,21 @@ class PaymentRequest {
},
});

try {
const res = await msgSend(req);
if (res.result != proto.SendMessageResponse_Result.OK) {
if (this.emitter) {
this.emitter.emit("error", { message: "Failed to send message" });
}
return;
}
} catch (error) {
if (this.emitter) {
this.emitter.emit("error", error as any);
// Get the status of the intent (in case the server already requested it)
const status = await this.getStatus();
if (!status) {
return; // Something went wrong, don't continue
}

// If the intent does not exist, send the initial request here
if (!status.exists) {
const res = await this.sendRequest();
if (!res) {
return; // Something went wrong, don't continue
}
return;
}

// Open the message stream
msgStream.write(new proto.OpenMessageStreamRequest({
rendezvousKey: new proto.RendezvousKey({
value: rendezvousKeypair.publicKey,
Expand Down