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
10 changes: 5 additions & 5 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/library/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@code-wallet/library",
"version": "1.1.1",
"version": "1.2.1",
"license": "MIT",
"repository": {
"type": "git",
Expand All @@ -22,7 +22,7 @@
"maintained node versions"
],
"dependencies": {
"@code-wallet/rpc": "^1.1.0",
"@code-wallet/rpc": "^1.2.0",
"@noble/curves": "^1.2.0",
"@noble/hashes": "^1.3.0",
"bs58": "^5.0.0",
Expand Down
8 changes: 6 additions & 2 deletions packages/library/src/elements/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
ErrLoginRequired,
ErrLoginDomainRequired,
ErrLoginVerifierRequired,
ErrNotImplemented,
ErrInvalidValue,
} from '../errors';
import { PublicKey } from '../keys';
Expand Down Expand Up @@ -113,7 +112,12 @@ function validateElementOptions(intent: ElementOptions) {

switch (intent.mode) {
case 'login':
throw ErrNotImplemented(); // TODO: implement login (soon)
validateLoginRequestOptions(intent);

if (intent.signers) {
validateSigners(intent);
}

break;
case 'payment':
validatePaymentRequestOptions(intent);
Expand Down
168 changes: 168 additions & 0 deletions packages/library/src/intents/LoginRequestIntent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import * as proto from '@code-wallet/rpc';
import { IdempotencyKey } from '../idempotency';
import { Keypair, PublicKey } from '../keys';
import { CodePayload, CodeKind } from '../payload';
import { Intent, SignedIntent } from '../intent';
import {
ErrLoginDomainRequired,
ErrLoginRequired,
ErrLoginVerifierRequired,
ErrUnexpectedError
} from '../errors';
import { generateRendezvousKeypair } from '../rendezvous';
import { validateElementOptions } from '../elements/validate';
import { ElementOptions } from '../elements/options';

/**
* Represents a login request and provides methods to construct, validate, and sign the request.
*/
class LoginRequestIntent implements Intent {
domain: string;
verifier: PublicKey;
signer?: Keypair;

options: ElementOptions;
nonce: IdempotencyKey;

rendezvousPayload: CodePayload;
rendezvousKeypair: Keypair;

/**
* Constructs a new PaymentRequestIntent instance.
*
* @param opt - The payment request options.
*/
constructor(opt: ElementOptions) {
this.options = {
...opt,
};

this.validate();

const { signers } = opt;
const { domain, verifier } = opt.login!;

this.domain = domain;
this.verifier = PublicKey.fromBase58(verifier);

if (signers) {
this.signer = signers.find((k) => k.getPublicKey().toBase58() === verifier)
}

// Create an 11 byte buffer from idempotencyKey if provided, otherwise generate a random nonce
if (this.options.idempotencyKey) {
this.nonce = IdempotencyKey.fromSeed(this.options.idempotencyKey);
} else if (this.options.clientSecret) {
this.nonce = IdempotencyKey.fromClientSecret(this.options.clientSecret);
} else {
this.nonce = IdempotencyKey.generate();
}

// See payload encoding for CodeKind.RequestPayment
const kind = CodeKind.RequestLogin;
const nonce = this.nonce.value;

// Create a rendezvous payload and derive a keypair from it
this.rendezvousPayload = new CodePayload({ kind, nonce, });
this.rendezvousKeypair = generateRendezvousKeypair(this.rendezvousPayload);
}

/**
* Validates the payment request options.
*/
validate() {
validateElementOptions(this.options);

if (!this.options.login) {
throw ErrLoginRequired();
}

if (!this.options.login.domain) {
throw ErrLoginDomainRequired();
}

if (!this.options.login.verifier) {
throw ErrLoginVerifierRequired();
}
}

/**
* Converts the payment request intent to its protobuf representation.
*
* @returns The protobuf representation of the payment request intent.
*/
toProto() : proto.Message {
const msg = new proto.RequestToLogin({
domain: {
value: this.domain,
},
verifier: {
value: this.verifier.toBuffer(),
},
rendezvousKey: {
value: this.rendezvousKeypair.getPublicKey().toBuffer(),
},
});

return new proto.Message({
kind: {
case: 'requestToLogin',
value: msg,
}
});
}

/**
* Signs the payment request intent.
*
* @returns A signed intent containing the message, intent, and signature.
*/
sign(): SignedIntent {
if (!this.signer) {
throw ErrLoginVerifierRequired();
}

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

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

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

return {
message,
intent,
signature,
}
}

/**
* Retrieves the client secret.
*
* @returns The client secret as a string.
*/
getClientSecret(): string {
return this.nonce.toString();
}

/**
* Retrieves the intent ID.
*
* @returns The intent ID as a Base58 encoded string.
*/
getIntentId(): string {
return this.rendezvousKeypair.getPublicKey().toBase58();
}
}

export {
LoginRequestIntent,
}
2 changes: 1 addition & 1 deletion packages/library/src/intents/PaymentRequestIntent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class PaymentRequestIntent implements Intent {
const nonce = this.nonce.value;

// Create a rendezvous payload and derive a keypair from it
this.rendezvousPayload = new CodePayload(kind, amount, nonce, this.options.currency);
this.rendezvousPayload = new CodePayload({kind, amount, nonce, currency: this.options.currency});
this.rendezvousKeypair = generateRendezvousKeypair(this.rendezvousPayload);
}

Expand Down
3 changes: 2 additions & 1 deletion packages/library/src/intents/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './PaymentRequestIntent';
export * from './PaymentRequestWithLoginIntent';
export * from './PaymentRequestWithLoginIntent';
export * from './LoginRequestIntent';
Loading