Skip to content

Commit

Permalink
minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
RaffaBux committed Jul 16, 2024
1 parent 3246afd commit a0c2e93
Showing 1 changed file with 11 additions and 24 deletions.
35 changes: 11 additions & 24 deletions src/protocols/peer-sharing/messages/PeerSharingRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,18 @@ export interface IPeerSharingRequest {
amount: number | bigint;
}

export function isIPeerSharingRequest( stuff: any ): stuff is IPeerSharingRequest
{
export function isIPeerSharingRequest( stuff: any ): stuff is IPeerSharingRequest {
return isObject( stuff );
}

export class PeerSharingRequest
implements ToCbor, ToCborObj, IPeerSharingRequest
{
export class PeerSharingRequest implements ToCbor, ToCborObj, IPeerSharingRequest {

readonly cborBytes?: Uint8Array | undefined;

readonly amount: number;

constructor( { amount } : IPeerSharingRequest)
{
if(!isByte( amount ))
{
throw new Error("peer sharing amount is not a number within a byte");
}
constructor( { amount } : IPeerSharingRequest ) {
if( !isByte( amount ) ) throw new Error("peer sharing amount is not a number within a byte");

Object.defineProperties(
this, {
Expand All @@ -40,32 +33,27 @@ export class PeerSharingRequest
);
}

toCborObj()
{
toCborObj(): CborArray {
return new CborArray([
new CborUInt( 0 ),
new CborUInt( this.amount )
]);
}

toCborBytes(): Uint8Array
{
if(!( this.cborBytes instanceof Uint8Array ))
{
toCborBytes(): Uint8Array {
if(!( this.cborBytes instanceof Uint8Array )) {
// @ts-ignore Cannot assign to 'cborBytes' because it is a read-only property.
this.cborBytes = Cbor.encode( this.toCborObj() ).toBuffer();
}

return Uint8Array.prototype.slice.call( this.cborBytes );
}

toCbor(): CborString
{
toCbor(): CborString {
return new CborString( this.toCborBytes() );
}

static fromCborObj( cbor: CborObj ): PeerSharingRequest
{
static fromCborObj( cbor: CborObj ): PeerSharingRequest {
if(!(
cbor instanceof CborArray &&
cbor.array[0] instanceof CborUInt &&
Expand All @@ -78,8 +66,7 @@ export class PeerSharingRequest
});
}

static fromCbor( cbor: CanBeCborString ): PeerSharingRequest
{
static fromCbor( cbor: CanBeCborString ): PeerSharingRequest {
const buff = cbor instanceof Uint8Array ? cbor : forceCborString( cbor ).toBuffer();

const msg = PeerSharingRequest.fromCborObj(Cbor.parse( buff ));
Expand Down

0 comments on commit a0c2e93

Please sign in to comment.