Skip to content
56 changes: 55 additions & 1 deletion modules/sdk-coin-flrp/src/lib/atomicTransactionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
FLARE_ATOMIC_PARSED_PREFIX,
HEX_ENCODING,
} from './constants';
import { createFlexibleHexRegex } from './utils';
import utils, { createFlexibleHexRegex } from './utils';

/**
* Flare P-chain atomic transaction builder with FlareJS credential support.
Expand Down Expand Up @@ -420,4 +420,58 @@ export abstract class AtomicTransactionBuilder {
);
}
}

/**
* Threshold is an int that names the number of unique signatures required to spend the output.
* Must be less than or equal to the length of Addresses.
* @param {number}
*/
threshold(value: number): this {
this.validateThreshold(value);
this.transaction._threshold = value;
return this;
}

/**
* Validates the threshold
* @param threshold
*/
validateThreshold(threshold: number): void {
if (!threshold || threshold !== 2) {
throw new BuildTransactionError('Invalid transaction: threshold must be set to 2');
}
}

/**
* fromPubKey is a list of unique addresses that correspond to the private keys that can be used to spend this output.
* @param {string | string[]} senderPubKey
*/
// TODO: check the format of the public keys
fromPubKey(senderPubKey: string | string[]): this {
const pubKeys = senderPubKey instanceof Array ? senderPubKey : [senderPubKey];
this.transaction._fromAddresses = pubKeys.map(utils.parseAddress);
return this;
}

/**
* Locktime is a long that contains the unix timestamp that this output can be spent after.
* The unix timestamp is specific to the second.
* @param value
*/
locktime(value: number): this {
const locktime = BigInt(value);
this.validateLocktime(locktime);
this.transaction._locktime = locktime;
return this;
}

/**
* Validates locktime
* @param locktime
*/
validateLocktime(locktime: bigint): void {
if (!locktime || locktime < 0n) {
throw new BuildTransactionError('Invalid transaction: locktime must be 0 or higher');
}
}
}
9 changes: 7 additions & 2 deletions modules/sdk-coin-flrp/src/lib/exportInCTxBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
NETWORK_ID_PROP,
BLOCKCHAIN_ID_PROP,
} from './constants';
import utils from './utils';

// Lightweight interface placeholders replacing Avalanche SDK transaction shapes
interface FlareExportInputShape {
Expand Down Expand Up @@ -95,8 +96,12 @@ export class ExportInCTxBuilder extends AtomicInCTransactionBuilder {
* @param pAddresses
*/
to(pAddresses: string | string[]): this {
const pubKeys = Array.isArray(pAddresses) ? pAddresses : pAddresses.split('~');
// For now ensure they are stored as bech32 / string addresses directly
const pubKeys = pAddresses instanceof Array ? pAddresses : pAddresses.split('~');
if (!utils.isValidAddress(pubKeys)) {
throw new BuildTransactionError('Invalid to address');
}

// TODO need to check if address should be string or Buffer
this.transaction._to = pubKeys.map((a) => a.toString());
return this;
}
Expand Down
1 change: 1 addition & 0 deletions modules/sdk-coin-flrp/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { KeyPair } from './keyPair';
export { Utils };
export { TransactionBuilderFactory } from './transactionBuilderFactory';
export { Transaction } from './transaction';
export { TransactionBuilder } from './transactionBuilder';
export { AtomicTransactionBuilder } from './atomicTransactionBuilder';
export { AtomicInCTransactionBuilder } from './atomicInCTransactionBuilder';
export { ImportInCTxBuilder } from './importInCTxBuilder';
Expand Down
2 changes: 1 addition & 1 deletion modules/sdk-coin-flrp/src/lib/keyPair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class KeyPair extends Secp256k1ExtendedKeyPair {
*
* @returns {Buffer} The address buffer derived from the public key
*/
private getAddressBuffer(): Buffer {
getAddressBuffer(): Buffer {
try {
// Use the safe buffer method for address derivation
return this.getAddressSafeBuffer();
Expand Down
Loading
Loading