js-base64#toUint8Array TypeScript Examples

The following examples show how to use js-base64#toUint8Array. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: monaco-persistence.ts    From netless-app with MIT License 6 votes vote down vote up
public constructor(
    public context: AppContext<NetlessAppMonacoAttributes>,
    public attrs: NetlessAppMonacoAttributes,
    public doc: Doc,
    public yText: Text
  ) {
    this.sideEffect = new SideEffectManager();

    this.textAttr = attrs.text;

    if (this.textAttr) {
      applyUpdate(this.doc, toUint8Array(this.textAttr), this);
    }

    this.sideEffect.add(() =>
      context.mobxUtils.autorun(() => {
        this.textAttr = attrs.text;
      })
    );

    this.setupYDocPersistence();
  }
Example #2
Source File: y-monaco.ts    From netless-app with MIT License 6 votes vote down vote up
private setupDocUpdate(): void {
    const displayer = this.context.getDisplayer();

    this.sideEffect.add(() => {
      const handleUpdate = (event: WhiteEvent) => {
        this.authorId = String(event.authorId);
        if (event.authorId !== displayer.observerId) {
          try {
            applyUpdate(this.doc, toUint8Array(event.payload), "_remote_edit_");
          } catch (e) {
            console.warn(e);
          }
        }
      };
      displayer.addMagixEventListener(this.MagixMonacoDocChannel, handleUpdate);
      return () => displayer.removeMagixEventListener(this.MagixMonacoDocChannel, handleUpdate);
    });

    this.sideEffect.add(() => {
      const handleUpdate = (update: Uint8Array, origin: unknown) => {
        if (origin !== "_remote_edit_" && this.context.getIsWritable()) {
          const room = this.context.getRoom();
          if (room) {
            this.authorId = String(displayer.observerId);
            room.dispatchMagixEvent(this.MagixMonacoDocChannel, fromUint8Array(update));
          }
        }
      };
      this.doc.on("update", handleUpdate);
      return () => this.doc.off("update", handleUpdate);
    });
  }
Example #3
Source File: fetchTransaction.ts    From solana-pay with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch a transaction from a Solana Pay transaction request link.
 *
 * @param connection - A connection to the cluster.
 * @param account - Account that may sign the transaction.
 * @param link - `link` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#link).
 * @param options - Options for `getRecentBlockhash`.
 *
 * @throws {FetchTransactionError}
 */
export async function fetchTransaction(
    connection: Connection,
    account: PublicKey,
    link: string | URL,
    { commitment }: { commitment?: Commitment } = {}
): Promise<Transaction> {
    const response = await fetch(String(link), {
        method: 'POST',
        mode: 'cors',
        cache: 'no-cache',
        credentials: 'omit',
        headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({ account }),
    });

    const json = await response.json();
    if (!json?.transaction) throw new FetchTransactionError('missing transaction');
    if (typeof json.transaction !== 'string') throw new FetchTransactionError('invalid transaction');

    const transaction = Transaction.from(toUint8Array(json.transaction));
    const { signatures, feePayer, recentBlockhash } = transaction;

    if (signatures.length) {
        if (!feePayer) throw new FetchTransactionError('missing fee payer');
        if (!feePayer.equals(signatures[0].publicKey)) throw new FetchTransactionError('invalid fee payer');
        if (!recentBlockhash) throw new FetchTransactionError('missing recent blockhash');

        // A valid signature for everything except `account` must be provided.
        const message = transaction.serializeMessage();
        for (const { signature, publicKey } of signatures) {
            if (signature) {
                if (!nacl.sign.detached.verify(message, signature, publicKey.toBuffer()))
                    throw new FetchTransactionError('invalid signature');
            } else if (publicKey.equals(account)) {
                // If the only signature expected is for `account`, ignore the recent blockhash in the transaction.
                if (signatures.length === 1) {
                    transaction.recentBlockhash = (await connection.getRecentBlockhash(commitment)).blockhash;
                }
            } else {
                throw new FetchTransactionError('missing signature');
            }
        }
    } else {
        // Ignore the fee payer and recent blockhash in the transaction and initialize them.
        transaction.feePayer = account;
        transaction.recentBlockhash = (await connection.getRecentBlockhash(commitment)).blockhash;
    }

    return transaction;
}