Skip to content

Commit 2918a7b

Browse files
committed
feat: add support for multiple signer types in NodeArweaveWallet
1 parent 7ede4a8 commit 2918a7b

File tree

3 files changed

+77
-27
lines changed

3 files changed

+77
-27
lines changed

src/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ export const ARWEAVE_CONFIG = {
1414
}
1515

1616
export const arweave = new Arweave(ARWEAVE_CONFIG)
17+
18+
export const DATAITEM_SIGNER_KIND = 'ans104'
19+
export const HTTP_SIGNER_KIND = 'httpsig'

src/index.ts

Lines changed: 72 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
PermissionType,
1414
RsaPssParams,
1515
SignDataItemParams,
16+
Signer,
1617
SigningResponse,
1718
SignMessageOptions,
1819
TokenInfo,
@@ -33,9 +34,11 @@ import {
3334
arweave,
3435
BROWSER_READY_DELAY,
3536
BROWSER_TIMEOUT,
37+
DATAITEM_SIGNER_KIND,
3638
DEFAULT_HOST,
3739
DEFAULT_PORT,
3840
DEFAULT_REQUEST_TIMEOUT,
41+
HTTP_SIGNER_KIND,
3942
SHUTDOWN_DELAY,
4043
} from './constants'
4144
import { base64ToBuffer, bufferToBase64 } from './utils'
@@ -722,7 +725,7 @@ export class NodeArweaveWallet {
722725
* ```
723726
*/
724727
getDataItemSigner() {
725-
return this.createDataItemSigner()
728+
return createDataItemSigner(this)
726729
}
727730

728731
/**
@@ -987,29 +990,6 @@ export class NodeArweaveWallet {
987990
})
988991
}
989992

990-
private createDataItemSigner() {
991-
return async (create: any) => {
992-
const { data, tags, target, anchor } = await create({
993-
alg: 'rsa-v1_5-sha256',
994-
passthrough: true,
995-
})
996-
997-
const signedBuffer = await this.signDataItem({
998-
data,
999-
tags: tags || [],
1000-
target,
1001-
anchor,
1002-
})
1003-
1004-
const dataItem = new ArBundlesDataItem(Buffer.from(signedBuffer))
1005-
const itemId = await dataItem.id
1006-
const rawBuffer = await dataItem.getRaw()
1007-
const raw = new Uint8Array(rawBuffer)
1008-
1009-
return { id: itemId, raw }
1010-
}
1011-
}
1012-
1013993
private async openBrowser(url: string): Promise<void> {
1014994
if (this.config.browser === false) {
1015995
console.log(`\n🌐 Browser URL: ${url}`)
@@ -1146,7 +1126,59 @@ export class NodeArweaveWallet {
11461126
}
11471127
}
11481128

1149-
// ==================== Exports ====================
1129+
function createANS104Signer(arweaveWallet: NodeArweaveWallet) {
1130+
return async (create: any) => {
1131+
const { data, tags, target, anchor } = await create({
1132+
alg: 'rsa-v1_5-sha256',
1133+
passthrough: true,
1134+
})
1135+
1136+
const signedBuffer = await arweaveWallet.signDataItem({
1137+
data,
1138+
tags: tags || [],
1139+
target,
1140+
anchor,
1141+
})
1142+
1143+
const dataItem = new ArBundlesDataItem(Buffer.from(signedBuffer))
1144+
const itemId = await dataItem.id
1145+
const rawBuffer = await dataItem.getRaw()
1146+
const raw = new Uint8Array(rawBuffer)
1147+
1148+
return { id: itemId, raw }
1149+
}
1150+
}
1151+
1152+
function createHttpSigner(arweaveWallet: NodeArweaveWallet) {
1153+
const signer = async (create: any) =>
1154+
arweaveWallet
1155+
.connect(['ACCESS_ADDRESS', 'ACCESS_PUBLIC_KEY', 'SIGNATURE'])
1156+
.then(async () => {
1157+
const [publicKey, address] = await Promise.all([
1158+
arweaveWallet.getActivePublicKey(),
1159+
arweaveWallet.getActiveAddress(),
1160+
])
1161+
return { publicKey, address }
1162+
})
1163+
.then(async ({ publicKey, address }) => {
1164+
const signatureBase = await create({
1165+
type: 1,
1166+
publicKey,
1167+
address,
1168+
alg: 'rsa-pss-sha512',
1169+
})
1170+
1171+
const view = await arweaveWallet.signMessage(signatureBase, { hashAlgorithm: 'SHA-512' })
1172+
1173+
return {
1174+
signature: Buffer.from(view),
1175+
address,
1176+
}
1177+
})
1178+
1179+
return signer
1180+
}
1181+
11501182
/**
11511183
* Creates a DataItemSigner compatible with @permaweb/aoconnect.
11521184
* This is a convenience function that wraps the wallet's getDataItemSigner() method.
@@ -1168,10 +1200,23 @@ export class NodeArweaveWallet {
11681200
* const messageId = await message({
11691201
* process: 'PROCESS_ID',
11701202
* signer,
1171-
* tags: [{ name: 'Action', value: 'Balance' }]
1203+
* tags: [
1204+
* { name: 'Action', value: 'Transfer' },
1205+
* { name: 'Recipient', value: 'address_to_send_to' },
1206+
* { name: 'Quantity', value: '1000000000000000000' }
1207+
* ]
11721208
* })
11731209
* ```
11741210
*/
11751211
export function createDataItemSigner(arweaveWallet: NodeArweaveWallet) {
1176-
return arweaveWallet.getDataItemSigner()
1212+
const dataItemSigner = createANS104Signer(arweaveWallet)
1213+
const httpSigner = createHttpSigner(arweaveWallet)
1214+
1215+
const signer = (create: any, kind: string) => {
1216+
if (kind === DATAITEM_SIGNER_KIND) return dataItemSigner(create)
1217+
if (kind === HTTP_SIGNER_KIND) return httpSigner(create)
1218+
throw new Error(`signer kind unknown "${kind}"`)
1219+
}
1220+
1221+
return signer as Signer
11771222
}

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,5 @@ export interface NodeArweaveWalletConfig {
150150
*/
151151
browserProfile?: string
152152
}
153+
154+
export type Signer = (...args: unknown[]) => unknown

0 commit comments

Comments
 (0)