Repository navigation
fix(km): make Albedo and Freighter signing follow the configured network #259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,183 @@ | ||
| import { | ||
| Keypair, | ||
| Networks, | ||
| Account, | ||
| TransactionBuilder, | ||
| Operation, | ||
| Transaction, | ||
| Asset, | ||
| BASE_FEE, | ||
| WebAuth, | ||
| } from "@stellar/stellar-sdk"; | ||
| import albedo from "@albedo-link/intent"; | ||
|
|
||
| import { albedoSigningWith } from "./fixtures/albedo"; | ||
| import { albedoHandler } from "../src/Handlers/albedo"; | ||
| import { Key, KeyType } from "../src/Types"; | ||
|
|
||
| jest.mock("@albedo-link/intent", () => ({ | ||
| __esModule: true, | ||
| default: { | ||
| tx: jest.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| const mockTx = albedo.tx as jest.Mock; | ||
|
|
||
| function buildTestTransaction( | ||
| source: Keypair, | ||
| networkPassphrase: string, | ||
| ): Transaction { | ||
| return new TransactionBuilder(new Account(source.publicKey(), "100"), { | ||
| fee: BASE_FEE, | ||
| networkPassphrase, | ||
| }) | ||
| .addOperation( | ||
| Operation.payment({ | ||
| destination: Keypair.random().publicKey(), | ||
| asset: Asset.native(), | ||
| amount: "10", | ||
| }), | ||
| ) | ||
| .setTimeout(30) | ||
| .build(); | ||
| } | ||
|
|
||
| function makeAlbedoKey(signer: Keypair, overrides: Partial<Key> = {}): Key { | ||
| return { | ||
| id: "albedo-test-key", | ||
| privateKey: "", | ||
| publicKey: signer.publicKey(), | ||
| type: KeyType.albedo, | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| describe("albedoHandler", () => { | ||
| let signer: Keypair; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| signer = Keypair.random(); | ||
| mockTx.mockImplementation(albedoSigningWith(signer)); | ||
| }); | ||
|
|
||
| it("signs for the network the key is scoped to", async () => { | ||
| const tx = buildTestTransaction(signer, Networks.TESTNET); | ||
| const key = makeAlbedoKey(signer, { network: Networks.TESTNET }); | ||
|
|
||
| const result = await albedoHandler.signTransaction({ | ||
| transaction: tx, | ||
| key, | ||
| }); | ||
|
|
||
| expect(result.networkPassphrase).toBe(Networks.TESTNET); | ||
| expect(WebAuth.verifyTxSignedBy(result, signer.publicKey())).toBe(true); | ||
| }); | ||
|
|
||
| it("signs for custom.networkPassphrase over the key's network", async () => { | ||
| const tx = buildTestTransaction(signer, Networks.TESTNET); | ||
| const key = makeAlbedoKey(signer, { network: Networks.PUBLIC }); | ||
|
|
||
| const result = await albedoHandler.signTransaction({ | ||
| transaction: tx, | ||
| key, | ||
| custom: { networkPassphrase: Networks.TESTNET }, | ||
| }); | ||
|
|
||
| expect(result.networkPassphrase).toBe(Networks.TESTNET); | ||
| expect(WebAuth.verifyTxSignedBy(result, signer.publicKey())).toBe(true); | ||
| }); | ||
|
|
||
| it("asks Albedo for the public network when no network is set", async () => { | ||
| const tx = buildTestTransaction(signer, Networks.PUBLIC); | ||
| const key = makeAlbedoKey(signer); | ||
|
|
||
| const result = await albedoHandler.signTransaction({ | ||
| transaction: tx, | ||
| key, | ||
| }); | ||
|
|
||
| expect(mockTx).toHaveBeenCalledWith( | ||
| expect.objectContaining({ xdr: tx.toXdr(), network: "public" }), | ||
| ); | ||
| expect(result.networkPassphrase).toBe(Networks.PUBLIC); | ||
| expect(WebAuth.verifyTxSignedBy(result, signer.publicKey())).toBe(true); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ["Networks.FUTURENET", Networks.FUTURENET], | ||
| ["a private network passphrase", "Private Network ; October 2026"], | ||
| ])("refuses %s without opening Albedo", async (_, networkPassphrase) => { | ||
| const tx = buildTestTransaction(signer, networkPassphrase); | ||
| const key = makeAlbedoKey(signer, { network: networkPassphrase }); | ||
|
|
||
| await expect( | ||
| albedoHandler.signTransaction({ transaction: tx, key }), | ||
| ).rejects.toThrow(networkPassphrase); | ||
| expect(mockTx).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it.each([ | ||
| { | ||
| setup: "a key without a network", | ||
| txNetwork: Networks.TESTNET, | ||
| keyNetwork: undefined, | ||
| customNetwork: undefined, | ||
| }, | ||
| { | ||
| setup: "a key scoped to another network", | ||
| txNetwork: Networks.PUBLIC, | ||
| keyNetwork: Networks.TESTNET, | ||
| customNetwork: undefined, | ||
| }, | ||
| { | ||
| setup: "custom.networkPassphrase for another network", | ||
| txNetwork: Networks.TESTNET, | ||
| keyNetwork: Networks.TESTNET, | ||
| customNetwork: Networks.PUBLIC, | ||
| }, | ||
| ])( | ||
| "refuses a transaction for another network ($setup) without opening Albedo", | ||
| async ({ txNetwork, keyNetwork, customNetwork }) => { | ||
| const tx = buildTestTransaction(signer, txNetwork); | ||
| const key = makeAlbedoKey(signer, { network: keyNetwork }); | ||
|
|
||
| await expect( | ||
| albedoHandler.signTransaction({ | ||
| transaction: tx, | ||
| key, | ||
| custom: { networkPassphrase: customNetwork }, | ||
| }), | ||
| ).rejects.toThrow(`"${txNetwork}"`); | ||
| expect(mockTx).not.toHaveBeenCalled(); | ||
| }, | ||
| ); | ||
|
|
||
| it("refuses a key that holds a private key without opening Albedo", async () => { | ||
| const tx = buildTestTransaction(signer, Networks.TESTNET); | ||
| const key = makeAlbedoKey(signer, { privateKey: signer.secret() }); | ||
|
|
||
| await expect( | ||
| albedoHandler.signTransaction({ transaction: tx, key }), | ||
| ).rejects.toThrow(signer.publicKey()); | ||
| expect(mockTx).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("throws when Albedo returns no signed envelope", async () => { | ||
| mockTx.mockResolvedValue({ | ||
| intent: "tx", | ||
| pubkey: signer.publicKey(), | ||
| network: "testnet", | ||
| xdr: "", | ||
| tx_hash: "", | ||
| signed_envelope_xdr: "", | ||
| }); | ||
| const tx = buildTestTransaction(signer, Networks.TESTNET); | ||
| const key = makeAlbedoKey(signer, { network: Networks.TESTNET }); | ||
|
|
||
| await expect( | ||
| albedoHandler.signTransaction({ transaction: tx, key }), | ||
| ).rejects.toThrow("We couldn’t sign the transaction with Albedo"); | ||
| }); | ||
| }); |
This file contains hidden or 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,39 @@ | ||
| import { | ||
| Keypair, | ||
| Networks, | ||
| Transaction, | ||
| TransactionBuilder, | ||
| xdr, | ||
| } from "@stellar/stellar-sdk"; | ||
|
|
||
| // The tx intent's `network` param takes a network name or that network's | ||
| // passphrase, and Albedo signs with the matching passphrase. | ||
| const albedoPassphrases: { [network: string]: string } = { | ||
| public: Networks.PUBLIC, | ||
| testnet: Networks.TESTNET, | ||
| [Networks.PUBLIC]: Networks.PUBLIC, | ||
| [Networks.TESTNET]: Networks.TESTNET, | ||
| }; | ||
|
|
||
| // Stands in for albedo.link answering a tx intent: it signs the XDR with the | ||
| // user's account for the requested network ("public" when none is given) and | ||
| // echoes that network back. | ||
| export function albedoSigningWith(signer: Keypair) { | ||
| return ({ xdr: envelope, network }: { xdr: string; network?: string }) => { | ||
| const responseNetwork = network || "public"; | ||
| const tx = TransactionBuilder.fromXdr( | ||
| envelope, | ||
| albedoPassphrases[responseNetwork], | ||
| ) as Transaction; | ||
| tx.sign(signer); | ||
|
|
||
| return Promise.resolve({ | ||
| intent: "tx", | ||
| pubkey: signer.publicKey(), | ||
| network: responseNetwork, | ||
| xdr: envelope, | ||
| tx_hash: xdr.encodeBytes(tx.hash(), "hex"), | ||
| signed_envelope_xdr: tx.toXdr(), | ||
| }); | ||
| }; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.