Skip to content

Commit c9eaaee

Browse files
[PSDK-441] createPayloadSignature for Wallet Class (#207)
* [PSDK-441] createPayloadSignature for Wallet Class * fix doc string typo
1 parent 5820dbc commit c9eaaee

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

src/coinbase/address/wallet_address.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ export class WalletAddress extends Address {
368368
/**
369369
* Creates a Payload Signature.
370370
*
371-
* @param unsignedPayload - The Unisgned Payload to sign.
371+
* @param unsignedPayload - The Unsigned Payload to sign.
372372
* @returns A promise that resolves to the Payload Signature object.
373373
* @throws {APIError} if the API request to create a Payload Signature fails.
374374
* @throws {Error} if the address does not have a private key loaded or an associated Server-Signer.

src/coinbase/wallet.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { convertStringToHex, delay, formatDate, getWeekBackDate } from "./utils"
3131
import { StakingOperation } from "./staking_operation";
3232
import { StakingReward } from "./staking_reward";
3333
import { StakingBalance } from "./staking_balance";
34+
import { PayloadSignature } from "./payload_signature";
3435

3536
/**
3637
* A representation of a Wallet. Wallets come with a single default Address, but can expand to have a set of Addresses,
@@ -755,6 +756,22 @@ export class Wallet {
755756
return await this.getDefaultAddress()!.createTransfer(options);
756757
}
757758

759+
/**
760+
* Creates a Payload Signature.
761+
*
762+
* @param unsignedPayload - The Unsigned Payload to sign.
763+
* @returns A promise that resolves to the Payload Signature object.
764+
* @throws {APIError} if the API request to create a Payload Signature fails.
765+
* @throws {Error} if the default address is not found.
766+
*/
767+
public async createPayloadSignature(unsignedPayload: string): Promise<PayloadSignature> {
768+
if (!this.getDefaultAddress()) {
769+
throw new Error("Default address not found");
770+
}
771+
772+
return await this.getDefaultAddress()!.createPayloadSignature(unsignedPayload);
773+
}
774+
758775
/**
759776
* Returns a String representation of the Wallet.
760777
*

src/tests/wallet_test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ import {
4444
externalAddressApiMock,
4545
stakeApiMock,
4646
walletStakeApiMock,
47+
VALID_SIGNED_PAYLOAD_SIGNATURE_MODEL,
4748
} from "./utils";
4849
import { Trade } from "../coinbase/trade";
4950
import { WalletAddress } from "../coinbase/address/wallet_address";
5051
import { StakingOperation } from "../coinbase/staking_operation";
5152
import { StakingReward } from "../coinbase/staking_reward";
5253
import { StakingBalance } from "../coinbase/staking_balance";
54+
import { PayloadSignature } from "../coinbase/payload_signature";
5355

5456
describe("Wallet Class", () => {
5557
let wallet: Wallet;
@@ -624,6 +626,69 @@ describe("Wallet Class", () => {
624626
});
625627
});
626628

629+
describe("#createPayloadSignature", () => {
630+
let unsignedPayload = VALID_SIGNED_PAYLOAD_SIGNATURE_MODEL.unsigned_payload;
631+
let signature =
632+
"0xa4e14b28d86dfd7bae739d724ba2ffb13b4458d040930b805eea0a4bc2f5251e7901110677d1ef2ec23ef810c755d0bc72cc6472a4cfb3c53ef242c6ba9fa60a1b";
633+
634+
beforeAll(() => {
635+
Coinbase.apiClients.address = addressesApiMock;
636+
});
637+
638+
beforeEach(() => {
639+
jest.clearAllMocks();
640+
});
641+
642+
it("should successfully create a payload signature", async () => {
643+
Coinbase.apiClients.address!.createPayloadSignature = mockReturnValue(
644+
VALID_SIGNED_PAYLOAD_SIGNATURE_MODEL,
645+
);
646+
647+
const payloadSignature = await wallet.createPayloadSignature(unsignedPayload);
648+
649+
expect(Coinbase.apiClients.address!.createPayloadSignature).toHaveBeenCalledWith(
650+
wallet.getId(),
651+
wallet.getDefaultAddress()!.getId(),
652+
{
653+
unsigned_payload: unsignedPayload,
654+
signature,
655+
},
656+
);
657+
expect(Coinbase.apiClients.address!.createPayloadSignature).toHaveBeenCalledTimes(1);
658+
expect(payloadSignature).toBeInstanceOf(PayloadSignature);
659+
});
660+
661+
it("should throw an APIError when the API call to create a payload signature fails", async () => {
662+
Coinbase.apiClients.address!.createPayloadSignature = mockReturnRejectedValue(
663+
new APIError("Failed to create payload signature"),
664+
);
665+
666+
expect(async () => {
667+
await wallet.createPayloadSignature(unsignedPayload);
668+
}).rejects.toThrow(Error);
669+
670+
expect(Coinbase.apiClients.address!.createPayloadSignature).toHaveBeenCalledWith(
671+
wallet.getId(),
672+
wallet.getDefaultAddress()!.getId(),
673+
{
674+
unsigned_payload: unsignedPayload,
675+
signature,
676+
},
677+
);
678+
expect(Coinbase.apiClients.address!.createPayloadSignature).toHaveBeenCalledTimes(1);
679+
});
680+
681+
it("should throw an Error when the wallet does not have a default address", async () => {
682+
const invalidWallet = Wallet.init(walletModel);
683+
684+
expect(async () => {
685+
await invalidWallet.createPayloadSignature(unsignedPayload);
686+
}).rejects.toThrow(Error);
687+
688+
expect(Coinbase.apiClients.address!.createPayloadSignature).not.toHaveBeenCalled();
689+
});
690+
});
691+
627692
describe(".create", () => {
628693
beforeEach(() => {});
629694
it("should return a Wallet instance", async () => {

0 commit comments

Comments
 (0)