summaryrefslogtreecommitdiff
path: root/impls/monero.ts/src/unsigned_transaction.ts
blob: c5d8ed7b51cec2b45f3b154046d1d9651a821a09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { fns } from "./bindings.ts";
import { C_SEPARATOR, CString, maybeMultipleStrings, readCString } from "./utils.ts";

export type UnsignedTransactionPtr = Deno.PointerObject<"pendingTransaction">;

export class UnsignedTransaction<MultDest extends boolean = false> {
  #ptr: UnsignedTransactionPtr;

  #amount!: string | string[] | null;
  #fee!: string | string[] | null;
  #txCount!: bigint;
  #paymentId!: string | null;
  #recipientAddress!: string | string[] | null;

  constructor(ptr: UnsignedTransactionPtr) {
    this.#ptr = ptr;
  }

  async status(): Promise<number> {
    return await fns.UnsignedTransaction_status(this.#ptr);
  }

  async errorString(): Promise<string | null> {
    return await readCString(await fns.UnsignedTransaction_errorString(this.#ptr));
  }

  static async new(ptr: UnsignedTransactionPtr): Promise<UnsignedTransaction> {
    const instance = new UnsignedTransaction(ptr);

    const [amount, paymentId, fee, txCount, recipientAddress] = await Promise.all([
      fns.UnsignedTransaction_amount(ptr, C_SEPARATOR).then(readCString),
      fns.UnsignedTransaction_paymentId(ptr, C_SEPARATOR).then(readCString),
      fns.UnsignedTransaction_fee(ptr, C_SEPARATOR).then(readCString),
      fns.UnsignedTransaction_txCount(ptr),
      fns.UnsignedTransaction_recipientAddress(ptr, C_SEPARATOR).then(readCString),
    ]);

    instance.#amount = maybeMultipleStrings(amount);
    instance.#fee = maybeMultipleStrings(fee);
    instance.#recipientAddress = maybeMultipleStrings(recipientAddress);
    instance.#txCount = txCount;
    instance.#paymentId = paymentId;

    return instance;
  }

  get amount(): string | string[] | null {
    return this.#amount;
  }

  get fee(): string | string[] | null {
    return this.#fee;
  }

  get txCount(): bigint {
    return this.#txCount;
  }

  get paymentId(): string | null {
    return this.#paymentId;
  }

  get recipientAddress(): string | string[] | null {
    return this.#recipientAddress;
  }

  async sign(signedFileName: string): Promise<boolean> {
    return await fns.UnsignedTransaction_sign(this.#ptr, CString(signedFileName));
  }

  async signUR(maxFragmentLength: number): Promise<string | null> {
    const signUR = fns.UnsignedTransaction_signUR;
    if (!signUR) return null;

    return await readCString(
      await signUR(this.#ptr, maxFragmentLength),
    );
  }
}