Skip to content

Commit

Permalink
Merge pull request #239 from streamflow-finance/fix/migrate-crypto-to…
Browse files Browse the repository at this point in the history
…-web-std-apis

Migrate crypto to web std apis
  • Loading branch information
RolginRoman authored Nov 29, 2024
2 parents 2f0a916 + d8aa82e commit 4313879
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 39 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"packages": [
"packages/*"
],
"version": "7.1.0-alpha.2",
"version": "7.1.0",
"$schema": "node_modules/lerna/schemas/lerna-schema.json"
}
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@streamflow/common",
"version": "7.1.0-alpha.2",
"version": "7.1.0",
"description": "Common utilities and types used by streamflow packages.",
"homepage": "https://github.com/streamflow-finance/js-sdk/",
"main": "./dist/esm/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/distributor/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@streamflow/distributor",
"version": "7.1.0-alpha.2",
"version": "7.1.0",
"description": "JavaScript SDK to interact with Streamflow Airdrop protocol.",
"homepage": "https://github.com/streamflow-finance/js-sdk/",
"main": "dist/esm/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@streamflow/eslint-config",
"version": "7.1.0-alpha.2",
"version": "7.1.0",
"license": "ISC",
"main": "index.js",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion packages/launchpad/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@streamflow/launchpad",
"version": "7.1.0-alpha.2",
"version": "7.1.0",
"description": "JavaScript SDK to interact with Streamflow Launchpad protocol.",
"homepage": "https://github.com/streamflow-finance/js-sdk/",
"main": "dist/esm/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/staking/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@streamflow/staking",
"version": "7.1.0-alpha.2",
"version": "7.1.0",
"description": "JavaScript SDK to interact with Streamflow Staking protocol.",
"homepage": "https://github.com/streamflow-finance/js-sdk/",
"main": "dist/esm/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/stream/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@streamflow/stream",
"version": "7.1.0-alpha.2",
"version": "7.1.0",
"description": "JavaScript SDK to interact with Streamflow protocol.",
"homepage": "https://github.com/streamflow-finance/js-sdk/",
"main": "./dist/esm/index.js",
Expand Down
14 changes: 7 additions & 7 deletions packages/stream/solana/StreamClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ export class SolanaStreamClient extends BaseStreamClient {
const partnerTokens = await ata(mintPublicKey, partnerPublicKey, tokenProgramId);

ixs.push(
createStreamInstruction(
await createStreamInstruction(
{
start: new BN(start),
depositedAmount,
Expand Down Expand Up @@ -539,7 +539,7 @@ export class SolanaStreamClient extends BaseStreamClient {
ixs.push(...(await prepareWrappedAccount(this.connection, sender.publicKey, totalAmount)));
}

const createInstruction = createUncheckedStreamInstruction(
const createInstruction = await createUncheckedStreamInstruction(
{
start: new BN(start),
depositedAmount,
Expand Down Expand Up @@ -935,7 +935,7 @@ export class SolanaStreamClient extends BaseStreamClient {

ixs.push(
...ataIx,
withdrawStreamInstruction(amount, this.programId, {
await withdrawStreamInstruction(amount, this.programId, {
partner,
partnerTokens,
mint,
Expand Down Expand Up @@ -1079,7 +1079,7 @@ export class SolanaStreamClient extends BaseStreamClient {

ixs.push(
...ixsAta,
cancelStreamInstruction(this.programId, {
await cancelStreamInstruction(this.programId, {
sender,
senderTokens,
recipient,
Expand Down Expand Up @@ -1152,7 +1152,7 @@ export class SolanaStreamClient extends BaseStreamClient {
const newRecipientTokens = await ata(mint, newRecipientPublicKey, tokenProgramId);

ixs.push(
transferStreamInstruction(this.programId, {
await transferStreamInstruction(this.programId, {
authority: invoker.publicKey,
newRecipient: newRecipientPublicKey,
newRecipientTokens,
Expand Down Expand Up @@ -1220,7 +1220,7 @@ export class SolanaStreamClient extends BaseStreamClient {
}

ixs.push(
topupStreamInstruction(amount, this.programId, {
await topupStreamInstruction(amount, this.programId, {
sender: invoker.publicKey,
senderTokens,
metadata: streamPublicKey,
Expand Down Expand Up @@ -1434,7 +1434,7 @@ export class SolanaStreamClient extends BaseStreamClient {
computeLimit,
});
ixs.push(
updateStreamInstruction(data, this.programId, {
await updateStreamInstruction(data, this.programId, {
authority: invoker.publicKey,
metadata: streamPublicKey,
withdrawor: WITHDRAWOR_PUBLIC_KEY,
Expand Down
59 changes: 34 additions & 25 deletions packages/stream/solana/instructions.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Buffer } from "buffer";
import { PublicKey, TransactionInstruction } from "@solana/web3.js";
import BN from "bn.js";
import { createHash } from "node:crypto";

import * as Layout from "./layout.js";
import { IUpdateData } from "../common/types.js";

const hasher = () => createHash("sha256");
const sha256 = {
digest: (data: string): Uint8Array => new Uint8Array(hasher().update(data).digest()),
digest: async (data: string): Promise<Uint8Array> => {
const dataBuffer = new TextEncoder().encode(data);
const hashBuffer = await globalThis.crypto.subtle.digest("SHA-256", dataBuffer);
return new Uint8Array(hashBuffer);
},
};

interface CreateStreamData {
Expand Down Expand Up @@ -51,11 +53,11 @@ interface CreateStreamAccounts {
systemProgram: PublicKey;
}

export const createStreamInstruction = (
export const createStreamInstruction = async (
data: CreateStreamData,
programId: PublicKey,
accounts: CreateStreamAccounts,
): TransactionInstruction => {
): Promise<TransactionInstruction> => {
const keys = [
{ pubkey: accounts.sender, isSigner: true, isWritable: true },
{ pubkey: accounts.senderTokens, isSigner: false, isWritable: true },
Expand Down Expand Up @@ -116,7 +118,11 @@ export const createStreamInstruction = (
};
const encodeLength = Layout.createStreamLayout.encode(decodedData, bufferData);
bufferData = bufferData.slice(0, encodeLength);
bufferData = Buffer.concat([Buffer.from(sha256.digest("global:create")).slice(0, 8), bufferData, Buffer.alloc(10)]);
bufferData = Buffer.concat([
Buffer.from(await sha256.digest("global:create")).slice(0, 8),
bufferData,
Buffer.alloc(10),
]);

return new TransactionInstruction({
keys,
Expand Down Expand Up @@ -161,11 +167,11 @@ interface CreateUncheckedStreamAccounts {
systemProgram: PublicKey;
}

export const createUncheckedStreamInstruction = (
export const createUncheckedStreamInstruction = async (
data: CreateUncheckedStreamData,
programId: PublicKey,
accounts: CreateUncheckedStreamAccounts,
): TransactionInstruction => {
): Promise<TransactionInstruction> => {
const keys = [
{ pubkey: accounts.sender, isSigner: true, isWritable: true },
{ pubkey: accounts.senderTokens, isSigner: false, isWritable: true },
Expand Down Expand Up @@ -216,8 +222,8 @@ export const createUncheckedStreamInstruction = (
const encodeLength = Layout.createUncheckedStreamLayout.encode(decodedData, bufferData);
bufferData = bufferData.slice(0, encodeLength);
const digest = accounts.payer
? sha256.digest("global:create_unchecked_with_payer")
: sha256.digest("global:create_unchecked");
? await sha256.digest("global:create_unchecked_with_payer")
: await sha256.digest("global:create_unchecked");
bufferData = Buffer.concat([Buffer.from(digest).slice(0, 8), bufferData, Buffer.alloc(10)]);

return new TransactionInstruction({
Expand All @@ -241,7 +247,7 @@ interface WithdrawAccounts {
tokenProgram: PublicKey;
}

export const withdrawStreamInstruction = (
export const withdrawStreamInstruction = async (
amount: BN,
programId: PublicKey,
{
Expand All @@ -257,7 +263,7 @@ export const withdrawStreamInstruction = (
mint,
tokenProgram,
}: WithdrawAccounts,
): TransactionInstruction => {
): Promise<TransactionInstruction> => {
const keys = [
{ pubkey: authority, isSigner: true, isWritable: true },
{ pubkey: recipient, isSigner: false, isWritable: true },
Expand All @@ -280,7 +286,7 @@ export const withdrawStreamInstruction = (
const decodedData = { amount: amount.toArrayLike(Buffer, "le", 8) };
const encodeLength = Layout.withdrawStreamLayout.encode(decodedData, data);
data = data.slice(0, encodeLength);
data = Buffer.concat([Buffer.from(sha256.digest("global:withdraw")).slice(0, 8), data, Buffer.alloc(10)]);
data = Buffer.concat([Buffer.from(await sha256.digest("global:withdraw")).slice(0, 8), data, Buffer.alloc(10)]);

return new TransactionInstruction({
keys,
Expand All @@ -295,11 +301,11 @@ interface UpdateAccounts {
withdrawor: PublicKey;
systemProgram: PublicKey;
}
export const updateStreamInstruction = (
export const updateStreamInstruction = async (
params: IUpdateData,
programId: PublicKey,
{ authority, metadata, withdrawor, systemProgram }: UpdateAccounts,
): TransactionInstruction => {
): Promise<TransactionInstruction> => {
const keys = [
{ pubkey: authority, isSigner: true, isWritable: true },
{ pubkey: metadata, isSigner: false, isWritable: true },
Expand All @@ -315,7 +321,7 @@ export const updateStreamInstruction = (
};
const encodeLength = Layout.encodeUpdateStream(decodedData, data);
data = data.slice(0, encodeLength);
data = Buffer.concat([Buffer.from(sha256.digest("global:update")).slice(0, 8), data, Buffer.alloc(20)]);
data = Buffer.concat([Buffer.from(await sha256.digest("global:update")).slice(0, 8), data, Buffer.alloc(20)]);
return new TransactionInstruction({
keys: keys,
programId: programId,
Expand All @@ -339,7 +345,7 @@ interface CancelAccounts {
tokenProgram: PublicKey;
}

export const cancelStreamInstruction = (
export const cancelStreamInstruction = async (
programId: PublicKey,
{
authority,
Expand All @@ -356,7 +362,7 @@ export const cancelStreamInstruction = (
mint,
tokenProgram,
}: CancelAccounts,
): TransactionInstruction => {
): Promise<TransactionInstruction> => {
const keys = [
{ pubkey: authority, isSigner: true, isWritable: false },
{ pubkey: sender, isSigner: false, isWritable: true },
Expand All @@ -377,7 +383,7 @@ export const cancelStreamInstruction = (
{ pubkey: tokenProgram, isSigner: false, isWritable: false },
];

const data = Buffer.concat([Buffer.from(sha256.digest("global:cancel")).slice(0, 8), Buffer.alloc(10)]);
const data = Buffer.concat([Buffer.from(await sha256.digest("global:cancel")).slice(0, 8), Buffer.alloc(10)]);

return new TransactionInstruction({
keys,
Expand All @@ -398,7 +404,7 @@ interface TransferAccounts {
systemProgram: PublicKey;
}

export const transferStreamInstruction = (
export const transferStreamInstruction = async (
programId: PublicKey,
{
authority,
Expand All @@ -411,7 +417,7 @@ export const transferStreamInstruction = (
associatedTokenProgram,
systemProgram,
}: TransferAccounts,
): TransactionInstruction => {
): Promise<TransactionInstruction> => {
const keys = [
{ pubkey: authority, isSigner: true, isWritable: true },
{ pubkey: newRecipient, isSigner: false, isWritable: true },
Expand All @@ -424,7 +430,10 @@ export const transferStreamInstruction = (
{ pubkey: systemProgram, isSigner: false, isWritable: false },
];

const data = Buffer.concat([Buffer.from(sha256.digest("global:transfer_recipient")).slice(0, 8), Buffer.alloc(10)]);
const data = Buffer.concat([
Buffer.from(await sha256.digest("global:transfer_recipient")).slice(0, 8),
Buffer.alloc(10),
]);

return new TransactionInstruction({
keys,
Expand All @@ -448,7 +457,7 @@ interface TopupAccounts {
systemProgram: PublicKey;
}

export const topupStreamInstruction = (
export const topupStreamInstruction = async (
amount: BN,
programId: PublicKey,
{
Expand All @@ -465,7 +474,7 @@ export const topupStreamInstruction = (
withdrawor,
systemProgram,
}: TopupAccounts,
): TransactionInstruction => {
): Promise<TransactionInstruction> => {
const keys = [
{ pubkey: sender, isSigner: true, isWritable: true },
{ pubkey: senderTokens, isSigner: false, isWritable: true },
Expand All @@ -490,7 +499,7 @@ export const topupStreamInstruction = (

const encodeLength = Layout.topupStreamLayout.encode(decodedData, data);
data = data.slice(0, encodeLength);
data = Buffer.concat([Buffer.from(sha256.digest("global:topup")).slice(0, 8), data, Buffer.alloc(10)]);
data = Buffer.concat([Buffer.from(await sha256.digest("global:topup")).slice(0, 8), data, Buffer.alloc(10)]);

return new TransactionInstruction({
keys,
Expand Down

0 comments on commit 4313879

Please sign in to comment.