refactor: employ prettier
This commit is contained in:
@@ -15,8 +15,8 @@ import * as Layout from './layout';
|
||||
* @property {PublicKey} from Public key from which `applySignature()` will be accepted from
|
||||
*/
|
||||
export type SignatureCondition = {
|
||||
type: 'signature';
|
||||
from: PublicKey;
|
||||
type: 'signature',
|
||||
from: PublicKey,
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -29,9 +29,9 @@ export type SignatureCondition = {
|
||||
* @property {Date} when The timestamp that was observed
|
||||
*/
|
||||
export type TimestampCondition = {
|
||||
type: 'timestamp';
|
||||
from: PublicKey;
|
||||
when: Date;
|
||||
type: 'timestamp',
|
||||
from: PublicKey,
|
||||
when: Date,
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -42,9 +42,9 @@ export type TimestampCondition = {
|
||||
* @property {PublicKey} to Public key of the recipient
|
||||
*/
|
||||
export type Payment = {
|
||||
amount: number;
|
||||
to: PublicKey;
|
||||
}
|
||||
amount: number,
|
||||
to: PublicKey,
|
||||
};
|
||||
|
||||
/**
|
||||
* A condition that can unlock a payment
|
||||
@@ -67,9 +67,7 @@ function serializePayment(payment: Payment): Buffer {
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
function serializeDate(
|
||||
when: Date
|
||||
): Buffer {
|
||||
function serializeDate(when: Date): Buffer {
|
||||
const userdata = Buffer.alloc(8 + 20);
|
||||
userdata.writeUInt32LE(20, 0); // size of timestamp as u64
|
||||
|
||||
@@ -81,13 +79,20 @@ function serializeDate(
|
||||
return number;
|
||||
}
|
||||
|
||||
return date.getUTCFullYear() +
|
||||
'-' + pad(date.getUTCMonth() + 1) +
|
||||
'-' + pad(date.getUTCDate()) +
|
||||
'T' + pad(date.getUTCHours()) +
|
||||
':' + pad(date.getUTCMinutes()) +
|
||||
':' + pad(date.getUTCSeconds()) +
|
||||
'Z';
|
||||
return (
|
||||
date.getUTCFullYear() +
|
||||
'-' +
|
||||
pad(date.getUTCMonth() + 1) +
|
||||
'-' +
|
||||
pad(date.getUTCDate()) +
|
||||
'T' +
|
||||
pad(date.getUTCHours()) +
|
||||
':' +
|
||||
pad(date.getUTCMinutes()) +
|
||||
':' +
|
||||
pad(date.getUTCSeconds()) +
|
||||
'Z'
|
||||
);
|
||||
}
|
||||
userdata.write(iso(when), 8);
|
||||
return userdata;
|
||||
@@ -97,52 +102,50 @@ function serializeDate(
|
||||
* @private
|
||||
*/
|
||||
function serializeCondition(condition: BudgetCondition) {
|
||||
switch(condition.type) {
|
||||
case 'timestamp':
|
||||
{
|
||||
const date = serializeDate(condition.when);
|
||||
const from = condition.from.toBuffer();
|
||||
switch (condition.type) {
|
||||
case 'timestamp': {
|
||||
const date = serializeDate(condition.when);
|
||||
const from = condition.from.toBuffer();
|
||||
|
||||
const userdata = Buffer.alloc(4 + date.length + from.length);
|
||||
userdata.writeUInt32LE(0, 0); // Condition enum = Timestamp
|
||||
date.copy(userdata, 4);
|
||||
from.copy(userdata, 4 + date.length);
|
||||
return userdata;
|
||||
}
|
||||
case 'signature':
|
||||
{
|
||||
const userdataLayout = BufferLayout.struct([
|
||||
BufferLayout.u32('condition'),
|
||||
Layout.publicKey('from'),
|
||||
]);
|
||||
const userdata = Buffer.alloc(4 + date.length + from.length);
|
||||
userdata.writeUInt32LE(0, 0); // Condition enum = Timestamp
|
||||
date.copy(userdata, 4);
|
||||
from.copy(userdata, 4 + date.length);
|
||||
return userdata;
|
||||
}
|
||||
case 'signature': {
|
||||
const userdataLayout = BufferLayout.struct([
|
||||
BufferLayout.u32('condition'),
|
||||
Layout.publicKey('from'),
|
||||
]);
|
||||
|
||||
const from = condition.from.toBuffer();
|
||||
const userdata = Buffer.alloc(4 + from.length);
|
||||
userdataLayout.encode(
|
||||
{
|
||||
instruction: 1, // Signature
|
||||
from
|
||||
},
|
||||
userdata,
|
||||
);
|
||||
return userdata;
|
||||
}
|
||||
default:
|
||||
throw new Error(`Unknown condition type: ${condition.type}`);
|
||||
const from = condition.from.toBuffer();
|
||||
const userdata = Buffer.alloc(4 + from.length);
|
||||
userdataLayout.encode(
|
||||
{
|
||||
instruction: 1, // Signature
|
||||
from,
|
||||
},
|
||||
userdata,
|
||||
);
|
||||
return userdata;
|
||||
}
|
||||
default:
|
||||
throw new Error(`Unknown condition type: ${condition.type}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Factory class for transactions to interact with the Budget program
|
||||
*/
|
||||
export class BudgetProgram {
|
||||
|
||||
/**
|
||||
* Public key that identifies the Budget program
|
||||
*/
|
||||
static get programId(): PublicKey {
|
||||
return new PublicKey('0x8100000000000000000000000000000000000000000000000000000000000000');
|
||||
return new PublicKey(
|
||||
'0x8100000000000000000000000000000000000000000000000000000000000000',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -152,11 +155,10 @@ export class BudgetProgram {
|
||||
return 128;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a timestamp condition
|
||||
*/
|
||||
static timestampCondition(from: PublicKey, when: Date) : TimestampCondition {
|
||||
static timestampCondition(from: PublicKey, when: Date): TimestampCondition {
|
||||
return {
|
||||
type: 'timestamp',
|
||||
from,
|
||||
@@ -167,7 +169,7 @@ export class BudgetProgram {
|
||||
/**
|
||||
* Creates a signature condition
|
||||
*/
|
||||
static signatureCondition(from: PublicKey) : SignatureCondition {
|
||||
static signatureCondition(from: PublicKey): SignatureCondition {
|
||||
return {
|
||||
type: 'signature',
|
||||
from,
|
||||
@@ -190,64 +192,68 @@ export class BudgetProgram {
|
||||
pos += 4;
|
||||
|
||||
switch (conditions.length) {
|
||||
case 0:
|
||||
userdata.writeUInt32LE(0, pos); // Budget enum = Pay
|
||||
pos += 4;
|
||||
case 0:
|
||||
userdata.writeUInt32LE(0, pos); // Budget enum = Pay
|
||||
pos += 4;
|
||||
|
||||
{
|
||||
const payment = serializePayment({amount, to});
|
||||
payment.copy(userdata, pos);
|
||||
pos += payment.length;
|
||||
}
|
||||
{
|
||||
const payment = serializePayment({amount, to});
|
||||
payment.copy(userdata, pos);
|
||||
pos += payment.length;
|
||||
}
|
||||
|
||||
return new Transaction().add({
|
||||
keys: [from, to],
|
||||
programId: this.programId,
|
||||
userdata: userdata.slice(0, pos),
|
||||
});
|
||||
case 1:
|
||||
userdata.writeUInt32LE(1, pos); // Budget enum = After
|
||||
pos += 4;
|
||||
{
|
||||
const condition = conditions[0];
|
||||
return new Transaction().add({
|
||||
keys: [from, to],
|
||||
programId: this.programId,
|
||||
userdata: userdata.slice(0, pos),
|
||||
});
|
||||
case 1:
|
||||
userdata.writeUInt32LE(1, pos); // Budget enum = After
|
||||
pos += 4;
|
||||
{
|
||||
const condition = conditions[0];
|
||||
|
||||
const conditionData = serializeCondition(condition);
|
||||
conditionData.copy(userdata, pos);
|
||||
pos += conditionData.length;
|
||||
const conditionData = serializeCondition(condition);
|
||||
conditionData.copy(userdata, pos);
|
||||
pos += conditionData.length;
|
||||
|
||||
const paymentData = serializePayment({amount, to});
|
||||
paymentData.copy(userdata, pos);
|
||||
pos += paymentData.length;
|
||||
}
|
||||
const paymentData = serializePayment({amount, to});
|
||||
paymentData.copy(userdata, pos);
|
||||
pos += paymentData.length;
|
||||
}
|
||||
|
||||
return new Transaction().add({
|
||||
keys: [from, program, to],
|
||||
programId: this.programId,
|
||||
userdata: userdata.slice(0, pos),
|
||||
});
|
||||
return new Transaction().add({
|
||||
keys: [from, program, to],
|
||||
programId: this.programId,
|
||||
userdata: userdata.slice(0, pos),
|
||||
});
|
||||
|
||||
case 2:
|
||||
userdata.writeUInt32LE(2, pos); // Budget enum = Or
|
||||
pos += 4;
|
||||
case 2:
|
||||
userdata.writeUInt32LE(2, pos); // Budget enum = Or
|
||||
pos += 4;
|
||||
|
||||
for (let condition of conditions) {
|
||||
const conditionData = serializeCondition(condition);
|
||||
conditionData.copy(userdata, pos);
|
||||
pos += conditionData.length;
|
||||
for (let condition of conditions) {
|
||||
const conditionData = serializeCondition(condition);
|
||||
conditionData.copy(userdata, pos);
|
||||
pos += conditionData.length;
|
||||
|
||||
const paymentData = serializePayment({amount, to});
|
||||
paymentData.copy(userdata, pos);
|
||||
pos += paymentData.length;
|
||||
}
|
||||
const paymentData = serializePayment({amount, to});
|
||||
paymentData.copy(userdata, pos);
|
||||
pos += paymentData.length;
|
||||
}
|
||||
|
||||
return new Transaction().add({
|
||||
keys: [from, program, to],
|
||||
programId: this.programId,
|
||||
userdata: userdata.slice(0, pos),
|
||||
});
|
||||
return new Transaction().add({
|
||||
keys: [from, program, to],
|
||||
programId: this.programId,
|
||||
userdata: userdata.slice(0, pos),
|
||||
});
|
||||
|
||||
default:
|
||||
throw new Error(`A maximum of two conditions are support: ${conditions.length} provided`);
|
||||
default:
|
||||
throw new Error(
|
||||
`A maximum of two conditions are support: ${
|
||||
conditions.length
|
||||
} provided`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -287,12 +293,16 @@ export class BudgetProgram {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates a transaction that applies a timestamp, which could enable a
|
||||
* pending payment to proceed.
|
||||
*/
|
||||
static applyTimestamp(from: PublicKey, program: PublicKey, to: PublicKey, when: Date): Transaction {
|
||||
static applyTimestamp(
|
||||
from: PublicKey,
|
||||
program: PublicKey,
|
||||
to: PublicKey,
|
||||
when: Date,
|
||||
): Transaction {
|
||||
const whenData = serializeDate(when);
|
||||
const userdata = Buffer.alloc(4 + whenData.length);
|
||||
|
||||
@@ -310,7 +320,11 @@ export class BudgetProgram {
|
||||
* Generates a transaction that applies a signature, which could enable a
|
||||
* pending payment to proceed.
|
||||
*/
|
||||
static applySignature(from: PublicKey, program: PublicKey, to: PublicKey): Transaction {
|
||||
static applySignature(
|
||||
from: PublicKey,
|
||||
program: PublicKey,
|
||||
to: PublicKey,
|
||||
): Transaction {
|
||||
const userdataLayout = BufferLayout.struct([
|
||||
BufferLayout.u32('instruction'),
|
||||
]);
|
||||
|
Reference in New Issue
Block a user