-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PSBT Support Added for Blockcore Wallet (#350)
* PSBT signing added * Fixed workflow error * Added testnet3 * PSBT support completed * Update README.md Added link for PSBT Test guide
- Loading branch information
Showing
19 changed files
with
257 additions
and
12 deletions.
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 |
---|---|---|
|
@@ -7,6 +7,9 @@ on: | |
- main | ||
workflow_dispatch: | ||
|
||
permissions: | ||
contents: write | ||
|
||
jobs: | ||
build: | ||
name: Build, Test and Release | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.signing-content { | ||
height: 120px; | ||
} |
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,5 @@ | ||
<br />{{ "Action.AttestContentDescription" | translate }}: <br /><br /> | ||
<mat-form-field class="input-full-width" appearance="outline"> | ||
<mat-label>{{ "Action.SigningPSBTLabel" | translate }}</mat-label> | ||
<textarea class="signing-content" readonly="readonly" matInput [ngModel]="content"></textarea> | ||
</mat-form-field> |
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,25 @@ | ||
import { Component, OnInit, OnDestroy } from '@angular/core'; | ||
import { UIState } from '../../services'; | ||
import { ActionService } from 'src/app/services/action.service'; | ||
|
||
@Component({ | ||
selector: 'app-sign-psbt', | ||
templateUrl: './signpsbt.component.html', | ||
styleUrls: ['./signpsbt.component.css'], | ||
}) | ||
export class ActionSignPsbtComponent implements OnInit, OnDestroy { | ||
content: string; | ||
|
||
constructor(public uiState: UIState, public actionService: ActionService) { | ||
this.actionService.consentType = 'regular'; | ||
} | ||
|
||
ngOnDestroy(): void { } | ||
|
||
ngOnInit(): void { | ||
// The content to be signed will be a PSBT represented in a string | ||
|
||
this.content = this.uiState.action.content; | ||
|
||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { BackgroundManager } from '../background-manager'; | ||
import { ActionPrepareResult, ActionResponse, Permission } from '../interfaces'; | ||
import { ActionHandler, ActionState } from './action-handler'; | ||
import { HDKey } from '@scure/bip32'; | ||
import { Network } from '../networks'; | ||
import { Psbt } from '@blockcore/blockcore-js'; | ||
import BIP32Factory from 'bip32'; | ||
import ecc from '@bitcoinerlab/secp256k1'; | ||
const Bip32 = BIP32Factory(ecc); | ||
import { bip32 } from '../noble-ecc-wrapper'; | ||
|
||
// const testHDKey = "tprv8ZgxMBicQKsPf5D3jKXvvvsRi3RMZvE8g98752W3KeCUeggbyf8HQ3BJjppWRrHVPhkgKefZZD8x1jCQSHkoSLaagVNWPBfgTtJVhaRewR5"; | ||
|
||
export class SignPsbtHandler implements ActionHandler { | ||
action = ['signPsbt']; | ||
|
||
constructor(private backgroundManager: BackgroundManager) { } | ||
|
||
private hexToBase64(hex: string): string { | ||
const buffer = Buffer.from(hex, 'hex'); | ||
return buffer.toString('base64'); | ||
} | ||
|
||
async signPsbt(network: Network, content: string, xPrivKey: string): Promise<string> { | ||
// console.debug("PSBT Handler has been called"); | ||
var hexPsbt = content; | ||
const psbtBase64 = this.hexToBase64(hexPsbt); | ||
// const network = { | ||
// messagePrefix: "\u0018Bitcoin Signed Message:\n", | ||
// bech32: "tb", | ||
// bip32: { | ||
// public: 70617039, | ||
// private: 70615956, | ||
// }, | ||
// pubKeyHash: 111, | ||
// scriptHash: 196, | ||
// wif: 239, | ||
// }; | ||
const psbt = Psbt.fromBase64(psbtBase64, { | ||
network, | ||
}); | ||
// console.log("psbt: ", psbt); | ||
const hdKeyPair = Bip32.fromBase58(xPrivKey, network); | ||
// console.log("HD key Pair: ", hdKeyPair); | ||
const SignedPsbt = psbt.signAllInputsHD(hdKeyPair); | ||
// console.log(SignedPsbt); | ||
|
||
return SignedPsbt.toBase64(); | ||
} | ||
|
||
|
||
async prepare(state: ActionState): Promise<ActionPrepareResult> { | ||
if (!state.message || !state.message.request || !state.message.request.params || !state.message.request.params[0] || !state.message.request.params[0].message) { | ||
throw Error('The params must include a single entry that has a message field.'); | ||
} | ||
|
||
return { | ||
content: state.message.request.params[0].message, | ||
consent: true, | ||
}; | ||
} | ||
|
||
async execute(state: ActionState, permission: Permission): Promise<ActionResponse> { | ||
// Get the private key | ||
const { node, network } = await this.backgroundManager.getKey(permission.walletId, permission.accountId, permission.keyId); | ||
// console.log("HD Key pair in Blockcore wallet: ", node); | ||
const masterSeedBase64 = await this.backgroundManager.getMasterSeedBase64(permission.walletId) | ||
const masterSeed = Buffer.from(masterSeedBase64, 'base64'); | ||
const hdNode = bip32.fromSeed(masterSeed, network); | ||
const xPrivKey = hdNode.toBase58(); | ||
|
||
if (state.content) { | ||
let contentText = state.content; | ||
|
||
if (typeof state.content !== 'string') { | ||
contentText = JSON.stringify(state.content); | ||
} | ||
|
||
let signedData = await this.signPsbt(network, contentText as string, xPrivKey); | ||
|
||
return { key: permission.key, walletId: permission.walletId, accountId: permission.accountId, response: { signature: signedData, content: state.content }, network: network.id }; | ||
} else { | ||
return { key: '', response: null, network: network.id }; | ||
} | ||
} | ||
} |
Oops, something went wrong.