-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: WalletConnect integration, part 9, getAccounts
- Loading branch information
1 parent
aaecb51
commit f153824
Showing
4 changed files
with
194 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { makeToolkit } from "@umami/tezos"; | ||
import { WalletConnectError } from "@umami/utils"; | ||
|
||
import { getPublicKeyAndCurve } from "./getPublicKeyAndCurve"; | ||
|
||
jest.mock("@umami/tezos", () => ({ | ||
...jest.requireActual("@umami/tezos"), | ||
makeToolkit: jest.fn(), | ||
})); | ||
const mockGetManagerKey = jest.fn(); | ||
|
||
describe("getPublicKeyAndCurve", () => { | ||
beforeEach(() => { | ||
jest.mocked(makeToolkit).mockImplementation( | ||
() => | ||
({ | ||
rpc: { | ||
getManagerKey: mockGetManagerKey, | ||
}, | ||
}) as any | ||
); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
const mockSigner = { address: "tz1..." } as any; | ||
const mockNetwork = { name: "mainnet" } as any; | ||
const mockAddress = "tz1..."; | ||
|
||
it("returns the public key and curve for ed25519", async () => { | ||
mockGetManagerKey.mockResolvedValue("edpk123456789"); | ||
|
||
const result = await getPublicKeyAndCurve(mockAddress, mockSigner, mockNetwork); | ||
|
||
expect(result).toEqual({ | ||
publicKey: "edpk123456789", | ||
curve: "ed25519", | ||
}); | ||
}); | ||
|
||
it("returns the public key and curve for secp256k1", async () => { | ||
mockGetManagerKey.mockResolvedValue("sppk123456789"); | ||
|
||
const result = await getPublicKeyAndCurve(mockAddress, mockSigner, mockNetwork); | ||
|
||
expect(result).toEqual({ | ||
publicKey: "sppk123456789", | ||
curve: "secp256k1", | ||
}); | ||
}); | ||
|
||
it("returns the public key and curve for p-256", async () => { | ||
mockGetManagerKey.mockResolvedValue("p2pk123456789"); | ||
|
||
const result = await getPublicKeyAndCurve(mockAddress, mockSigner, mockNetwork); | ||
|
||
expect(result).toEqual({ | ||
publicKey: "p2pk123456789", | ||
curve: "p-256", | ||
}); | ||
}); | ||
|
||
it("throws an error if the public key has an unknown prefix", async () => { | ||
mockGetManagerKey.mockResolvedValue("unknown123456789"); | ||
|
||
await expect(getPublicKeyAndCurve(mockAddress, mockSigner, mockNetwork)).rejects.toThrow( | ||
WalletConnectError | ||
); | ||
|
||
await expect(getPublicKeyAndCurve(mockAddress, mockSigner, mockNetwork)).rejects.toThrow( | ||
"Unknown curve for the public key: unknown123456789" | ||
); | ||
}); | ||
|
||
it("throws an error if the account is not revealed", async () => { | ||
mockGetManagerKey.mockResolvedValue(null); | ||
|
||
await expect(getPublicKeyAndCurve(mockAddress, mockSigner, mockNetwork)).rejects.toThrow( | ||
WalletConnectError | ||
); | ||
|
||
await expect(getPublicKeyAndCurve(mockAddress, mockSigner, mockNetwork)).rejects.toThrow( | ||
`Signer address is not revealed on the ${mockNetwork.name}` | ||
); | ||
}); | ||
|
||
it("handles the case where the managerKeyResponse is an object with a key field", async () => { | ||
mockGetManagerKey.mockResolvedValue({ key: "edpk987654321" }); | ||
|
||
const result = await getPublicKeyAndCurve(mockAddress, mockSigner, mockNetwork); | ||
|
||
expect(result).toEqual({ | ||
publicKey: "edpk987654321", | ||
curve: "ed25519", | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { type ManagerKeyResponse } from "@taquito/rpc"; | ||
import { type ImplicitAccount } from "@umami/core"; | ||
import { type Network, type RawPkh, makeToolkit } from "@umami/tezos"; | ||
import { WalletConnectError } from "@umami/utils"; | ||
import { type SessionTypes } from "@walletconnect/types"; | ||
|
||
/** | ||
* Estimates (and simulates the execution of) the operations. | ||
* | ||
* @param address - tz1 address of the account | ||
* @param signer - Implicit account | ||
* @param network - network | ||
* @param session - WalletConnect session | ||
* @returns the public key if revelead | ||
* Throws an error if the account is not revelead | ||
*/ | ||
export const getPublicKeyAndCurve = async ( | ||
address: RawPkh, | ||
signer: ImplicitAccount, | ||
network: Network, | ||
session?: SessionTypes.Struct | null | ||
): Promise<{ publicKey: string; curve: string }> => { | ||
const tezosToolkit = await makeToolkit({ | ||
type: "fake", | ||
signer: signer, | ||
network, | ||
}); | ||
const keyResponse: ManagerKeyResponse | null = await tezosToolkit.rpc.getManagerKey(address); | ||
console.log("keyResponse", keyResponse); | ||
if (!keyResponse) { | ||
throw new WalletConnectError( | ||
`Signer address is not revealed on the ${network.name}`, | ||
"UNSUPPORTED_ACCOUNTS", | ||
session || null | ||
); | ||
} | ||
const publicKey = typeof keyResponse === "string" ? keyResponse : keyResponse.key; | ||
console.log("publicKey", publicKey); | ||
const curve = publicKey.startsWith("edpk") | ||
? "ed25519" | ||
: publicKey.startsWith("sppk") | ||
? "secp256k1" | ||
: publicKey.startsWith("p2pk") | ||
? "p-256" | ||
: null; | ||
if (!curve) { | ||
throw new WalletConnectError( | ||
`Unknown curve for the public key: ${publicKey}`, | ||
"UNSUPPORTED_ACCOUNTS", | ||
session || null | ||
); | ||
} | ||
return { publicKey, curve }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f153824
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.