Skip to content
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

refactor(rn): clean things up so they're more digestable #1134

Open
wants to merge 1 commit into
base: moldy/rn-base
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ site/.vitepress/cache/**/*
account-kit/rn-signer/lib/*
**/android/generated/*
**/ios/generated/*
**/build/*
**/.cxx/*
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
check(pkKeyData.typeUrl == "type.googleapis.com/google.crypto.tink.HpkePrivateKey") {
"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
Loading