-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ (signer-solana) [DSDK-554]: Add GetAddressUseCase (#451)
- Loading branch information
Showing
23 changed files
with
389 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@ledgerhq/device-sdk-sample": minor | ||
--- | ||
|
||
Add GetAddress Solana Signer use case |
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,5 @@ | ||
--- | ||
"@ledgerhq/device-signer-kit-solana": minor | ||
--- | ||
|
||
Add GetAddressUseCase |
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,11 @@ | ||
"use client"; | ||
import React from "react"; | ||
|
||
import { SessionIdWrapper } from "@/components/SessionIdWrapper"; | ||
import { SignerSolanaView } from "@/components/SignerSolanaView"; | ||
|
||
const Signer: React.FC = () => { | ||
return <SessionIdWrapper ChildComponent={SignerSolanaView} />; | ||
}; | ||
|
||
export default Signer; |
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,58 @@ | ||
import React, { useMemo } from "react"; | ||
import { | ||
GetAddressDAError, | ||
GetAddressDAIntermediateValue, | ||
GetAddressDAOutput, | ||
SignerSolanaBuilder, | ||
} from "@ledgerhq/device-signer-kit-solana"; | ||
|
||
import { DeviceActionsList } from "@/components/DeviceActionsView/DeviceActionsList"; | ||
import { DeviceActionProps } from "@/components/DeviceActionsView/DeviceActionTester"; | ||
import { useSdk } from "@/providers/DeviceSdkProvider"; | ||
|
||
const DEFAULT_DERIVATION_PATH = "44'/501'"; | ||
|
||
export const SignerSolanaView: React.FC<{ sessionId: string }> = ({ | ||
sessionId, | ||
}) => { | ||
const sdk = useSdk(); | ||
const signer = new SignerSolanaBuilder({ sdk, sessionId }).build(); | ||
|
||
const deviceModelId = sdk.getConnectedDevice({ | ||
sessionId, | ||
}).modelId; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const deviceActions: DeviceActionProps<any, any, any, any>[] = useMemo( | ||
() => [ | ||
{ | ||
title: "Get address", | ||
description: | ||
"Perform all the actions necessary to get a Solana address from the device", | ||
executeDeviceAction: ({ derivationPath, checkOnDevice }) => { | ||
return signer.getAddress(derivationPath, { | ||
checkOnDevice, | ||
}); | ||
}, | ||
initialValues: { | ||
derivationPath: DEFAULT_DERIVATION_PATH, | ||
checkOnDevice: false, | ||
}, | ||
deviceModelId, | ||
} satisfies DeviceActionProps< | ||
GetAddressDAOutput, | ||
{ | ||
derivationPath: string; | ||
checkOnDevice?: boolean; | ||
}, | ||
GetAddressDAError, | ||
GetAddressDAIntermediateValue | ||
>, | ||
], | ||
[deviceModelId, signer], | ||
); | ||
|
||
return ( | ||
<DeviceActionsList title="Solana Signer" deviceActions={deviceActions} /> | ||
); | ||
}; |
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,8 @@ | ||
export type { | ||
GetAddressDAError, | ||
GetAddressDAIntermediateValue, | ||
GetAddressDAOutput, | ||
GetAddressDAReturnType, | ||
} from "@api/app-binder/GetAddressDeviceActionTypes"; | ||
export type { SignerSolana } from "@api/SignerSolana"; | ||
export { SignerSolanaBuilder } from "@api/SignerSolanaBuilder"; |
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
// inversify requirement | ||
import "reflect-metadata"; | ||
|
||
export * from "@api/index"; |
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
139 changes: 138 additions & 1 deletion
139
packages/signer/signer-solana/src/internal/app-binder/SolanaAppBinder.test.ts
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 |
---|---|---|
@@ -1,10 +1,147 @@ | ||
import { DeviceSdk, DeviceSessionId } from "@ledgerhq/device-management-kit"; | ||
import { | ||
DeviceActionState, | ||
DeviceActionStatus, | ||
DeviceSdk, | ||
DeviceSessionId, | ||
SendCommandInAppDeviceAction, | ||
UserInteractionRequired, | ||
} from "@ledgerhq/device-management-kit"; | ||
import { from } from "rxjs"; | ||
|
||
import { | ||
GetAddressDAError, | ||
GetAddressDAIntermediateValue, | ||
GetAddressDAOutput, | ||
} from "@api/index"; | ||
|
||
import { GetPubKeyCommand } from "./command/GetPubKeyCommand"; | ||
import { SolanaAppBinder } from "./SolanaAppBinder"; | ||
|
||
describe("SolanaAppBinder", () => { | ||
const mockedSdk: DeviceSdk = { | ||
sendCommand: jest.fn(), | ||
executeDeviceAction: jest.fn(), | ||
} as unknown as DeviceSdk; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should be defined", () => { | ||
const binder = new SolanaAppBinder({} as DeviceSdk, {} as DeviceSessionId); | ||
expect(binder).toBeDefined(); | ||
}); | ||
|
||
describe("getAddress", () => { | ||
it("should return the address", (done) => { | ||
// GIVEN | ||
const address = "D2PPQSYFe83nDzk96FqGumVU8JA7J8vj2Rhjc2oXzEi5"; | ||
|
||
jest.spyOn(mockedSdk, "executeDeviceAction").mockReturnValue({ | ||
observable: from([ | ||
{ | ||
status: DeviceActionStatus.Completed, | ||
output: address, | ||
} as DeviceActionState< | ||
GetAddressDAOutput, | ||
GetAddressDAError, | ||
GetAddressDAIntermediateValue | ||
>, | ||
]), | ||
cancel: jest.fn(), | ||
}); | ||
|
||
// WHEN | ||
const appBinder = new SolanaAppBinder(mockedSdk, "sessionId"); | ||
const { observable } = appBinder.getAddress({ | ||
derivationPath: "44'/501'", | ||
checkOnDevice: false, | ||
}); | ||
|
||
// THEN | ||
const states: DeviceActionState< | ||
GetAddressDAOutput, | ||
GetAddressDAError, | ||
GetAddressDAIntermediateValue | ||
>[] = []; | ||
observable.subscribe({ | ||
next: (state) => { | ||
states.push(state); | ||
}, | ||
error: (err) => { | ||
done(err); | ||
}, | ||
complete: () => { | ||
try { | ||
expect(states).toEqual([ | ||
{ | ||
status: DeviceActionStatus.Completed, | ||
output: address, | ||
}, | ||
]); | ||
done(); | ||
} catch (err) { | ||
done(err); | ||
} | ||
}, | ||
}); | ||
}); | ||
|
||
describe("calls of executeDeviceAction with the correct params", () => { | ||
const baseParams = { | ||
derivationPath: "44'/60'/3'/2/1", | ||
returnChainCode: false, | ||
}; | ||
|
||
it("when checkOnDevice is true: UserInteractionRequired.VerifyAddress", () => { | ||
// GIVEN | ||
const checkOnDevice = true; | ||
const params = { | ||
...baseParams, | ||
checkOnDevice, | ||
}; | ||
|
||
// WHEN | ||
const appBinder = new SolanaAppBinder(mockedSdk, "sessionId"); | ||
appBinder.getAddress(params); | ||
|
||
// THEN | ||
expect(mockedSdk.executeDeviceAction).toHaveBeenCalledWith({ | ||
sessionId: "sessionId", | ||
deviceAction: new SendCommandInAppDeviceAction({ | ||
input: { | ||
command: new GetPubKeyCommand(params), | ||
appName: "Solana", | ||
requiredUserInteraction: UserInteractionRequired.VerifyAddress, | ||
}, | ||
}), | ||
}); | ||
}); | ||
|
||
it("when checkOnDevice is false: UserInteractionRequired.None", () => { | ||
// GIVEN | ||
const checkOnDevice = false; | ||
const params = { | ||
...baseParams, | ||
checkOnDevice, | ||
}; | ||
|
||
// WHEN | ||
const appBinder = new SolanaAppBinder(mockedSdk, "sessionId"); | ||
appBinder.getAddress(params); | ||
|
||
// THEN | ||
expect(mockedSdk.executeDeviceAction).toHaveBeenCalledWith({ | ||
sessionId: "sessionId", | ||
deviceAction: new SendCommandInAppDeviceAction({ | ||
input: { | ||
command: new GetPubKeyCommand(params), | ||
appName: "Solana", | ||
requiredUserInteraction: UserInteractionRequired.None, | ||
}, | ||
}), | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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
Empty file.
Oops, something went wrong.