-
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 8, getAccounts
- Loading branch information
1 parent
aaecb51
commit 5f5365e
Showing
4 changed files
with
196 additions
and
7 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,99 @@ | ||
import { getPublicKeyAndCurve } from "./getPublicKeyAndCurve"; | ||
import { WalletConnectError } from "@umami/utils"; | ||
import { makeToolkit } from "@umami/tezos"; | ||
import { TezosToolkit } from "@taquito/taquito"; | ||
|
||
jest.mock("@umami/tezos", () => ({ | ||
...jest.requireActual("@umami/tezos"), | ||
makeToolkit: jest.fn(), | ||
})); | ||
const mockGetManagerKey = jest.fn(); | ||
|
||
describe("getPublicKeyAndCurve", () => { | ||
|
||
beforeEach(() => { | ||
const testToolkit = new TezosToolkit("test-tezos-toolkit"); | ||
|
||
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("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", | ||
}); | ||
}); | ||
|
||
it("throws an error if the managerKeyResponse object does not have a key", async () => { | ||
mockGetManagerKey.mockResolvedValue({}); | ||
|
||
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}` | ||
); | ||
}); | ||
}); |
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,53 @@ | ||
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"; | ||
|
||
/** | ||
* Estimates (and simulates the execution of) the operations. | ||
* | ||
* @param address - tz1 address of the account | ||
* @param signer - Implicit account | ||
* @param network - network | ||
* @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 | ||
): Promise<{ publicKey: string; curve: string }> => { | ||
const tezosToolkit = await makeToolkit({ | ||
type: "fake", | ||
signer: signer, | ||
network, | ||
}); | ||
const managerKeyResponse: ManagerKeyResponse = await tezosToolkit.rpc.getManagerKey(address); | ||
let publicKey = ""; | ||
if (typeof managerKeyResponse === "string") { | ||
publicKey = managerKeyResponse; | ||
} else if (managerKeyResponse.key) { | ||
publicKey = managerKeyResponse.key; | ||
} else { | ||
throw new WalletConnectError( | ||
`Signer address is not revealed on the ${network.name}`, | ||
"UNSUPPORTED_ACCOUNTS", | ||
null | ||
); | ||
} | ||
let curve = "unknown"; | ||
if (publicKey.startsWith("edpk")) { | ||
curve = "ed25519"; | ||
} else if (publicKey.startsWith("sppk")) { | ||
curve = "secp256k1"; | ||
} else if (publicKey.startsWith("p2pk")) { | ||
curve = "p-256"; | ||
} else { | ||
throw new WalletConnectError( | ||
`Unknown curve for the public key: ${publicKey}`, | ||
"UNSUPPORTED_ACCOUNTS", | ||
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