diff --git a/src/net.ts b/src/net.ts index 046ddce..39f0551 100644 --- a/src/net.ts +++ b/src/net.ts @@ -25,32 +25,30 @@ export const signedFetch = async (url: string, init: RequestInit, signedInit: Si ["Content-Type", `application/ld+json; profile="${CONTEXT}"`] ]; + const newHeaders = new Headers(); + if (signedInit.digest && init.body) { signedHeaders.push(["Digest", signedInit.digest]); } - else if (init.body) { + else if (init.method === "POST" && init.body) { if (Buffer.isBuffer(init.body) || typeof init.body === "string") { signedHeaders.push(["Digest", hashDigest(init.body)]); } else throw "unsupported body type"; + + const signer = new Sha256Signer({ + privateKey: signedInit.privateKey, + publicKeyId: signedInit.keyId + }); + + const signature = signer.sign({ + url, + method: init.method as string, + headers: signedHeaders + }); + + newHeaders.set("Signature", signature); } - else { - throw "no body provided"; - } - - const signer = new Sha256Signer({ - privateKey: signedInit.privateKey, - publicKeyId: signedInit.keyId - }); - - const signature = signer.sign({ - url, - method: init.method as string, - headers: signedHeaders - }); - - const newHeaders = new Headers(); - newHeaders.set("Signature", signature); if (Array.isArray(init.headers) || !init.headers) { for (const header of (init.headers || [])) {