@blooo/hw-app-acre
Advanced tools
Comparing version
@@ -126,2 +126,15 @@ import type { CreateTransactionArg } from "./createTransaction"; | ||
/** | ||
* Signs a ERC4361 hex-formatted message with the private key at | ||
* the provided derivation path according to the Bitcoin Signature format | ||
* and returns v, r, s. | ||
*/ | ||
signERC4361Message({ path, messageHex }: { | ||
path: string; | ||
messageHex: string; | ||
}): Promise<{ | ||
v: number; | ||
r: string; | ||
s: string; | ||
}>; | ||
/** | ||
* Calculates an output script along with public key and possible redeemScript | ||
@@ -128,0 +141,0 @@ * from a path and accountType. The accountPath must be a prefix of path. |
@@ -264,4 +264,2 @@ "use strict"; | ||
formatAcreWithdrawalData(withdrawalData) { | ||
console.log("withdrawalData", withdrawalData); | ||
console.log("dataLength", withdrawalData.data.length); | ||
const to = Buffer.from(this.cleanHexPrefix(withdrawalData.to.toString()), "hex").slice(-20); | ||
@@ -314,3 +312,2 @@ let withdrawalValueBuffer = Buffer.from(this.cleanHexPrefix(withdrawalData.value), "hex").slice(-32); | ||
const withdrawalDataBuffer = this.formatAcreWithdrawalData(withdrawalData); | ||
console.log("withdrawalDataBuffer", withdrawalDataBuffer); | ||
const sig = yield this.client.signWithdrawal(pathElements, withdrawalDataBuffer); | ||
@@ -329,2 +326,23 @@ const buf = Buffer.from(sig, "base64"); | ||
/** | ||
* Signs a ERC4361 hex-formatted message with the private key at | ||
* the provided derivation path according to the Bitcoin Signature format | ||
* and returns v, r, s. | ||
*/ | ||
signERC4361Message(_a) { | ||
return __awaiter(this, arguments, void 0, function* ({ path, messageHex }) { | ||
const pathElements = (0, bip32_1.pathStringToArray)(path); | ||
const message = Buffer.from(messageHex, "hex"); | ||
const sig = yield this.client.signERC4361Message(message, pathElements); | ||
const buf = Buffer.from(sig, "base64"); | ||
const v = buf.readUInt8() - 27 - 4; | ||
const r = buf.slice(1, 33).toString("hex"); | ||
const s = buf.slice(33, 65).toString("hex"); | ||
return { | ||
v, | ||
r, | ||
s, | ||
}; | ||
}); | ||
} | ||
/** | ||
* Calculates an output script along with public key and possible redeemScript | ||
@@ -331,0 +349,0 @@ * from a path and accountType. The accountPath must be a prefix of path. |
@@ -19,3 +19,4 @@ import Transport from "@ledgerhq/hw-transport"; | ||
signWithdrawal(pathElements: number[], withdrawalDataBuffer: AcreWithdrawalDataBuffer): Promise<string>; | ||
signERC4361Message(message: Buffer, pathElements: number[]): Promise<string>; | ||
} | ||
//# sourceMappingURL=appClient.d.ts.map |
@@ -30,2 +30,3 @@ "use strict"; | ||
BitcoinIns[BitcoinIns["SIGN_WITHDRAW"] = 17] = "SIGN_WITHDRAW"; | ||
BitcoinIns[BitcoinIns["SIGN_ERC4361_MESSAGE"] = 18] = "SIGN_ERC4361_MESSAGE"; | ||
})(BitcoinIns || (BitcoinIns = {})); | ||
@@ -182,4 +183,22 @@ var FrameworkIns; | ||
} | ||
signERC4361Message(message, pathElements) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (pathElements.length > 6) { | ||
throw new Error("Path too long. At most 6 levels allowed."); | ||
} | ||
const clientInterpreter = new clientCommands_1.ClientCommandInterpreter(() => { }); | ||
// prepare ClientCommandInterpreter | ||
const nChunks = Math.ceil(message.length / 64); | ||
const chunks = []; | ||
for (let i = 0; i < nChunks; i++) { | ||
chunks.push(message.subarray(64 * i, 64 * i + 64)); | ||
} | ||
clientInterpreter.addKnownList(chunks); | ||
const chunksRoot = new merkle_1.Merkle(chunks.map(m => (0, merkle_1.hashLeaf)(m))).getRoot(); | ||
const response = yield this.makeRequest(BitcoinIns.SIGN_ERC4361_MESSAGE, Buffer.concat([(0, bip32_1.pathElementsToBuffer)(pathElements), (0, varint_1.createVarint)(message.length), chunksRoot]), clientInterpreter); | ||
return response.toString("base64"); | ||
}); | ||
} | ||
} | ||
exports.AppClient = AppClient; | ||
//# sourceMappingURL=appClient.js.map |
{ | ||
"name": "@blooo/hw-app-acre", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Ledger Hardware Wallet Acre Application API", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
112
README.md
@@ -60,22 +60,28 @@ <img src="https://user-images.githubusercontent.com/4631227/191834116-59cf590e-25cc-4956-ae5c-812ea464f324.png" height="100" /> | ||
* [Parameters](#parameters-10) | ||
* [Examples](#examples-7) | ||
* [signERC4361Message](#signerc4361message) | ||
* [Parameters](#parameters-11) | ||
* [Examples](#examples-8) | ||
* [AcreBtcNew](#acrebtcnew) | ||
* [getWalletXpub](#getwalletxpub-1) | ||
* [Parameters](#parameters-11) | ||
* [Parameters](#parameters-12) | ||
* [getWalletPublicKey](#getwalletpublickey-1) | ||
* [Parameters](#parameters-12) | ||
* [Parameters](#parameters-13) | ||
* [createPaymentTransaction](#createpaymenttransaction-1) | ||
* [Parameters](#parameters-13) | ||
* [Parameters](#parameters-14) | ||
* [signMessage](#signmessage-1) | ||
* [Parameters](#parameters-14) | ||
* [Parameters](#parameters-15) | ||
* [signWithdrawal](#signwithdrawal-1) | ||
* [Parameters](#parameters-15) | ||
* [Parameters](#parameters-16) | ||
* [signERC4361Message](#signerc4361message-1) | ||
* [Parameters](#parameters-17) | ||
* [descrTemplFrom](#descrtemplfrom) | ||
* [Parameters](#parameters-16) | ||
* [Parameters](#parameters-18) | ||
* [AcreBtcOld](#acrebtcold) | ||
* [getWalletPublicKey](#getwalletpublickey-2) | ||
* [Parameters](#parameters-17) | ||
* [Examples](#examples-7) | ||
* [Parameters](#parameters-19) | ||
* [Examples](#examples-9) | ||
* [createPaymentTransaction](#createpaymenttransaction-2) | ||
* [Parameters](#parameters-18) | ||
* [Examples](#examples-8) | ||
* [Parameters](#parameters-20) | ||
* [Examples](#examples-10) | ||
* [CreateTransactionArg](#createtransactionarg) | ||
@@ -88,35 +94,35 @@ * [Properties](#properties) | ||
* [spendingCondition](#spendingcondition) | ||
* [Parameters](#parameters-19) | ||
* [Parameters](#parameters-21) | ||
* [setInput](#setinput) | ||
* [Parameters](#parameters-20) | ||
* [Parameters](#parameters-22) | ||
* [setOwnOutput](#setownoutput) | ||
* [Parameters](#parameters-21) | ||
* [Parameters](#parameters-23) | ||
* [getDescriptorTemplate](#getdescriptortemplate) | ||
* [SingleKeyAccount](#singlekeyaccount) | ||
* [getTaprootOutputKey](#gettaprootoutputkey) | ||
* [Parameters](#parameters-22) | ||
* [Parameters](#parameters-24) | ||
* [AppClient](#appclient) | ||
* [Parameters](#parameters-23) | ||
* [Parameters](#parameters-25) | ||
* [ClientCommandInterpreter](#clientcommandinterpreter) | ||
* [Parameters](#parameters-24) | ||
* [Parameters](#parameters-26) | ||
* [MerkelizedPsbt](#merkelizedpsbt) | ||
* [Parameters](#parameters-25) | ||
* [Parameters](#parameters-27) | ||
* [Merkle](#merkle) | ||
* [Parameters](#parameters-26) | ||
* [Parameters](#parameters-28) | ||
* [MerkleMap](#merklemap) | ||
* [Parameters](#parameters-27) | ||
* [Parameters](#parameters-29) | ||
* [WalletPolicy](#walletpolicy) | ||
* [Parameters](#parameters-28) | ||
* [Parameters](#parameters-30) | ||
* [extract](#extract) | ||
* [Parameters](#parameters-29) | ||
* [Parameters](#parameters-31) | ||
* [finalize](#finalize) | ||
* [Parameters](#parameters-30) | ||
* [Parameters](#parameters-32) | ||
* [clearFinalizedInput](#clearfinalizedinput) | ||
* [Parameters](#parameters-31) | ||
* [Parameters](#parameters-33) | ||
* [writePush](#writepush) | ||
* [Parameters](#parameters-32) | ||
* [Parameters](#parameters-34) | ||
* [PsbtV2](#psbtv2) | ||
* [serializeTransactionOutputs](#serializetransactionoutputs-1) | ||
* [Parameters](#parameters-33) | ||
* [Examples](#examples-9) | ||
* [Parameters](#parameters-35) | ||
* [Examples](#examples-11) | ||
* [SignP2SHTransactionArg](#signp2shtransactionarg) | ||
@@ -358,4 +364,2 @@ * [Properties](#properties-2) | ||
Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<{v: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number), r: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), s: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)}>**  | ||
##### Examples | ||
@@ -383,5 +387,30 @@ | ||
#### signERC4361Message | ||
Signs an Ethereum Sign-In (ERC-4361) message with the private key at | ||
the provided derivation path according to the Bitcoin Signature format | ||
and returns v, r, s. | ||
##### Parameters | ||
* `path` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**  | ||
* `messageHex` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**  | ||
##### Examples | ||
```javascript | ||
acre.signERC4361Message("44'/60'/0'/0'/0", Buffer.from("Example ERC-4361 message").toString("hex")).then(function(result) { | ||
const v = result['v'] + 27 + 4; | ||
const signature = Buffer.from(v.toString(16) + result['r'] + result['s'], 'hex').toString('base64'); | ||
console.log("Signature : " + signature); | ||
}).catch(function(ex) {console.log(ex);}); | ||
``` | ||
Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<{v: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number), r: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), s: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)}>**  | ||
**Note:** The message is restricted to maximum 128 character lines. | ||
Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<{v: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number), r: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), s: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)}>**  | ||
### AcreBtcNew | ||
@@ -499,23 +528,24 @@ | ||
### descrTemplFrom | ||
#### signERC4361Message | ||
This function returns a descriptor template based on the address format. | ||
See <https://github.com/blooo-io/app-acre/blob/develop/doc/wallet.md> for details of | ||
the bitcoin descriptor template. | ||
Signs an ERC-4361 (Sign-In with Ethereum) formatted message with the private key at | ||
the provided derivation path according to the Bitcoin Signature format | ||
and returns v, r, s. | ||
#### Parameters | ||
##### Parameters | ||
* `addressFormat` **[AddressFormat](#addressformat)**  | ||
* `$0` **{path: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), messageHex: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)}** | ||
Returns **DefaultDescriptorTemplate**  | ||
* `$0.path` The BIP32 derivation path of the key to use for signing | ||
* `$0.messageHex` The ERC-4361 formatted message to sign, in hexadecimal format | ||
### AcreBtcOld | ||
Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<{v: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number), r: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), s: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)}>**  | ||
This old API is compatible with versions of the Bitcoin nano app that are earlier than 2.1.0 . | ||
It is never used by Acre, that is based on the latest Bitcoin nano app (2.1.0+). | ||
This class is kept for compatibility purposes. | ||
### descrTemplFrom | ||
#### getWalletPublicKey | ||
This function returns a descriptor template based on the address format. | ||
See <https://github.com/blooo-io/app-acre/blob/develop/doc/wallet.md> for details of | ||
the bitcoin descriptor template. | ||
##### Parameters | ||
#### Parameters | ||
@@ -522,0 +552,0 @@ * `path` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** a BIP 32 path |
@@ -316,2 +316,3 @@ import { crypto } from "bitcoinjs-lib"; | ||
} | ||
cleanHexPrefix(hexString: string): string { | ||
@@ -326,4 +327,2 @@ let cleanedHex = hexString.startsWith("0x") ? hexString.slice(2) : hexString; | ||
formatAcreWithdrawalData(withdrawalData: AcreWithdrawalData): AcreWithdrawalDataBuffer { | ||
console.log("withdrawalData", withdrawalData); | ||
console.log("dataLength", withdrawalData.data.length); | ||
const to = Buffer.from(this.cleanHexPrefix(withdrawalData.to.toString()), "hex").slice(-20); | ||
@@ -390,3 +389,2 @@ | ||
const withdrawalDataBuffer = this.formatAcreWithdrawalData(withdrawalData); | ||
console.log("withdrawalDataBuffer", withdrawalDataBuffer); | ||
@@ -408,2 +406,28 @@ const sig = await this.client.signWithdrawal(pathElements, withdrawalDataBuffer); | ||
/** | ||
* Signs a ERC4361 hex-formatted message with the private key at | ||
* the provided derivation path according to the Bitcoin Signature format | ||
* and returns v, r, s. | ||
*/ | ||
async signERC4361Message({ path, messageHex }: { path: string; messageHex: string }): Promise<{ | ||
v: number; | ||
r: string; | ||
s: string; | ||
}> { | ||
const pathElements: number[] = pathStringToArray(path); | ||
const message = Buffer.from(messageHex, "hex"); | ||
const sig = await this.client.signERC4361Message(message, pathElements); | ||
const buf = Buffer.from(sig, "base64"); | ||
const v = buf.readUInt8() - 27 - 4; | ||
const r = buf.slice(1, 33).toString("hex"); | ||
const s = buf.slice(33, 65).toString("hex"); | ||
return { | ||
v, | ||
r, | ||
s, | ||
}; | ||
} | ||
/** | ||
* Calculates an output script along with public key and possible redeemScript | ||
@@ -410,0 +434,0 @@ * from a path and accountType. The accountPath must be a prefix of path. |
@@ -23,3 +23,4 @@ import Transport from "@ledgerhq/hw-transport"; | ||
SIGN_MESSAGE = 0x10, | ||
SIGN_WITHDRAW = 0x11 | ||
SIGN_WITHDRAW = 0x11, | ||
SIGN_ERC4361_MESSAGE = 0x12 | ||
} | ||
@@ -251,2 +252,28 @@ | ||
} | ||
async signERC4361Message(message: Buffer, pathElements: number[]): Promise<string> { | ||
if (pathElements.length > 6) { | ||
throw new Error("Path too long. At most 6 levels allowed."); | ||
} | ||
const clientInterpreter = new ClientCommandInterpreter(() => {}); | ||
// prepare ClientCommandInterpreter | ||
const nChunks = Math.ceil(message.length / 64); | ||
const chunks: Buffer[] = []; | ||
for (let i = 0; i < nChunks; i++) { | ||
chunks.push(message.subarray(64 * i, 64 * i + 64)); | ||
} | ||
clientInterpreter.addKnownList(chunks); | ||
const chunksRoot = new Merkle(chunks.map(m => hashLeaf(m))).getRoot(); | ||
const response = await this.makeRequest( | ||
BitcoinIns.SIGN_ERC4361_MESSAGE, | ||
Buffer.concat([pathElementsToBuffer(pathElements), createVarint(message.length), chunksRoot]), | ||
clientInterpreter, | ||
); | ||
return response.toString("base64"); | ||
} | ||
} |
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
import { openTransportReplayer, RecordStore } from "@ledgerhq/hw-transport-mocker"; | ||
import { TransportReplayer } from "@ledgerhq/hw-transport-mocker/lib/openTransportReplayer"; | ||
import SpeculosTransport from "../speculosTransport"; | ||
import ecc from "tiny-secp256k1"; | ||
@@ -10,3 +11,3 @@ import { getXpubComponents, pathArrayToString } from "../../src/bip32"; | ||
import { splitTransaction } from "../../src/splitTransaction"; | ||
import { withdrawalAPDUs, signMessageAPDUs } from "./apdus"; | ||
import { withdrawalAPDUs, signMessageAPDUs, signERC4361APDUs } from "./apdus"; | ||
import { | ||
@@ -65,2 +66,6 @@ StandardPurpose, | ||
test("Sign ERC4361 message", async () => { | ||
await testSignERC4361MessageReplayer("m/44'/0'/0'"); | ||
}); | ||
function testPaths(type: StandardPurpose): { ins: string[]; out?: string } { | ||
@@ -233,2 +238,20 @@ const basePath = `m/${type}/1'/0'/`; | ||
async function testSignERC4361MessageReplayer( | ||
accountPath: string, | ||
) { | ||
const transport = await openTransportReplayer(RecordStore.fromString(signERC4361APDUs)); | ||
const client = new AppClient(transport); | ||
const path = accountPath + "/0/0"; | ||
const acreBtcNew = new AcreBtcNew(client); | ||
const message = "stake.acre.fi wants you to sign in with your Bitcoin account:\nbc1q8fq0vs2f9g52cuk8px9f664qs0j7vtmx3r7wvx\n\n\nURI: https://stake.acre.fi\nVersion: 1\nNonce: cw73Kfdfn1lY42Jj8\nIssued At: 2024-10-01T11:03:05.707Z\nExpiration Time: 2024-10-08T11:03:05.707Z" | ||
const result = await acreBtcNew.signERC4361Message({messageHex: Buffer.from(message).toString("hex"), path: path}); | ||
expect(result).toEqual({ | ||
v: 1, | ||
r: 'f30ff91331b840cc97560b468d9dce0647afbef7fd74819773721a096905da7e', | ||
s: '664a3ce374f1951e40222d433cd8d6977dde08af6320acc8dd90fa35ed1c8ed8' | ||
}); | ||
} | ||
function verifyGetWalletPublicKeyResult( | ||
@@ -324,2 +347,2 @@ result: { publicKey: string; bitcoinAddress: string; chainCode: string }, | ||
} | ||
} | ||
} |
@@ -114,1 +114,22 @@ export const withdrawalAPDUs = ` | ||
`; | ||
export const signERC4361APDUs = ` | ||
=> e112000036058000002c80000000800000000000000000000000f714384fe48a178439d013364f5dda49f7996a5551e3c00727531906947ab21fc4 | ||
<= 4114384fe48a178439d013364f5dda49f7996a5551e3c00727531906947ab21fc40400e000 | ||
=> f801000062ed3e2c87ab5f2018467fec3740401cb87241e888f037e374331ae85a1ba5a22702026d870786ec5cd40f6898c4a13b94f8d5bcb50b776a7e83f868779d1a6b0ed170b0448f3bddf3fbc48f3e6d029aa0e43d8b882fd0bd490a18a01699eb4d1397b3 | ||
<= 4000ed3e2c87ab5f2018467fec3740401cb87241e888f037e374331ae85a1ba5a227e000 | ||
=> f8010000434141007374616b652e616372652e66692077616e747320796f7520746f207369676e20696e207769746820796f757220426974636f696e206163636f756e743a0a6263 | ||
<= 4114384fe48a178439d013364f5dda49f7996a5551e3c00727531906947ab21fc40401e000 | ||
=> f8010000626d870786ec5cd40f6898c4a13b94f8d5bcb50b776a7e83f868779d1a6b0ed1700202ed3e2c87ab5f2018467fec3740401cb87241e888f037e374331ae85a1ba5a227b0448f3bddf3fbc48f3e6d029aa0e43d8b882fd0bd490a18a01699eb4d1397b3 | ||
<= 40006d870786ec5cd40f6898c4a13b94f8d5bcb50b776a7e83f868779d1a6b0ed170e000 | ||
=> f801000043414100317138667130767332663967353263756b38707839663636347173306a3776746d783372377776780a0a0a5552493a2068747470733a2f2f7374616b652e6163 | ||
<= 4114384fe48a178439d013364f5dda49f7996a5551e3c00727531906947ab21fc40402e000 | ||
=> f801000062b02d2a861a934a4c9b88de91a2e1c399da88b1023423fdf60d8df66374fa1b9602023e0f091cf7fe3103e1327a1f734f4a2982068f8e9b33dd397d55fbe850967d61021417016261f943d7a7d76b4dc93c9cdac00805ee05e14233439b768148e06a | ||
<= 4000b02d2a861a934a4c9b88de91a2e1c399da88b1023423fdf60d8df66374fa1b96e000 | ||
=> f80100004341410072652e66690a56657273696f6e3a20310a4e6f6e63653a20637737334b6664666e316c5934324a6a380a4973737565642041743a20323032342d31302d303154 | ||
<= 4114384fe48a178439d013364f5dda49f7996a5551e3c00727531906947ab21fc40403e000 | ||
=> f8010000623e0f091cf7fe3103e1327a1f734f4a2982068f8e9b33dd397d55fbe850967d610202b02d2a861a934a4c9b88de91a2e1c399da88b1023423fdf60d8df66374fa1b96021417016261f943d7a7d76b4dc93c9cdac00805ee05e14233439b768148e06a | ||
<= 40003e0f091cf7fe3103e1327a1f734f4a2982068f8e9b33dd397d55fbe850967d61e000 | ||
=> f80100003a38380031313a30333a30352e3730375a0a45787069726174696f6e2054696d653a20323032342d31302d30385431313a30333a30352e3730375a | ||
<= 20f30ff91331b840cc97560b468d9dce0647afbef7fd74819773721a096905da7e664a3ce374f1951e40222d433cd8d6977dde08af6320acc8dd90fa35ed1c8ed89000 | ||
`; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
719314
1.51%11249
1.17%929
3.34%