Skip to content

Commit

Permalink
sdk: retire luxon dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
tsmbl committed Apr 3, 2024
1 parent e0a2899 commit 6baf1ed
Show file tree
Hide file tree
Showing 22 changed files with 126 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Duration } from 'luxon';
import type { Token } from '@dialectlabs/sdk';
import {
bytesToBase64,
Expand All @@ -7,11 +6,11 @@ import {
} from '@dialectlabs/sdk';

export class AptosEd25519PayloadTokenGenerator extends TokenGenerator {
override async generate(ttl: Duration): Promise<Token> {
override async generate(ttlSeconds: number): Promise<Token> {
const header = this.header();
const base64Header = jsonStringifyToBase64(header);

const body = this.body(ttl);
const body = this.body(ttlSeconds);
const base64Body = jsonStringifyToBase64(body);

const { signature, base64Signature, signedPayload } = await this.sign(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Duration } from 'luxon';
import { DialectAptosWalletAdapterWrapper } from '../../../src/wallet-adapter/dialect-aptos-wallet-adapter-wrapper';
import {
AptosEd25519PayloadTokenSigner,
Expand Down Expand Up @@ -35,7 +34,7 @@ describe('aptos ed25519 payload token tests', () => {
test('when not expired validation returns true', async () => {
// when
const token = await authenticationFacade.generateToken(
Duration.fromObject({ seconds: 100 }),
100,
);
// then
const isValid = authenticationFacade.isValid(token);
Expand All @@ -48,7 +47,7 @@ describe('aptos ed25519 payload token tests', () => {
test('when expired validation returns false', async () => {
// when
const token = await authenticationFacade.generateToken(
Duration.fromObject({ seconds: -100 }),
-100,
);
// then
const isValid = authenticationFacade.isValid(token);
Expand Down Expand Up @@ -79,7 +78,7 @@ describe('aptos ed25519 payload token tests', () => {
).get();
// when
const token = await authenticationFacade.generateToken(
Duration.fromObject({ seconds: 100 }),
100,
);
// then
expect(token.body.sub).toBe(sub);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Duration } from 'luxon';
import { DialectAptosWalletAdapterWrapper } from '../../../src/wallet-adapter/dialect-aptos-wallet-adapter-wrapper';
import {
AptosEd25519TokenSigner,
Expand Down Expand Up @@ -30,7 +29,7 @@ describe('aptos ed25519 token tests', () => {
test('when not expired validation returns true', async () => {
// when
const token = await authenticationFacade.generateToken(
Duration.fromObject({ seconds: 100 }),
100,
);
// then
const isValid = authenticationFacade.isValid(token);
Expand All @@ -43,7 +42,7 @@ describe('aptos ed25519 token tests', () => {
test('when expired validation returns false', async () => {
// when
const token = await authenticationFacade.generateToken(
Duration.fromObject({ seconds: -100 }),
-100,
);
// then
const isValid = authenticationFacade.isValid(token);
Expand All @@ -56,7 +55,7 @@ describe('aptos ed25519 token tests', () => {
test('when sub compromised returns false', async () => {
// when
const token = await authenticationFacade.generateToken(
Duration.fromObject({ minutes: 5 }),
5 * 60,
);
const isValid = authenticationFacade.isValid(token);
expect(isValid).toBeTruthy();
Expand All @@ -80,7 +79,7 @@ describe('aptos ed25519 token tests', () => {
test('when sub jwk compromised returns false', async () => {
// when
const token = await authenticationFacade.generateToken(
Duration.fromObject({ minutes: 5 }),
5 * 60
);
const isValid = authenticationFacade.isValid(token);
expect(isValid).toBeTruthy();
Expand All @@ -104,7 +103,7 @@ describe('aptos ed25519 token tests', () => {
test('when exp compromised returns false', async () => {
// when
const token = await authenticationFacade.generateToken(
Duration.fromObject({ minutes: 5 }),
5 * 60
);
const isValid = authenticationFacade.isValid(token);
expect(isValid).toBeTruthy();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DialectEvmWalletAdapterWrapper } from '../../../src/wallet-adapter/dialect-evm-wallet-adapter-wrapper';
import { Duration } from 'luxon';
import type { AuthenticationFacade, TokenBody } from '@dialectlabs/sdk';
import { NodeDialectEvmWalletAdapter } from '../../../src/wallet-adapter/node-evm-wallet-adapter';
import {
Expand Down Expand Up @@ -27,7 +26,7 @@ describe('evm ed25519 token tests', () => {

test('when not expired validation returns true', async () => {
const token = await authenticationFacade.generateToken(
Duration.fromObject({ seconds: 10000 }),
10000,
);
const isValid = authenticationFacade.isValid(token);
expect(isValid).toBeTruthy();
Expand All @@ -39,7 +38,7 @@ describe('evm ed25519 token tests', () => {
test('when expired validation returns false', async () => {
// when
const token = await authenticationFacade.generateToken(
Duration.fromObject({ seconds: -100 }),
-100 ,
);
// then
const isValid = authenticationFacade.isValid(token);
Expand All @@ -52,7 +51,7 @@ describe('evm ed25519 token tests', () => {
test('when sub compromised returns false', async () => {
// when
const token = await authenticationFacade.generateToken(
Duration.fromObject({ minutes: 5 }),
5 * 60,
);
const isValid = authenticationFacade.isValid(token);
expect(isValid).toBeTruthy();
Expand All @@ -78,7 +77,8 @@ describe('evm ed25519 token tests', () => {
test('when sub jwk compromised returns false', async () => {
// when
const token = await authenticationFacade.generateToken(
Duration.fromObject({ minutes: 5 }),
5 * 60,

);
const isValid = authenticationFacade.isValid(token);
expect(isValid).toBeTruthy();
Expand All @@ -104,7 +104,7 @@ describe('evm ed25519 token tests', () => {
test('when exp compromised returns false', async () => {
// when
const token = await authenticationFacade.generateToken(
Duration.fromObject({ minutes: 5 }),
5 * 60,
);
const isValid = authenticationFacade.isValid(token);
expect(isValid).toBeTruthy();
Expand Down
4 changes: 4 additions & 0 deletions packages/blockchain-sdk-solana/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [UNRELEASED]

## [1.1.1] - 2024-04-03

- chore: retire luxon
-
## [1.1.0]

- feat: retire on-chain messaging api
Expand Down
54 changes: 50 additions & 4 deletions packages/blockchain-sdk-solana/examples/e2e-dapp-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,58 @@ import {
const sdk: DialectSdk<Solana> = Dialect.sdk(
{
environment: 'production',
dialectCloud: {}
},
SolanaSdkFactory.create({
wallet: NodeDialectSolanaWalletAdapter.create(), // don't forget to set DIALECT_SDK_CREDENTIALS env var to dapp wallet private key
}),
);

const dapp = await sdk.dapps.find();

if (!dapp) {
console.error('Dapp not found. Please register a dapp first.');
process.exit(1);
}
await dapp.messages.send({
title: 'New notification',
message: 'Hello, from the dialect sdk example!',
actions: [
{
label: 'Open Dialect',
url: 'https://dialect.io',
},
],
});

console.log(`Server wallet address: ${sdk.wallet.address}`);

async function main() {
// First, we register the sending keypair as a "dapp" (this is any project that wants to
// have users subscribe to receive notifications).
const dapp = await getOrRegisterDapp();
const sdk: DialectSdk<Solana> = Dialect.sdk(
{
environment: 'production',
},
SolanaSdkFactory.create({
wallet: NodeDialectSolanaWalletAdapter.create(), // don't forget to set DIALECT_SDK_CREDENTIALS env var to dapp wallet private key
}),
);

const dapp = await sdk.dapps.find();

if (!dapp) {
console.error('Dapp not found. Please register a dapp first.');
process.exit(1);
}
await dapp.messages.send({
title: 'New notification',
message: 'Hello, from the dialect sdk example!',
actions: [
{
label: 'Open Dialect',
url: 'https://dialect.io',
},
],
});

console.log(`Dapp id: ${dapp.id}`);

Expand All @@ -50,6 +90,12 @@ async function main() {
.send({
title: 'New notification',
message: message.toString(),
actions: [
{
label: 'Open Dialect',
url: 'https://dialect.io',
},
],
})
.catch((e) => console.error(e));
}
Expand Down Expand Up @@ -79,5 +125,5 @@ async function getOrRegisterDapp() {

return createdDapp;
}
return dapp.messages.send({});
return dapp;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
NodeDialectSolanaWalletAdapter,
SolanaEd25519AuthenticationFacadeFactory,
} from '@dialectlabs/blockchain-sdk-solana';
import { Duration } from 'luxon';

const adapter = NodeDialectSolanaWalletAdapter.create();
const walletAdapter = DialectSolanaWalletAdapterWrapper.create(adapter);
Expand All @@ -16,8 +15,6 @@ const walletAdapter = DialectSolanaWalletAdapterWrapper.create(adapter);
const authenticationFacade = new SolanaEd25519AuthenticationFacadeFactory(
signer,
).get();
const token = await authenticationFacade.generateToken(
Duration.fromObject({ minutes: 120 }),
);
const token = await authenticationFacade.generateToken(120 * 60);
console.log(token.rawValue);
})();
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ import {
message: 'Hello, world',
});

// Broadcast with metadata
await dapp.messages.send({
title: 'Hello',
message: 'Hello, world',
// Actions will be displayed in the notification as buttons, e.g. "Demo CTA" button will open "https://dialect.io" in the browser
// NB: Only one action is supported at the moment
actions: [{ label: 'Demo CTA', url: 'https://dialect.io' }],
});

// Unicast, but only to wallet channel (Dialect, Solflare or Step inboxes)
await dapp.messages.send({
title: 'Hello, dialectooooor',
Expand Down
2 changes: 1 addition & 1 deletion packages/blockchain-sdk-solana/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dialectlabs/blockchain-sdk-solana",
"version": "1.1.0",
"version": "1.1.1",
"repository": "[email protected]:dialectlabs/sdk.git",
"author": "dialectlabs",
"license": "Apache-2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Duration } from 'luxon';
import type { Token } from '@dialectlabs/sdk';
import {
bytesToBase64,
Expand All @@ -7,11 +6,11 @@ import {
} from '@dialectlabs/sdk';

export class SolanaTxTokenGenerator extends TokenGenerator {
override async generate(ttl: Duration): Promise<Token> {
override async generate(ttlSeconds: number): Promise<Token> {
const header = this.header();
const base64Header = jsonStringifyToBase64(header);

const body = this.body(ttl);
const body = this.body(ttlSeconds);
const base64Body = jsonStringifyToBase64(body);

const { signature, base64Signature, signedPayload } = await this.sign(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Keypair } from '@solana/web3.js';
import { Duration } from 'luxon';
import { DialectSolanaWalletAdapterWrapper } from '../../../src/wallet-adapter/dialect-solana-wallet-adapter-wrapper';
import type {
AccountAddress,
Expand Down Expand Up @@ -36,7 +35,7 @@ describe('solana ed25519 token tests', () => {
test('when not expired validation returns true', async () => {
// when
const token = await authenticationFacade.generateToken(
Duration.fromObject({ seconds: 100 }),
100
);
// then
const isValid = authenticationFacade.isValid(token);
Expand All @@ -49,7 +48,7 @@ describe('solana ed25519 token tests', () => {
test('when expired validation returns false', async () => {
// when
const token = await authenticationFacade.generateToken(
Duration.fromObject({ seconds: -100 }),
-100
);
// then
const isValid = authenticationFacade.isValid(token);
Expand All @@ -62,7 +61,7 @@ describe('solana ed25519 token tests', () => {
test('when sub compromised returns false', async () => {
// when
const token = await authenticationFacade.generateToken(
Duration.fromObject({ minutes: 5 }),
5 * 60
);
const isValid = authenticationFacade.isValid(token);
expect(isValid).toBeTruthy();
Expand All @@ -86,7 +85,8 @@ describe('solana ed25519 token tests', () => {
test('when exp compromised returns false', async () => {
// when
const token = await authenticationFacade.generateToken(
Duration.fromObject({ minutes: 5 }),
5 * 60

);
const isValid = authenticationFacade.isValid(token);
expect(isValid).toBeTruthy();
Expand Down Expand Up @@ -115,7 +115,7 @@ describe('solana ed25519 token tests', () => {
).get();
// when
const token = await authenticationFacade.generateToken(
Duration.fromObject({ seconds: 100 }),
100
);
// then
expect(token.body.sub).toBe(wallet.publicKey.toString());
Expand All @@ -137,7 +137,7 @@ describe('solana ed25519 token tests', () => {
).get();
// when
const token = await authenticationFacade.generateToken(
Duration.fromObject({ seconds: 100 }),
100
);
// then
expect(token.body.sub).toBe(subjectPublicKey.toString());
Expand All @@ -161,7 +161,7 @@ describe('solana ed25519 token tests', () => {
).get();
// when
const token = await authenticationFacade.generateToken(
Duration.fromObject({ seconds: 100 }),
100
);
// then
expect(token.body.sub).toBe(wallet.publicKey.toString());
Expand Down
Loading

0 comments on commit 6baf1ed

Please sign in to comment.