# AirMoney Controller SDK

A TypeScript SDK for interacting with AirMoney device. This SDK provides core functionality to control device screens, handle key events, and perform cryptographic operations.

## Development

### Prerequisites for Development

Before developing with this SDK, make sure you have the following installed:

- Node.js (v18 or higher)
- Yarn package manager

### Development Workflow

1. **Clone and Install Dependencies**
   ```bash
   git clone <repository-url>
   cd controller-sdk
   yarn install
   ```

2. **Start Development Mode**
   ```bash
   yarn dev
   ```
   This command will build the SDK and provide instructions for local testing.

3. **Make Changes and Test**
   - Make your changes to the SDK source code
   - Run `yarn dev` again to rebuild the package
   - Test your changes in your consuming project

### Available Scripts

- `yarn dev` - Build the SDK for development
- `yarn build` - Build the SDK for production
- `yarn preview` - Preview the built SDK locally
- `yarn lint` - Run ESLint to check code quality
- `yarn check-types` - Run TypeScript type checking
- `yarn changeset` - Create a changeset for version management
- `yarn version` - Apply changesets and update version
- `yarn release` - Build and publish to npm registry

### Code Quality

The project uses:
- **ESLint** for code linting with AirMoney's custom configuration
- **Prettier** for code formatting
- **TypeScript** for type safety
- **Changesets** for version management

Run linting and type checking:
```bash
yarn lint
yarn check-types
```

## Preview

To preview the built SDK locally:

```bash
yarn preview
```

This will start a local preview server where you can test the built SDK.

## Release

### Version Management

This project uses [Changesets](https://github.com/changesets/changesets) for version management.

### Creating a Release

1. **Create a Changeset**
   ```bash
   yarn changeset
   ```
   This will prompt you to:
   - Select which packages to include
   - Choose the type of change (patch, minor, major)
   - Write a summary of the changes

2. **Apply Changesets and Update Version**
   ```bash
   yarn version
   ```
   This will:
   - Apply all pending changesets
   - Update package versions
   - Update CHANGELOG.md

3. **Build and Publish**
   ```bash
   yarn release
   ```
   This will:
   - Build the SDK
   - Publish to npm registry

### Release Process

1. Make your changes and commit them
2. Create a changeset describing your changes
3. Run `yarn version` to update versions
4. Run `yarn release` to publish
5. Push changes and tags to the repository


## API Reference

### AirMoneyService

#### Constructor
```typescript
new AirMoneyService(options?: { [key in Methods]?: Options<key> })
```

#### Methods

##### setImage
```typescript
setImage(params: { id: 'left' | 'right', imageName: string }): Promise<AMServiceScreenResponse>
```
Displays a static image on the specified screen. Supports throttling configuration.

##### setAnimate
```typescript
setAnimate(params: { id: 'left' | 'right', imageName: string }): Promise<AMServiceScreenResponse>
```
Displays an animated GIF on the specified screen. Supports throttling configuration.

##### initialize
```typescript
initialize(): Promise<void>
```
Initializes the service by loading the app name from metadata.json or window.AIRMONEY_APP_ID.

### AirMoneyKeyEvent

#### Constructor
```typescript
new AirMoneyKeyEvent(config?: Partial<AMKeyEventConfig>)
```

#### Configuration Options
```typescript
interface AMKeyEventConfig {
  threshold: number; // milliseconds for long press detection
  combinations?: Record<string, AMKey[]>; // key combinations
  doubleClicks?: Record<string, AMKey>; // double click keys
  debug?: boolean; // enable debug logging
}
```

#### Methods

##### on
```typescript
on(callback: AMKeyEventCallback): void
```
Registers a callback for key events.

##### off
```typescript
off(): void
```
Removes all event listeners and callbacks.

##### updateConfig
```typescript
updateConfig(config?: Partial<AMKeyEventConfig>): void
```
Updates the configuration of the key event handler.

### AirMoneyKeyEventManager

#### Constructor
```typescript
new AirMoneyKeyEventManager(config: { instance: AirMoneyKeyEvent, debug?: boolean })
```

#### Methods

##### subscribe
```typescript
subscribe(config: AMKeyEventTriggerGroupConfig): string
```
Registers a group of triggers with a config object. Returns a unique listener ID.

##### unsubscribe
```typescript
unsubscribe(listenerId: string): boolean
```
Unsubscribe from key events by group ID. Returns `true` if successfully unsubscribed.

##### unsubscribeAll
```typescript
unsubscribeAll(): void
```
Unsubscribe from all key events.

##### getKeyEventInstance
```typescript
getKeyEventInstance(): AirMoneyKeyEvent
```
Returns the underlying AirMoneyKeyEvent instance.

##### updateConfig
```typescript
updateConfig(config: Partial<AMKeyEventConfig>): void
```
Updates the configuration of the AirMoneyKeyEvent instance.

##### destroy
```typescript
destroy(): void
```
Unsubscribes all listeners and cleans up the manager and AirMoneyKeyEvent instance.

### AirMoneyCryptoService

#### Constructor
```typescript
new AirMoneyCryptoService()
```

#### EVM Methods

##### getDefaultEvmWallet
```typescript
getDefaultEvmWallet(): Promise<AMCryptoServiceGetDefaultEvmWalletResponse>
```
Gets the default EVM wallet address.

##### signEvmMessage
```typescript
signEvmMessage(params: { address: string, message: string }): Promise<AMCryptoServiceEvmMessageResponse>
```
Signs a message with the specified EVM wallet.

##### signEvmTransaction
```typescript
signEvmTransaction(params: { address: string, transaction: EVMTransaction, chainId: string }): Promise<AMCryptoServiceEvmTransactionResponse>
```
Signs an EVM transaction.

##### signGeneralEvmTransaction
```typescript
signGeneralEvmTransaction(params: { address: string, transaction: EVMTransaction, chainId: string }): Promise<AMCryptoServiceEvmTransactionResponse>
```
Signs a general EVM transaction.

##### signEip712TypedData
```typescript
signEip712TypedData(params: { address: string, domain: object, types: object, primaryType: string, message: object }): Promise<AMCryptoServiceEip712TypedDataResponse>
```
Signs EIP-712 typed data.

##### verifyEip1271Signature
```typescript
verifyEip1271Signature(params: { address: string, message: string, signature: string }): Promise<AMCryptoServiceVerifyEip1271SignatureResponse>
```
Verifies an EIP-1271 signature.

##### signEVMRawTransaction
```typescript
signEVMRawTransaction(params: { address: string, rawTransaction: RawEVMTransaction, chainId: string | number }): Promise<AMCryptoServiceEvmTransactionResponse>
```
Signs a raw EVM transaction.

#### Solana Methods

##### getDefaultSvmWallet
```typescript
getDefaultSvmWallet(): Promise<AMCryptoServiceGetDefaultSvmWalletResponse>
```
Gets the default Solana wallet address.

##### signSolanaMessage
```typescript
signSolanaMessage(params: { address: string, message: string }): Promise<AMCryptoServiceSolanaMessageResponse>
```
Signs a message with the specified Solana wallet.

##### signSolanaTransaction
```typescript
signSolanaTransaction(params: { address: string, transaction_base64: string }): Promise<AMCryptoServiceSolanaTransactionResponse>
```
Signs a Solana transaction.

#### Bitcoin Methods

##### getDefaultBitcoinWallet
```typescript
getDefaultBitcoinWallet(): Promise<AMCryptoServiceGetDefaultBitcoinWalletResponse>
```
Gets the default Bitcoin wallet address.

##### signBitcoinMessage
```typescript
signBitcoinMessage(params: { address: string, message: string }): Promise<AMCryptoServiceBitcoinMessageResponse>
```
Signs a Bitcoin message.

##### verifyBitcoinSignature
```typescript
verifyBitcoinSignature(params: { address: string, message: string, signature: string }): Promise<AMCryptoServiceVerifyBitcoinSignatureResponse>
```
Verifies a Bitcoin signature.

##### signBitcoinTransaction
```typescript
signBitcoinTransaction(params: { address: string, transaction: string }): Promise<AMCryptoServiceBitcoinTransactionResponse>
```
Signs a Bitcoin transaction.

### Utility Functions

#### Navigation Functions

##### backToHome
```typescript
backToHome(): void
```
Navigates back to the home page.

##### goToApp
```typescript
goToApp(appName?: string): void
```
Navigates to a specific app.

##### getAppLink
```typescript
getAppLink(appName?: string): string
```
Gets the link for an app.

##### displayAsset
```typescript
displayAsset(appName?: string, path?: string): string
```
Gets the display URL for an asset.

##### getAppLogo
```typescript
getAppLogo(appName?: string): string
```
Gets the logo URL for an app.

#### Transaction Functions

##### normalizeEVMTransaction
```typescript
normalizeEVMTransaction(obj: RawEVMTransaction): EVMTransaction
```
Normalizes an EVM transaction object.

##### toHexString
```typescript
toHexString(obj: bigint | number | string): HexString
```
Converts a value to a hex string.

## License

MIT © AirMoney
