-
Notifications
You must be signed in to change notification settings - Fork 92
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
wip: solana native support #702
Draft
gomesalexandre
wants to merge
24
commits into
master
Choose a base branch
from
feat_solana_native
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+328
−13
Draft
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
9dbcc45
wip: solana native support
gomesalexandre b15b110
fix: shit
gomesalexandre 5823787
fix: shit
gomesalexandre 6707876
fix: toBase58
gomesalexandre 2009e84
feat: revert back toString()
gomesalexandre 2c1e92b
Revert "feat: revert back toString()"
gomesalexandre a061c43
feat: last try
gomesalexandre c551b91
fix: for real last try ffs
gomesalexandre d8d6c78
feat: last last try I swear fml
gomesalexandre 2aaeea6
fix: ok sleeping it off was a good idea, she works
gomesalexandre 96835d3
fix: sign use 0x02-stripped pubkey
gomesalexandre a59c5a1
feat: I have no idea what I am doing
gomesalexandre 029ce81
fix: versions
gomesalexandre e900ec4
fix: still no idea what I'm doing btw
gomesalexandre 61faceb
fix: segments
gomesalexandre dc162a9
feat: make it work (highly doubt it but hey)
gomesalexandre 62434a3
fix: try and fix initialize
gomesalexandre 2996b2c
fix: fml
gomesalexandre d60beb2
fix: maybe?
gomesalexandre acfc891
fix: maybe??
gomesalexandre c3f3a3b
fix: maybe???
gomesalexandre b07f3bd
fix: maybe????
gomesalexandre a3f0af4
fix: maybe?????
gomesalexandre 5f7eb93
feat: rage quit
gomesalexandre 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 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 |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
}, | ||
"dependencies": { | ||
"@bitcoinerlab/secp256k1": "^1.1.1", | ||
"@noble/curves": "^1.4.0", | ||
"@shapeshiftoss/bitcoinjs-lib": "7.0.0-shapeshift.0", | ||
"@shapeshiftoss/fiosdk": "1.2.1-shapeshift.6", | ||
"@shapeshiftoss/hdwallet-core": "1.57.1", | ||
|
@@ -57,5 +58,6 @@ | |
"bs58": "^4.0.1", | ||
"cosmjs-types": "^0.4.1", | ||
"msw": "^0.27.1" | ||
} | ||
}, | ||
"gitHead": "a59c5a12b265b6f64c65920cf330358a250227c2" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. revert before opening me |
||
} |
41 changes: 41 additions & 0 deletions
41
packages/hdwallet-native/src/crypto/isolation/adapters/bip32ed25519.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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Ed25519Node } from "../core/ed25519"; | ||
import { ByteArray } from "../types"; | ||
|
||
export class BIP32Ed25519Adapter { | ||
readonly node: Ed25519Node; | ||
|
||
private constructor(node: Ed25519Node) { | ||
this.node = node; | ||
} | ||
|
||
static async fromNode(node: Ed25519Node): Promise<BIP32Ed25519Adapter> { | ||
return new BIP32Ed25519Adapter(node); | ||
} | ||
|
||
async getPublicKey(): Promise<ByteArray> { | ||
const publicKey = await this.node.getPublicKey(); | ||
return Buffer.from(publicKey); | ||
} | ||
|
||
async derivePath(path: string): Promise<BIP32Ed25519Adapter> { | ||
let currentNode = this.node; | ||
|
||
if (path === "m" || path === "M" || path === "m'" || path === "M'") { | ||
return this; | ||
} | ||
|
||
const segments = path | ||
.toLowerCase() | ||
.split("/") | ||
.filter((segment) => segment !== "m"); | ||
|
||
for (const segment of segments) { | ||
const index = parseInt(segment.replace("'", "")); | ||
currentNode = await currentNode.derive(index); | ||
} | ||
|
||
return new BIP32Ed25519Adapter(currentNode); | ||
} | ||
} | ||
|
||
export default BIP32Ed25519Adapter; |
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
36 changes: 36 additions & 0 deletions
36
packages/hdwallet-native/src/crypto/isolation/adapters/solana.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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as core from "@shapeshiftoss/hdwallet-core"; | ||
import { PublicKey, VersionedTransaction } from "@solana/web3.js"; | ||
|
||
import BIP32Ed25519Adapter from "./bip32ed25519"; | ||
|
||
export class SolanaDirectAdapter { | ||
protected readonly nodeAdapter: BIP32Ed25519Adapter; | ||
|
||
constructor(nodeAdapter: BIP32Ed25519Adapter) { | ||
this.nodeAdapter = nodeAdapter; | ||
} | ||
|
||
async getAddress(addressNList: core.BIP32Path): Promise<string> { | ||
const nodeAdapter = await this.nodeAdapter.derivePath(core.addressNListToBIP32(addressNList)); | ||
const publicKeyBuffer = await nodeAdapter.getPublicKey(); | ||
|
||
const bufferForHex = Buffer.from(publicKeyBuffer); | ||
|
||
const pubKey = new PublicKey(bufferForHex); | ||
|
||
return pubKey.toBase58(); | ||
} | ||
|
||
async signDirect(transaction: VersionedTransaction, addressNList: core.BIP32Path): Promise<VersionedTransaction> { | ||
const nodeAdapter = await this.nodeAdapter.derivePath(core.addressNListToBIP32(addressNList)); | ||
const pubkey = await this.getAddress(addressNList); | ||
|
||
const messageToSign = transaction.message.serialize(); | ||
const signature = await nodeAdapter.node.sign(messageToSign); | ||
|
||
transaction.addSignature(new PublicKey(pubkey), signature); | ||
return transaction; | ||
} | ||
} | ||
|
||
export default SolanaDirectAdapter; |
2 changes: 2 additions & 0 deletions
2
packages/hdwallet-native/src/crypto/isolation/core/bip32/interfaces.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
77 changes: 77 additions & 0 deletions
77
packages/hdwallet-native/src/crypto/isolation/core/ed25519/index.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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { ExtPointConstructor } from "@noble/curves/abstract/edwards"; | ||
import { ed25519 } from "@noble/curves/ed25519"; | ||
import * as bip32crypto from "bip32/src/crypto"; | ||
|
||
import { Revocable, revocable } from "../../engines/default/revocable"; | ||
import { ByteArray } from "../../types"; | ||
|
||
export type Ed25519Key = { | ||
getPublicKey(): Promise<ByteArray>; | ||
sign(message: Uint8Array): Promise<ByteArray>; | ||
verify(message: Uint8Array, signature: Uint8Array): Promise<boolean>; | ||
}; | ||
|
||
export class Ed25519Node extends Revocable(class {}) { | ||
readonly #privateKey: ByteArray; | ||
readonly #chainCode: ByteArray; | ||
readonly explicitPath?: string; | ||
|
||
protected constructor(privateKey: ByteArray, chainCode: ByteArray, explicitPath?: string) { | ||
super(); | ||
this.#privateKey = privateKey; | ||
this.#chainCode = chainCode; | ||
this.explicitPath = explicitPath; | ||
} | ||
|
||
static async create(privateKey: ByteArray, chainCode: ByteArray, explicitPath?: string): Promise<Ed25519Node> { | ||
const obj = new Ed25519Node(privateKey, chainCode, explicitPath); | ||
return revocable(obj, (x) => obj.addRevoker(x)); | ||
} | ||
|
||
async getPublicKey(): Promise<ByteArray> { | ||
// Generate public key from private key | ||
return Buffer.from(ed25519.getPublicKey(this.#privateKey)); | ||
} | ||
|
||
async getChainCode(): Promise<ByteArray> { | ||
return this.#chainCode; | ||
} | ||
|
||
async sign(message: Uint8Array): Promise<ByteArray> { | ||
return Buffer.from(ed25519.sign(message, this.#privateKey)); | ||
} | ||
|
||
async derive(index: number): Promise<Ed25519Node> { | ||
// Ensure hardened derivation | ||
if (index < 0x80000000) { | ||
index += 0x80000000; | ||
} | ||
|
||
const indexBuffer = Buffer.alloc(4); | ||
indexBuffer.writeUInt32BE(index, 0); | ||
|
||
// SLIP-0010 for Ed25519 | ||
const data = Buffer.concat([Buffer.from([0x00]), Buffer.from(this.#privateKey), indexBuffer]); | ||
Comment on lines
+53
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's the part we probably got wrong |
||
|
||
const I = bip32crypto.hmacSHA512(Buffer.from(this.#chainCode), data); | ||
const IL = I.slice(0, 32); | ||
const IR = I.slice(32); | ||
|
||
// Ed25519 clamping | ||
IL[0] &= 0xf8; | ||
IL[31] &= 0x7f; | ||
IL[31] |= 0x40; | ||
|
||
const path = this.explicitPath | ||
? `${this.explicitPath}/${index >= 0x80000000 ? index - 0x80000000 + "'" : index}` | ||
: undefined; | ||
|
||
return Ed25519Node.create(IL, IR, path); | ||
} | ||
} | ||
|
||
export type Point = ExtPointConstructor; | ||
export const Ed25519Point = { | ||
BASE_POINT: ed25519.getPublicKey(new Uint8Array(32)), | ||
}; | ||
export type { Point as ExtendedPoint }; |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note @noble/ed25519 is also available as a lightweight version but @noble/curves has more safety/features implemented