Skip to content

Commit

Permalink
refactor(rn): clean things up so they're more digestable
Browse files Browse the repository at this point in the history
  • Loading branch information
moldy530 committed Nov 11, 2024
1 parent e283195 commit 9d7842c
Show file tree
Hide file tree
Showing 19 changed files with 362 additions and 6,564 deletions.
4 changes: 3 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ site/.vitepress/cache/**/*

account-kit/rn-signer/lib/*
**/android/generated/*
**/ios/generated/*
**/ios/generated/*
**/build/*
**/.cxx/*
13 changes: 1 addition & 12 deletions .github/actions/setup-node/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,4 @@ runs:

- name: Install dependencies
run: yarn install --frozen-lockfile
shell: bash

- name: Cache Nx and Node modules
id: cache-nx
uses: actions/cache@v3
with:
path: |
.nx
node_modules/.cache/nx
key: ${{ runner.os }}-nx-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-nx-
shell: bash
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dist
node_modules
tsconfig*.tsbuildinfo
.idea/
.nx

# local env files
.env
Expand Down
21 changes: 6 additions & 15 deletions account-kit/rn-signer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,14 @@ React Native compatible Alchemy signer impl
npm install @account-kit/react-native-signer
```

## Usage

```js
import { multiply } from "@account-kit/react-native-signer";

// ...

const result = multiply(3, 7);
```

## Contributing

See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.

## License

MIT
1. Clone the repo
1. run `yarn` in the root of the repo
1. make changes in `rn-signer/src`
1. cd into `account-kit/rn-signer/example`
1. add a `.env` file which contains `API_KEY={alchemy_api_key}`. This API Key needs to correspond to an app that has Embedded Accounts enabled (https://dashboard.alchemy.com/accounts).
1. run `yarn android` to start the example app. You will need to make sure you do the env setup here: https://reactnative.dev/docs/set-up-your-environment to be able to run the app on android

---

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.accountkit.reactnativesigner

import com.google.crypto.tink.CleartextKeysetHandle
import com.google.crypto.tink.InsecureSecretKeyAccess
import com.google.crypto.tink.KeysetHandle
import com.google.crypto.tink.hybrid.HpkeParameters
import com.google.crypto.tink.hybrid.HpkePrivateKey
import com.google.crypto.tink.hybrid.HpkePublicKey
import com.google.crypto.tink.subtle.EllipticCurves
import com.google.crypto.tink.util.Bytes
import com.google.crypto.tink.util.SecretBytes
import java.security.interfaces.ECPublicKey
import javax.xml.bind.DatatypeConverter
import com.google.crypto.tink.proto.HpkePrivateKey as ProtoHpkePrivateKey
import com.google.crypto.tink.proto.HpkePublicKey as ProtoHpkePublicKey

// Keyset Handle Extensions
fun KeysetHandle.toHpkePublicKey(hpkeParameters: HpkeParameters): HpkePublicKey {
val keySet = CleartextKeysetHandle.getKeyset(this.publicKeysetHandle)
val protoKey = ProtoHpkePublicKey.parseFrom(keySet.keyList[0].keyData.value)

return HpkePublicKey.create(
hpkeParameters,
Bytes.copyFrom(protoKey.publicKey.toByteArray()),
null
)
}

fun KeysetHandle.toHpkePrivateKey(hpkeParams: HpkeParameters): HpkePrivateKey {
val publicKey = this.toHpkePublicKey(hpkeParams)
val pkKs = CleartextKeysetHandle.getKeyset(this)
val pkKeyData = pkKs.keyList[0].keyData
if (pkKeyData.typeUrl != "type.googleapis.com/google.crypto.tink.HpkePrivateKey") {
throw Error("invalid key type")
}

return HpkePrivateKey.create(
HpkePublicKey.create(
hpkeParams,
Bytes.copyFrom(publicKey.toByteArray()),
null
),
SecretBytes.copyFrom(
ProtoHpkePrivateKey.parseFrom(pkKeyData.value).privateKey.toByteArray(),
InsecureSecretKeyAccess.get()
)
)
}

// HPKE Public Key Extensions
fun HpkePublicKey.toHex(): String {
return this.toByteArray().toHex()
}

fun HpkePublicKey.toByteArray(): ByteArray {
return this.publicKeyBytes.toByteArray()
}

// ECPublicKey Extensions
fun ECPublicKey.toBytes(
pfType: EllipticCurves.PointFormatType
): ByteArray {
return EllipticCurves.pointEncode(
this.params.curve,
pfType,
this.w
)
}

// Conversions from Hex <-> byte[]
fun String.fromHex(): ByteArray {
return DatatypeConverter.parseHexBinary(this)
}

fun ByteArray.toHex(): String {
return DatatypeConverter.printHexBinary(this)
}
Loading

0 comments on commit 9d7842c

Please sign in to comment.