move amount and time function to util
This commit is contained in:
parent
43f5572779
commit
e90f1b4206
@ -35,6 +35,8 @@ import { Logger } from "./logging.js";
|
|||||||
import { secretbox } from "./nacl-fast.js";
|
import { secretbox } from "./nacl-fast.js";
|
||||||
import * as fflate from "fflate";
|
import * as fflate from "fflate";
|
||||||
import { canonicalJson } from "./helpers.js";
|
import { canonicalJson } from "./helpers.js";
|
||||||
|
import { TalerProtocolDuration, TalerProtocolTimestamp } from "./time.js";
|
||||||
|
import { AmountLike, Amounts } from "./amounts.js";
|
||||||
|
|
||||||
export type Flavor<T, FlavorT extends string> = T & {
|
export type Flavor<T, FlavorT extends string> = T & {
|
||||||
_flavor?: `taler.${FlavorT}`;
|
_flavor?: `taler.${FlavorT}`;
|
||||||
@ -963,6 +965,7 @@ export enum TalerSignaturePurpose {
|
|||||||
EXCHANGE_CONFIRM_RECOUP_REFRESH = 1041,
|
EXCHANGE_CONFIRM_RECOUP_REFRESH = 1041,
|
||||||
TALER_SIGNATURE_AML_DECISION = 1350,
|
TALER_SIGNATURE_AML_DECISION = 1350,
|
||||||
TALER_SIGNATURE_AML_QUERY = 1351,
|
TALER_SIGNATURE_AML_QUERY = 1351,
|
||||||
|
TALER_SIGNATURE_MASTER_AML_KEY = 1017,
|
||||||
ANASTASIS_POLICY_UPLOAD = 1400,
|
ANASTASIS_POLICY_UPLOAD = 1400,
|
||||||
ANASTASIS_POLICY_DOWNLOAD = 1401,
|
ANASTASIS_POLICY_DOWNLOAD = 1401,
|
||||||
SYNC_BACKUP_UPLOAD = 1450,
|
SYNC_BACKUP_UPLOAD = 1450,
|
||||||
@ -1546,3 +1549,61 @@ export async function decryptContractForDeposit(
|
|||||||
contractTerms: JSON.parse(contractTermsString),
|
contractTerms: JSON.parse(contractTermsString),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function amountToBuffer(amount: AmountLike): Uint8Array {
|
||||||
|
const amountJ = Amounts.jsonifyAmount(amount);
|
||||||
|
const buffer = new ArrayBuffer(8 + 4 + 12);
|
||||||
|
const dvbuf = new DataView(buffer);
|
||||||
|
const u8buf = new Uint8Array(buffer);
|
||||||
|
const curr = stringToBytes(amountJ.currency);
|
||||||
|
if (typeof dvbuf.setBigUint64 !== "undefined") {
|
||||||
|
dvbuf.setBigUint64(0, BigInt(amountJ.value));
|
||||||
|
} else {
|
||||||
|
const arr = bigint(amountJ.value).toArray(2 ** 8).value;
|
||||||
|
let offset = 8 - arr.length;
|
||||||
|
for (let i = 0; i < arr.length; i++) {
|
||||||
|
dvbuf.setUint8(offset++, arr[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dvbuf.setUint32(8, amountJ.fraction);
|
||||||
|
u8buf.set(curr, 8 + 4);
|
||||||
|
|
||||||
|
return u8buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function timestampRoundedToBuffer(ts: TalerProtocolTimestamp): Uint8Array {
|
||||||
|
const b = new ArrayBuffer(8);
|
||||||
|
const v = new DataView(b);
|
||||||
|
// The buffer we sign over represents the timestamp in microseconds.
|
||||||
|
if (typeof v.setBigUint64 !== "undefined") {
|
||||||
|
const s = BigInt(ts.t_s) * BigInt(1000 * 1000);
|
||||||
|
v.setBigUint64(0, s);
|
||||||
|
} else {
|
||||||
|
const s =
|
||||||
|
ts.t_s === "never" ? bigint.zero : bigint(ts.t_s).multiply(1000 * 1000);
|
||||||
|
const arr = s.toArray(2 ** 8).value;
|
||||||
|
let offset = 8 - arr.length;
|
||||||
|
for (let i = 0; i < arr.length; i++) {
|
||||||
|
v.setUint8(offset++, arr[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new Uint8Array(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function durationRoundedToBuffer(ts: TalerProtocolDuration): Uint8Array {
|
||||||
|
const b = new ArrayBuffer(8);
|
||||||
|
const v = new DataView(b);
|
||||||
|
// The buffer we sign over represents the timestamp in microseconds.
|
||||||
|
if (typeof v.setBigUint64 !== "undefined") {
|
||||||
|
const s = BigInt(ts.d_us);
|
||||||
|
v.setBigUint64(0, s);
|
||||||
|
} else {
|
||||||
|
const s = ts.d_us === "forever" ? bigint.zero : bigint(ts.d_us);
|
||||||
|
const arr = s.toArray(2 ** 8).value;
|
||||||
|
let offset = 8 - arr.length;
|
||||||
|
for (let i = 0; i < arr.length; i++) {
|
||||||
|
v.setUint8(offset++, arr[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new Uint8Array(b);
|
||||||
|
}
|
||||||
|
@ -31,6 +31,7 @@ import {
|
|||||||
AmountLike,
|
AmountLike,
|
||||||
Amounts,
|
Amounts,
|
||||||
AmountString,
|
AmountString,
|
||||||
|
amountToBuffer,
|
||||||
BlindedDenominationSignature,
|
BlindedDenominationSignature,
|
||||||
bufferForUint32,
|
bufferForUint32,
|
||||||
bufferForUint64,
|
bufferForUint64,
|
||||||
@ -44,6 +45,7 @@ import {
|
|||||||
decryptContractForMerge,
|
decryptContractForMerge,
|
||||||
DenomKeyType,
|
DenomKeyType,
|
||||||
DepositInfo,
|
DepositInfo,
|
||||||
|
durationRoundedToBuffer,
|
||||||
ecdhGetPublic,
|
ecdhGetPublic,
|
||||||
eddsaGetPublic,
|
eddsaGetPublic,
|
||||||
EddsaPublicKeyString,
|
EddsaPublicKeyString,
|
||||||
@ -82,6 +84,7 @@ import {
|
|||||||
TalerProtocolDuration,
|
TalerProtocolDuration,
|
||||||
TalerProtocolTimestamp,
|
TalerProtocolTimestamp,
|
||||||
TalerSignaturePurpose,
|
TalerSignaturePurpose,
|
||||||
|
timestampRoundedToBuffer,
|
||||||
UnblindedSignature,
|
UnblindedSignature,
|
||||||
validateIban,
|
validateIban,
|
||||||
WireFee,
|
WireFee,
|
||||||
@ -1698,64 +1701,6 @@ export const nativeCryptoR: TalerCryptoInterfaceR = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
function amountToBuffer(amount: AmountLike): Uint8Array {
|
|
||||||
const amountJ = Amounts.jsonifyAmount(amount);
|
|
||||||
const buffer = new ArrayBuffer(8 + 4 + 12);
|
|
||||||
const dvbuf = new DataView(buffer);
|
|
||||||
const u8buf = new Uint8Array(buffer);
|
|
||||||
const curr = stringToBytes(amountJ.currency);
|
|
||||||
if (typeof dvbuf.setBigUint64 !== "undefined") {
|
|
||||||
dvbuf.setBigUint64(0, BigInt(amountJ.value));
|
|
||||||
} else {
|
|
||||||
const arr = bigint(amountJ.value).toArray(2 ** 8).value;
|
|
||||||
let offset = 8 - arr.length;
|
|
||||||
for (let i = 0; i < arr.length; i++) {
|
|
||||||
dvbuf.setUint8(offset++, arr[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
dvbuf.setUint32(8, amountJ.fraction);
|
|
||||||
u8buf.set(curr, 8 + 4);
|
|
||||||
|
|
||||||
return u8buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
function timestampRoundedToBuffer(ts: TalerProtocolTimestamp): Uint8Array {
|
|
||||||
const b = new ArrayBuffer(8);
|
|
||||||
const v = new DataView(b);
|
|
||||||
// The buffer we sign over represents the timestamp in microseconds.
|
|
||||||
if (typeof v.setBigUint64 !== "undefined") {
|
|
||||||
const s = BigInt(ts.t_s) * BigInt(1000 * 1000);
|
|
||||||
v.setBigUint64(0, s);
|
|
||||||
} else {
|
|
||||||
const s =
|
|
||||||
ts.t_s === "never" ? bigint.zero : bigint(ts.t_s).multiply(1000 * 1000);
|
|
||||||
const arr = s.toArray(2 ** 8).value;
|
|
||||||
let offset = 8 - arr.length;
|
|
||||||
for (let i = 0; i < arr.length; i++) {
|
|
||||||
v.setUint8(offset++, arr[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new Uint8Array(b);
|
|
||||||
}
|
|
||||||
|
|
||||||
function durationRoundedToBuffer(ts: TalerProtocolDuration): Uint8Array {
|
|
||||||
const b = new ArrayBuffer(8);
|
|
||||||
const v = new DataView(b);
|
|
||||||
// The buffer we sign over represents the timestamp in microseconds.
|
|
||||||
if (typeof v.setBigUint64 !== "undefined") {
|
|
||||||
const s = BigInt(ts.d_us);
|
|
||||||
v.setBigUint64(0, s);
|
|
||||||
} else {
|
|
||||||
const s = ts.d_us === "forever" ? bigint.zero : bigint(ts.d_us);
|
|
||||||
const arr = s.toArray(2 ** 8).value;
|
|
||||||
let offset = 8 - arr.length;
|
|
||||||
for (let i = 0; i < arr.length; i++) {
|
|
||||||
v.setUint8(offset++, arr[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new Uint8Array(b);
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface EddsaSignRequest {
|
export interface EddsaSignRequest {
|
||||||
msg: string;
|
msg: string;
|
||||||
priv: string;
|
priv: string;
|
||||||
|
Loading…
Reference in New Issue
Block a user