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

support BIP39 seeds for TON #23

Merged
merged 1 commit into from
Feb 24, 2025
Merged
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
28 changes: 2 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions packages/staking-cli/src/cmd/ton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ async function init (
addressDerivationFn: TonNominatorPoolStaker.getAddressDerivationFn({
addressDerivationConfig: networkConfig.addressDerivationConfig
}),
mnemonicToSeedFn: TonNominatorPoolStaker.getMnemonicToSeedFn(),
seedToKeypairFn: TonNominatorPoolStaker.getSeedToKeypairFn(),
mnemonicToSeedFn: TonNominatorPoolStaker.getMnemonicToSeedFn({
addressDerivationConfig: networkConfig.addressDerivationConfig
}),
seedToKeypairFn: TonNominatorPoolStaker.getSeedToKeypairFn({
addressDerivationConfig: networkConfig.addressDerivationConfig
}),
keyType: KeyType.ED25519,
logger: defaultLogger
})
Expand Down
5 changes: 2 additions & 3 deletions packages/ton/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@
"dependencies": {
"@chorus-one/signer": "^1.0.0",
"@chorus-one/utils": "^1.0.0",
"@noble/curves": "^1.4.0",
"@ton/crypto": "^3.3.0",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works everywhere: Browser, NodeJS and React Native

that dep shall be fine

"@ton/ton": "^15.1.0",
"axios": "^1.7.2",
"tonweb-mnemonic": "^1.0.1"
"axios": "^1.7.2"
}
}
48 changes: 38 additions & 10 deletions packages/ton/src/TonBaseStaker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import {
beginCell,
storeMessage
} from '@ton/ton'
import { mnemonicToSeed, deriveEd25519Path, keyPairFromSeed } from '@ton/crypto'
import { pbkdf2_sha512 } from '@ton/crypto-primitives'
import { TonClient } from './TonClient'
import { createWalletTransferV4, externalMessage, sign } from './tx'
import * as tonMnemonic from 'tonweb-mnemonic'
import { ed25519 } from '@noble/curves/ed25519'

/**
* This class provides the functionality to stake assets on the Ton network.
Expand Down Expand Up @@ -67,12 +67,24 @@ export class TonBaseStaker {
*
* It can be used for signer initialization, e.g. `FireblocksSigner` or `LocalSigner`.
*
* @param params.addressDerivationConfig - TON address derivation configuration
*
* @returns Returns a seed derived from the mnemonic
*/
static getMnemonicToSeedFn =
() =>
(params?: { addressDerivationConfig: AddressDerivationConfig | undefined }) =>
async (mnemonic: string, password?: string): Promise<Uint8Array> => {
return await tonMnemonic.mnemonicToSeed(mnemonic.split(' '), password)
const { isBIP39 } = params?.addressDerivationConfig ?? defaultAddressDerivationConfig()

// the logic is based on the following implementation:
// https://github.com/xssnick/tonutils-go/blob/619c2aa1f6b992997bf322f8f9bfc4ae036a5181/ton/wallet/seed.go#L82
if (isBIP39) {
const pass = password ?? ''
return await pbkdf2_sha512(mnemonic, 'mnemonic' + pass, 2048, 64)
}

const seed = await mnemonicToSeed(mnemonic.split(' '), 'TON default seed', password)
return seed.slice(0, 32)
}

/**
Expand All @@ -81,18 +93,33 @@ export class TonBaseStaker {
*
* It can be used for signer initialization, e.g. `FireblocksSigner` or `LocalSigner`.
*
* @param params.addressDerivationConfig - TON address derivation configuration
*
* @returns Returns a public and private keypair derived from the seed
*/
static getSeedToKeypairFn =
() =>
(params?: { addressDerivationConfig: AddressDerivationConfig | undefined }) =>
async (seed: Uint8Array, hdPath?: string): Promise<{ publicKey: Uint8Array; privateKey: Uint8Array }> => {
if (hdPath !== undefined && hdPath !== '') {
throw new Error('hdPath is not supported for TON')
const { isBIP39 } = params?.addressDerivationConfig ?? defaultAddressDerivationConfig()

// the logic is based on the following implementation:
// https://github.com/xssnick/tonutils-go/blob/619c2aa1f6b992997bf322f8f9bfc4ae036a5181/ton/wallet/seed.go#L82

let newSeed = Buffer.from(seed)
if (isBIP39) {
const path = hdPath
? hdPath
.replace('m/', '')
.split('/')
.map((x) => parseInt(x))
: []
newSeed = await deriveEd25519Path(newSeed, path)
}

const keypair = keyPairFromSeed(newSeed)
return {
publicKey: ed25519.getPublicKey(Buffer.from(seed)),
privateKey: seed
publicKey: keypair.publicKey,
privateKey: keypair.secretKey.slice(0, 32)
}
}

Expand Down Expand Up @@ -543,7 +570,8 @@ function defaultAddressDerivationConfig (): AddressDerivationConfig {
workchain: 0,
bounceable: false,
testOnly: false,
urlSafe: true
urlSafe: true,
isBIP39: false
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/ton/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export interface AddressDerivationConfig {
bounceable: boolean
testOnly: boolean
urlSafe: boolean
isBIP39: boolean
}

export interface Message {
Expand Down