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 77cd2a6 commit 3674d18
Show file tree
Hide file tree
Showing 23 changed files with 219 additions and 91 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
33 changes: 33 additions & 0 deletions packages/blockchain-sdk-solana/examples/create-dapp-2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { BlockchainType, Dialect } from '../../sdk';
import { NodeDialectSolanaWalletAdapter, Solana, SolanaSdkFactory } from '..';
import { Keypair } from '@solana/web3.js';
import bs58 from 'bs58';

async function main() {
const dappSdk = Dialect.sdk<Solana>(
{
// environment: 'development',
environment: 'development',
},
SolanaSdkFactory.create({
wallet: new NodeDialectSolanaWalletAdapter(
Keypair.fromSecretKey(
new Uint8Array([
244, 13, 185, 49, 29, 25, 61, 3, 216, 244, 38, 117, 66, 91, 59, 109,
33, 187, 247, 207, 94, 224, 47, 109, 94, 88, 111, 19, 245, 96, 152,
158, 87, 102, 228, 178, 98, 12, 204, 105, 85, 77, 199, 40, 157, 149,
87, 116, 196, 136, 33, 174, 123, 117, 87, 81, 48, 255, 116, 67, 134,
14, 243, 204,
]),
),
),
}),
);

const created = await dappSdk.dapps.create({
name: 'Dialectooors dapp',
blockchainType: BlockchainType.SOLANA,
});
}

main().catch(console.error);
76 changes: 76 additions & 0 deletions packages/blockchain-sdk-solana/examples/create-dapp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { BlockchainType, Dialect } from '../../sdk';
import { NodeDialectSolanaWalletAdapter, Solana, SolanaSdkFactory } from '..';
import { Keypair } from '@solana/web3.js';
import bs58 from 'bs58';

async function main() {
const monitor = Dialect.sdk<Solana>(
{
// environment: 'development',
environment: 'development',
},
SolanaSdkFactory.create({
wallet: new NodeDialectSolanaWalletAdapter(
Keypair.fromSecretKey(
new Uint8Array([
56, 111, 31, 152, 56, 138, 122, 34, 150, 99, 14, 65, 41, 148, 28,
58, 223, 101, 10, 31, 12, 146, 175, 132, 212, 6, 19, 222, 92, 177,
36, 123, 65, 246, 202, 251, 197, 105, 179, 188, 65, 163, 200, 12,
235, 148, 133, 28, 213, 61, 233, 180, 235, 110, 52, 2, 94, 136, 193,
46, 225, 253, 48, 144,
]),
),
),
}),
);
const uint8Array = new Uint8Array([
248, 80, 183, 221, 66, 144, 97, 254, 75, 101, 186, 117, 33, 130, 91, 117,
214, 195, 11, 209, 227, 230, 64, 138, 128, 91, 90, 248, 47, 46, 201, 100, 4,
1, 253, 87, 233, 66, 232, 136, 133, 117, 185, 11, 43, 114, 187, 61, 35, 102,
45, 78, 9, 6, 60, 136, 233, 210, 41, 32, 18, 228, 241, 182,
]);

const s = bs58.encode(uint8Array);
console.log(s);
const monitor2 = Dialect.sdk<Solana>(
{
// environment: 'development',
environment: 'development',
},
SolanaSdkFactory.create({
wallet: new NodeDialectSolanaWalletAdapter(
Keypair.fromSecretKey(uint8Array),
),
}),
);

const monitorDapp = await monitor.dapps.find();
if (!monitorDapp) {
const created = await monitor.dapps.create({
name: 'MONITOR 1',
blockchainType: BlockchainType.SOLANA,
});
}

const monitorDapp2 = await monitor2.dapps.find();
if (!monitorDapp2) {
const created = await monitor2.dapps.create({
name: 'MONITOR 2',
blockchainType: BlockchainType.SOLANA,
});
}

await monitorDapp2!.messages.send({
title: 'Hello topic',
message: 'Hello from monitor 2 topic',
actions: [
{
label: 'авыф',
url: 'https://www.figma.com',
},
],
notificationTypeId: '9b153418-f804-498d-9ea3-08f62359032a',
});
}

main().catch(console.error);
46 changes: 43 additions & 3 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 Down
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);
})();
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
Loading

0 comments on commit 3674d18

Please sign in to comment.